esphome_native_api/
parser.rs

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