esphome_native_api/
parser.rs

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