1use prost::Message;
4
5use crate::proto::{
6 AlarmControlPanelCommandRequest, AlarmControlPanelStateResponse, AuthenticationRequest,
7 AuthenticationResponse, BinarySensorStateResponse, BluetoothConnectionsFreeResponse,
8 BluetoothDeviceClearCacheResponse, BluetoothDeviceConnectionResponse,
9 BluetoothDevicePairingResponse, BluetoothDeviceRequest, BluetoothDeviceUnpairingResponse,
10 BluetoothGattErrorResponse, BluetoothGattGetServicesDoneResponse,
11 BluetoothGattGetServicesRequest, BluetoothGattGetServicesResponse,
12 BluetoothGattNotifyDataResponse, BluetoothGattNotifyRequest, BluetoothGattNotifyResponse,
13 BluetoothGattReadDescriptorRequest, BluetoothGattReadRequest, BluetoothGattReadResponse,
14 BluetoothGattWriteDescriptorRequest, BluetoothGattWriteRequest, BluetoothGattWriteResponse,
15 BluetoothLeAdvertisementResponse, BluetoothLeRawAdvertisementsResponse, ButtonCommandRequest,
16 CameraImageRequest, CameraImageResponse, ClimateCommandRequest, ClimateStateResponse,
17 CoverCommandRequest, CoverStateResponse, DateCommandRequest, DateStateResponse,
18 DateTimeCommandRequest, DateTimeStateResponse, DeviceInfoRequest, DeviceInfoResponse,
19 DisconnectRequest, DisconnectResponse, EventResponse, ExecuteServiceRequest, FanCommandRequest,
20 FanStateResponse, GetTimeRequest, GetTimeResponse, HelloRequest, HelloResponse,
21 HomeAssistantStateResponse, LightCommandRequest, LightStateResponse,
22 ListEntitiesAlarmControlPanelResponse, ListEntitiesBinarySensorResponse,
23 ListEntitiesButtonResponse, ListEntitiesCameraResponse, ListEntitiesClimateResponse,
24 ListEntitiesCoverResponse, ListEntitiesDateResponse, ListEntitiesDateTimeResponse,
25 ListEntitiesDoneResponse, ListEntitiesEventResponse, ListEntitiesFanResponse,
26 ListEntitiesLightResponse, ListEntitiesLockResponse, ListEntitiesMediaPlayerResponse,
27 ListEntitiesNumberResponse, ListEntitiesRequest, ListEntitiesSelectResponse,
28 ListEntitiesSensorResponse, ListEntitiesServicesResponse, ListEntitiesSwitchResponse,
29 ListEntitiesTextResponse, ListEntitiesTextSensorResponse, ListEntitiesTimeResponse,
30 ListEntitiesUpdateResponse, ListEntitiesValveResponse, LockCommandRequest, LockStateResponse,
31 MediaPlayerCommandRequest, MediaPlayerStateResponse, NumberCommandRequest, NumberStateResponse,
32 PingRequest, PingResponse, SelectCommandRequest, SelectStateResponse, SensorStateResponse,
33 SubscribeBluetoothConnectionsFreeRequest, SubscribeBluetoothLeAdvertisementsRequest,
34 SubscribeHomeAssistantStateResponse, SubscribeHomeAssistantStatesRequest,
35 SubscribeHomeassistantServicesRequest, SubscribeLogsRequest, SubscribeLogsResponse,
36 SubscribeStatesRequest, SubscribeVoiceAssistantRequest, SwitchCommandRequest,
37 SwitchStateResponse, TextCommandRequest, TextSensorStateResponse, TextStateResponse,
38 TimeCommandRequest, TimeStateResponse, UnsubscribeBluetoothLeAdvertisementsRequest,
39 UpdateCommandRequest, UpdateStateResponse, ValveCommandRequest, ValveStateResponse,
40 VoiceAssistantAnnounceFinished, VoiceAssistantAnnounceRequest, VoiceAssistantAudio,
41 VoiceAssistantConfigurationRequest, VoiceAssistantConfigurationResponse,
42 VoiceAssistantEventResponse, VoiceAssistantRequest, VoiceAssistantResponse,
43 VoiceAssistantSetConfiguration, VoiceAssistantTimerEventResponse,
44};
45
46macro_rules! proto_message_mappings {
47 ($($type_id:expr => $struct:ident),* $(,)?) => {
48 #[derive(Clone, Debug)]
50 pub enum ProtoMessage {
51 $(
52 $struct($struct),
53 )*
54 }
55
56 pub fn parse_proto_message(message_type: usize, buf: &[u8]) -> Result<ProtoMessage, &'static str> {
58 match message_type {
59 $(
60 $type_id => $struct::decode(buf)
61 .map(ProtoMessage::$struct)
62 .map_err(|_| concat!("Failed to decode ", stringify!($struct))),
63 )*
64 _ => Err(Box::leak(format!("Unknown message type: {}", message_type).into_boxed_str())),
65 }
66 }
67
68 pub fn proto_to_vec(message: &ProtoMessage) -> Result<Vec<u8>, &'static str> {
69 match message {
70 $(
71 ProtoMessage::$struct(msg) => {
72
73 Ok(msg.encode_to_vec())
74 }
75 )*
76 }
77 }
78
79 pub fn message_to_num(message_type: &ProtoMessage) -> Result<u8, &'static str> {
81 match message_type {
82 $(
83 ProtoMessage::$struct(_) => Ok($type_id),
84 )*
85 }
86 }
87 };
88}
89
90proto_message_mappings!(
93 1 => HelloRequest,
94 2 => HelloResponse,
95 3 => AuthenticationRequest,
96 4 => AuthenticationResponse,
97 5 => DisconnectRequest,
98 6 => DisconnectResponse,
99 7 => PingRequest,
100 8 => PingResponse,
101 9 => DeviceInfoRequest,
102 10 => DeviceInfoResponse,
103 11 => ListEntitiesRequest,
104 12 => ListEntitiesBinarySensorResponse,
105 13 => ListEntitiesCoverResponse,
106 14 => ListEntitiesFanResponse,
107 15 => ListEntitiesLightResponse,
108 16 => ListEntitiesSensorResponse,
109 17 => ListEntitiesSwitchResponse,
110 18 => ListEntitiesTextSensorResponse,
111 19 => ListEntitiesDoneResponse,
112 20 => SubscribeStatesRequest,
113 21 => BinarySensorStateResponse,
114 22 => CoverStateResponse,
115 23 => FanStateResponse,
116 24 => LightStateResponse,
117 25 => SensorStateResponse,
118 26 => SwitchStateResponse,
119 27 => TextSensorStateResponse,
120 28 => SubscribeLogsRequest,
121 29 => SubscribeLogsResponse,
122 30 => CoverCommandRequest,
123 31 => FanCommandRequest,
124 32 => LightCommandRequest,
125 33 => SwitchCommandRequest,
126 34 => SubscribeHomeassistantServicesRequest,
127 36 => GetTimeRequest,
129 37 => GetTimeResponse,
130 38 => SubscribeHomeAssistantStatesRequest,
131 39 => SubscribeHomeAssistantStateResponse,
132 40 => HomeAssistantStateResponse,
133 41 => ListEntitiesServicesResponse,
134 42 => ExecuteServiceRequest,
135 43 => ListEntitiesCameraResponse,
136 44 => CameraImageResponse,
137 45 => CameraImageRequest,
138 46 => ListEntitiesClimateResponse,
139 47 => ClimateStateResponse,
140 48 => ClimateCommandRequest,
141 49 => ListEntitiesNumberResponse,
142 50 => NumberStateResponse,
143 51 => NumberCommandRequest,
144 52 => ListEntitiesSelectResponse,
145 53 => SelectStateResponse,
146 54 => SelectCommandRequest,
147 58 => ListEntitiesLockResponse,
151 59 => LockStateResponse,
152 60 => LockCommandRequest,
153 61 => ListEntitiesButtonResponse,
154 62 => ButtonCommandRequest,
155 63 => ListEntitiesMediaPlayerResponse,
156 64 => MediaPlayerStateResponse,
157 65 => MediaPlayerCommandRequest,
158 66 => SubscribeBluetoothLeAdvertisementsRequest,
159 67 => BluetoothLeAdvertisementResponse,
160 68 => BluetoothDeviceRequest,
161 69 => BluetoothDeviceConnectionResponse,
162 70 => BluetoothGattGetServicesRequest,
163 71 => BluetoothGattGetServicesResponse,
164 72 => BluetoothGattGetServicesDoneResponse,
165 73 => BluetoothGattReadRequest,
166 74 => BluetoothGattReadResponse,
167 75 => BluetoothGattWriteRequest,
168 76 => BluetoothGattReadDescriptorRequest,
169 77 => BluetoothGattWriteDescriptorRequest,
170 78 => BluetoothGattNotifyRequest,
171 79 => BluetoothGattNotifyDataResponse,
172 80 => SubscribeBluetoothConnectionsFreeRequest,
173 81 => BluetoothConnectionsFreeResponse,
174 82 => BluetoothGattErrorResponse,
175 83 => BluetoothGattWriteResponse,
176 84 => BluetoothGattNotifyResponse,
177 85 => BluetoothDevicePairingResponse,
178 86 => BluetoothDeviceUnpairingResponse,
179 87 => UnsubscribeBluetoothLeAdvertisementsRequest,
180 88 => BluetoothDeviceClearCacheResponse,
181 89 => SubscribeVoiceAssistantRequest,
182 90 => VoiceAssistantRequest,
183 91 => VoiceAssistantResponse,
184 92 => VoiceAssistantEventResponse,
185 93 => BluetoothLeRawAdvertisementsResponse,
186 94 => ListEntitiesAlarmControlPanelResponse,
187 95 => AlarmControlPanelStateResponse,
188 96 => AlarmControlPanelCommandRequest,
189 97 => ListEntitiesTextResponse,
190 98 => TextStateResponse,
191 99 => TextCommandRequest,
192 100 => ListEntitiesDateResponse,
193 101 => DateStateResponse,
194 102 => DateCommandRequest,
195 103 => ListEntitiesTimeResponse,
196 104 => TimeStateResponse,
197 105 => TimeCommandRequest,
198 106 => VoiceAssistantAudio,
199 107 => ListEntitiesEventResponse,
200 108 => EventResponse,
201 109 => ListEntitiesValveResponse,
202 110 => ValveStateResponse,
203 111 => ValveCommandRequest,
204 112 => ListEntitiesDateTimeResponse,
205 113 => DateTimeStateResponse,
206 114 => DateTimeCommandRequest,
207 115 => VoiceAssistantTimerEventResponse,
208 116 => ListEntitiesUpdateResponse,
209 117 => UpdateStateResponse,
210 118 => UpdateCommandRequest,
211 119 => VoiceAssistantAnnounceRequest,
212 120 => VoiceAssistantAnnounceFinished,
213 121 => VoiceAssistantConfigurationRequest,
214 122 => VoiceAssistantConfigurationResponse,
215 123 => VoiceAssistantSetConfiguration,
216);