From 3c864b2bcae8bd06176b38b7806d79581e3a47bd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 11 Jul 2025 17:56:08 -1000 Subject: [PATCH] Reduce API flash usage by eliminating unnecessary template instantiations (#9452) --- esphome/components/api/api_pb2.cpp | 360 +++++++++++++++------------- esphome/components/api/proto.h | 55 +++-- script/api_protobuf/api_protobuf.py | 42 +++- 3 files changed, 259 insertions(+), 198 deletions(-) diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 0c110b8c8b..062ff54eb8 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -257,15 +257,17 @@ bool DeviceInfoResponse::decode_length(uint32_t field_id, ProtoLengthDelimited v return true; } case 20: { - this->devices.push_back(value.as_message()); + this->devices.emplace_back(); + value.decode_to_message(this->devices.back()); return true; } case 21: { - this->areas.push_back(value.as_message()); + this->areas.emplace_back(); + value.decode_to_message(this->areas.back()); return true; } case 22: { - this->area = value.as_message(); + value.decode_to_message(this->area); return true; } default: @@ -293,12 +295,12 @@ void DeviceInfoResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(18, this->bluetooth_mac_address); buffer.encode_bool(19, this->api_encryption_supported); for (auto &it : this->devices) { - buffer.encode_message(20, it, true); + buffer.encode_message(20, it, true); } for (auto &it : this->areas) { - buffer.encode_message(21, it, true); + buffer.encode_message(21, it, true); } - buffer.encode_message(22, this->area); + buffer.encode_message(22, this->area); } void DeviceInfoResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_bool_field(total_size, 1, this->uses_password); @@ -336,7 +338,7 @@ bool ListEntitiesBinarySensorResponse::decode_varint(uint32_t field_id, ProtoVar return true; } case 9: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 10: { @@ -392,7 +394,7 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(6, this->is_status_binary_sensor); buffer.encode_bool(7, this->disabled_by_default); buffer.encode_string(8, this->icon); - buffer.encode_enum(9, this->entity_category); + buffer.encode_uint32(9, static_cast(this->entity_category)); buffer.encode_uint32(10, this->device_id); } void ListEntitiesBinarySensorResponse::calculate_size(uint32_t &total_size) const { @@ -468,7 +470,7 @@ bool ListEntitiesCoverResponse::decode_varint(uint32_t field_id, ProtoVarInt val return true; } case 11: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 12: { @@ -530,7 +532,7 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(8, this->device_class); buffer.encode_bool(9, this->disabled_by_default); buffer.encode_string(10, this->icon); - buffer.encode_enum(11, this->entity_category); + buffer.encode_uint32(11, static_cast(this->entity_category)); buffer.encode_bool(12, this->supports_stop); buffer.encode_uint32(13, this->device_id); } @@ -552,11 +554,11 @@ void ListEntitiesCoverResponse::calculate_size(uint32_t &total_size) const { bool CoverStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 2: { - this->legacy_state = value.as_enum(); + this->legacy_state = static_cast(value.as_uint32()); return true; } case 5: { - this->current_operation = value.as_enum(); + this->current_operation = static_cast(value.as_uint32()); return true; } case 6: { @@ -587,10 +589,10 @@ bool CoverStateResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { } void CoverStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); - buffer.encode_enum(2, this->legacy_state); + buffer.encode_uint32(2, static_cast(this->legacy_state)); buffer.encode_float(3, this->position); buffer.encode_float(4, this->tilt); - buffer.encode_enum(5, this->current_operation); + buffer.encode_uint32(5, static_cast(this->current_operation)); buffer.encode_uint32(6, this->device_id); } void CoverStateResponse::calculate_size(uint32_t &total_size) const { @@ -608,7 +610,7 @@ bool CoverCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { return true; } case 3: { - this->legacy_command = value.as_enum(); + this->legacy_command = static_cast(value.as_uint32()); return true; } case 4: { @@ -652,7 +654,7 @@ bool CoverCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { void CoverCommandRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); buffer.encode_bool(2, this->has_legacy_command); - buffer.encode_enum(3, this->legacy_command); + buffer.encode_uint32(3, static_cast(this->legacy_command)); buffer.encode_bool(4, this->has_position); buffer.encode_float(5, this->position); buffer.encode_bool(6, this->has_tilt); @@ -696,7 +698,7 @@ bool ListEntitiesFanResponse::decode_varint(uint32_t field_id, ProtoVarInt value return true; } case 11: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 13: { @@ -754,7 +756,7 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_int32(8, this->supported_speed_count); buffer.encode_bool(9, this->disabled_by_default); buffer.encode_string(10, this->icon); - buffer.encode_enum(11, this->entity_category); + buffer.encode_uint32(11, static_cast(this->entity_category)); for (auto &it : this->supported_preset_modes) { buffer.encode_string(12, it, true); } @@ -790,11 +792,11 @@ bool FanStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { return true; } case 4: { - this->speed = value.as_enum(); + this->speed = static_cast(value.as_uint32()); return true; } case 5: { - this->direction = value.as_enum(); + this->direction = static_cast(value.as_uint32()); return true; } case 6: { @@ -833,8 +835,8 @@ void FanStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); buffer.encode_bool(2, this->state); buffer.encode_bool(3, this->oscillating); - buffer.encode_enum(4, this->speed); - buffer.encode_enum(5, this->direction); + buffer.encode_uint32(4, static_cast(this->speed)); + buffer.encode_uint32(5, static_cast(this->direction)); buffer.encode_int32(6, this->speed_level); buffer.encode_string(7, this->preset_mode); buffer.encode_uint32(8, this->device_id); @@ -864,7 +866,7 @@ bool FanCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { return true; } case 5: { - this->speed = value.as_enum(); + this->speed = static_cast(value.as_uint32()); return true; } case 6: { @@ -880,7 +882,7 @@ bool FanCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { return true; } case 9: { - this->direction = value.as_enum(); + this->direction = static_cast(value.as_uint32()); return true; } case 10: { @@ -928,11 +930,11 @@ void FanCommandRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(2, this->has_state); buffer.encode_bool(3, this->state); buffer.encode_bool(4, this->has_speed); - buffer.encode_enum(5, this->speed); + buffer.encode_uint32(5, static_cast(this->speed)); buffer.encode_bool(6, this->has_oscillating); buffer.encode_bool(7, this->oscillating); buffer.encode_bool(8, this->has_direction); - buffer.encode_enum(9, this->direction); + buffer.encode_uint32(9, static_cast(this->direction)); buffer.encode_bool(10, this->has_speed_level); buffer.encode_int32(11, this->speed_level); buffer.encode_bool(12, this->has_preset_mode); @@ -960,7 +962,7 @@ void FanCommandRequest::calculate_size(uint32_t &total_size) const { bool ListEntitiesLightResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 12: { - this->supported_color_modes.push_back(value.as_enum()); + this->supported_color_modes.push_back(static_cast(value.as_uint32())); return true; } case 5: { @@ -984,7 +986,7 @@ bool ListEntitiesLightResponse::decode_varint(uint32_t field_id, ProtoVarInt val return true; } case 15: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 16: { @@ -1045,7 +1047,7 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(3, this->name); buffer.encode_string(4, this->unique_id); for (auto &it : this->supported_color_modes) { - buffer.encode_enum(12, it, true); + buffer.encode_uint32(12, static_cast(it), true); } buffer.encode_bool(5, this->legacy_supports_brightness); buffer.encode_bool(6, this->legacy_supports_rgb); @@ -1058,7 +1060,7 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer buffer) const { } buffer.encode_bool(13, this->disabled_by_default); buffer.encode_string(14, this->icon); - buffer.encode_enum(15, this->entity_category); + buffer.encode_uint32(15, static_cast(this->entity_category)); buffer.encode_uint32(16, this->device_id); } void ListEntitiesLightResponse::calculate_size(uint32_t &total_size) const { @@ -1094,7 +1096,7 @@ bool LightStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { return true; } case 11: { - this->color_mode = value.as_enum(); + this->color_mode = static_cast(value.as_uint32()); return true; } case 14: { @@ -1165,7 +1167,7 @@ void LightStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); buffer.encode_bool(2, this->state); buffer.encode_float(3, this->brightness); - buffer.encode_enum(11, this->color_mode); + buffer.encode_uint32(11, static_cast(this->color_mode)); buffer.encode_float(10, this->color_brightness); buffer.encode_float(4, this->red); buffer.encode_float(5, this->green); @@ -1212,7 +1214,7 @@ bool LightCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { return true; } case 23: { - this->color_mode = value.as_enum(); + this->color_mode = static_cast(value.as_uint32()); return true; } case 20: { @@ -1330,7 +1332,7 @@ void LightCommandRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(4, this->has_brightness); buffer.encode_float(5, this->brightness); buffer.encode_bool(22, this->has_color_mode); - buffer.encode_enum(23, this->color_mode); + buffer.encode_uint32(23, static_cast(this->color_mode)); buffer.encode_bool(20, this->has_color_brightness); buffer.encode_float(21, this->color_brightness); buffer.encode_bool(6, this->has_rgb); @@ -1396,11 +1398,11 @@ bool ListEntitiesSensorResponse::decode_varint(uint32_t field_id, ProtoVarInt va return true; } case 10: { - this->state_class = value.as_enum(); + this->state_class = static_cast(value.as_uint32()); return true; } case 11: { - this->legacy_last_reset_type = value.as_enum(); + this->legacy_last_reset_type = static_cast(value.as_uint32()); return true; } case 12: { @@ -1408,7 +1410,7 @@ bool ListEntitiesSensorResponse::decode_varint(uint32_t field_id, ProtoVarInt va return true; } case 13: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 14: { @@ -1469,10 +1471,10 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_int32(7, this->accuracy_decimals); buffer.encode_bool(8, this->force_update); buffer.encode_string(9, this->device_class); - buffer.encode_enum(10, this->state_class); - buffer.encode_enum(11, this->legacy_last_reset_type); + buffer.encode_uint32(10, static_cast(this->state_class)); + buffer.encode_uint32(11, static_cast(this->legacy_last_reset_type)); buffer.encode_bool(12, this->disabled_by_default); - buffer.encode_enum(13, this->entity_category); + buffer.encode_uint32(13, static_cast(this->entity_category)); buffer.encode_uint32(14, this->device_id); } void ListEntitiesSensorResponse::calculate_size(uint32_t &total_size) const { @@ -1544,7 +1546,7 @@ bool ListEntitiesSwitchResponse::decode_varint(uint32_t field_id, ProtoVarInt va return true; } case 8: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 10: { @@ -1599,7 +1601,7 @@ void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->assumed_state); buffer.encode_bool(7, this->disabled_by_default); - buffer.encode_enum(8, this->entity_category); + buffer.encode_uint32(8, static_cast(this->entity_category)); buffer.encode_string(9, this->device_class); buffer.encode_uint32(10, this->device_id); } @@ -1692,7 +1694,7 @@ bool ListEntitiesTextSensorResponse::decode_varint(uint32_t field_id, ProtoVarIn return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 9: { @@ -1746,7 +1748,7 @@ void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); buffer.encode_uint32(9, this->device_id); } @@ -1811,7 +1813,7 @@ void TextSensorStateResponse::calculate_size(uint32_t &total_size) const { bool SubscribeLogsRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 1: { - this->level = value.as_enum(); + this->level = static_cast(value.as_uint32()); return true; } case 2: { @@ -1823,7 +1825,7 @@ bool SubscribeLogsRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { } } void SubscribeLogsRequest::encode(ProtoWriteBuffer buffer) const { - buffer.encode_enum(1, this->level); + buffer.encode_uint32(1, static_cast(this->level)); buffer.encode_bool(2, this->dump_config); } void SubscribeLogsRequest::calculate_size(uint32_t &total_size) const { @@ -1833,7 +1835,7 @@ void SubscribeLogsRequest::calculate_size(uint32_t &total_size) const { bool SubscribeLogsResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 1: { - this->level = value.as_enum(); + this->level = static_cast(value.as_uint32()); return true; } case 4: { @@ -1855,7 +1857,7 @@ bool SubscribeLogsResponse::decode_length(uint32_t field_id, ProtoLengthDelimite } } void SubscribeLogsResponse::encode(ProtoWriteBuffer buffer) const { - buffer.encode_enum(1, this->level); + buffer.encode_uint32(1, static_cast(this->level)); buffer.encode_bytes(3, reinterpret_cast(this->message.data()), this->message.size()); buffer.encode_bool(4, this->send_failed); } @@ -1935,15 +1937,18 @@ bool HomeassistantServiceResponse::decode_length(uint32_t field_id, ProtoLengthD return true; } case 2: { - this->data.push_back(value.as_message()); + this->data.emplace_back(); + value.decode_to_message(this->data.back()); return true; } case 3: { - this->data_template.push_back(value.as_message()); + this->data_template.emplace_back(); + value.decode_to_message(this->data_template.back()); return true; } case 4: { - this->variables.push_back(value.as_message()); + this->variables.emplace_back(); + value.decode_to_message(this->variables.back()); return true; } default: @@ -1953,13 +1958,13 @@ bool HomeassistantServiceResponse::decode_length(uint32_t field_id, ProtoLengthD void HomeassistantServiceResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->service); for (auto &it : this->data) { - buffer.encode_message(2, it, true); + buffer.encode_message(2, it, true); } for (auto &it : this->data_template) { - buffer.encode_message(3, it, true); + buffer.encode_message(3, it, true); } for (auto &it : this->variables) { - buffer.encode_message(4, it, true); + buffer.encode_message(4, it, true); } buffer.encode_bool(5, this->is_event); } @@ -2049,7 +2054,7 @@ void GetTimeResponse::calculate_size(uint32_t &total_size) const { bool ListEntitiesServicesArgument::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 2: { - this->type = value.as_enum(); + this->type = static_cast(value.as_uint32()); return true; } default: @@ -2068,7 +2073,7 @@ bool ListEntitiesServicesArgument::decode_length(uint32_t field_id, ProtoLengthD } void ListEntitiesServicesArgument::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->name); - buffer.encode_enum(2, this->type); + buffer.encode_uint32(2, static_cast(this->type)); } void ListEntitiesServicesArgument::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->name); @@ -2081,7 +2086,8 @@ bool ListEntitiesServicesResponse::decode_length(uint32_t field_id, ProtoLengthD return true; } case 3: { - this->args.push_back(value.as_message()); + this->args.emplace_back(); + value.decode_to_message(this->args.back()); return true; } default: @@ -2102,7 +2108,7 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->name); buffer.encode_fixed32(2, this->key); for (auto &it : this->args) { - buffer.encode_message(3, it, true); + buffer.encode_message(3, it, true); } } void ListEntitiesServicesResponse::calculate_size(uint32_t &total_size) const { @@ -2211,7 +2217,8 @@ void ExecuteServiceArgument::calculate_size(uint32_t &total_size) const { bool ExecuteServiceRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { case 2: { - this->args.push_back(value.as_message()); + this->args.emplace_back(); + value.decode_to_message(this->args.back()); return true; } default: @@ -2231,7 +2238,7 @@ bool ExecuteServiceRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { void ExecuteServiceRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); for (auto &it : this->args) { - buffer.encode_message(2, it, true); + buffer.encode_message(2, it, true); } } void ExecuteServiceRequest::calculate_size(uint32_t &total_size) const { @@ -2246,7 +2253,7 @@ bool ListEntitiesCameraResponse::decode_varint(uint32_t field_id, ProtoVarInt va return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 8: { @@ -2296,7 +2303,7 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_bool(5, this->disabled_by_default); buffer.encode_string(6, this->icon); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_uint32(8, this->device_id); } void ListEntitiesCameraResponse::calculate_size(uint32_t &total_size) const { @@ -2390,7 +2397,7 @@ bool ListEntitiesClimateResponse::decode_varint(uint32_t field_id, ProtoVarInt v return true; } case 7: { - this->supported_modes.push_back(value.as_enum()); + this->supported_modes.push_back(static_cast(value.as_uint32())); return true; } case 11: { @@ -2402,15 +2409,15 @@ bool ListEntitiesClimateResponse::decode_varint(uint32_t field_id, ProtoVarInt v return true; } case 13: { - this->supported_fan_modes.push_back(value.as_enum()); + this->supported_fan_modes.push_back(static_cast(value.as_uint32())); return true; } case 14: { - this->supported_swing_modes.push_back(value.as_enum()); + this->supported_swing_modes.push_back(static_cast(value.as_uint32())); return true; } case 16: { - this->supported_presets.push_back(value.as_enum()); + this->supported_presets.push_back(static_cast(value.as_uint32())); return true; } case 18: { @@ -2418,7 +2425,7 @@ bool ListEntitiesClimateResponse::decode_varint(uint32_t field_id, ProtoVarInt v return true; } case 20: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 22: { @@ -2509,7 +2516,7 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(5, this->supports_current_temperature); buffer.encode_bool(6, this->supports_two_point_target_temperature); for (auto &it : this->supported_modes) { - buffer.encode_enum(7, it, true); + buffer.encode_uint32(7, static_cast(it), true); } buffer.encode_float(8, this->visual_min_temperature); buffer.encode_float(9, this->visual_max_temperature); @@ -2517,23 +2524,23 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(11, this->legacy_supports_away); buffer.encode_bool(12, this->supports_action); for (auto &it : this->supported_fan_modes) { - buffer.encode_enum(13, it, true); + buffer.encode_uint32(13, static_cast(it), true); } for (auto &it : this->supported_swing_modes) { - buffer.encode_enum(14, it, true); + buffer.encode_uint32(14, static_cast(it), true); } for (auto &it : this->supported_custom_fan_modes) { buffer.encode_string(15, it, true); } for (auto &it : this->supported_presets) { - buffer.encode_enum(16, it, true); + buffer.encode_uint32(16, static_cast(it), true); } for (auto &it : this->supported_custom_presets) { buffer.encode_string(17, it, true); } buffer.encode_bool(18, this->disabled_by_default); buffer.encode_string(19, this->icon); - buffer.encode_enum(20, this->entity_category); + buffer.encode_uint32(20, static_cast(this->entity_category)); buffer.encode_float(21, this->visual_current_temperature_step); buffer.encode_bool(22, this->supports_current_humidity); buffer.encode_bool(23, this->supports_target_humidity); @@ -2596,7 +2603,7 @@ void ListEntitiesClimateResponse::calculate_size(uint32_t &total_size) const { bool ClimateStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 2: { - this->mode = value.as_enum(); + this->mode = static_cast(value.as_uint32()); return true; } case 7: { @@ -2604,19 +2611,19 @@ bool ClimateStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { return true; } case 8: { - this->action = value.as_enum(); + this->action = static_cast(value.as_uint32()); return true; } case 9: { - this->fan_mode = value.as_enum(); + this->fan_mode = static_cast(value.as_uint32()); return true; } case 10: { - this->swing_mode = value.as_enum(); + this->swing_mode = static_cast(value.as_uint32()); return true; } case 12: { - this->preset = value.as_enum(); + this->preset = static_cast(value.as_uint32()); return true; } case 16: { @@ -2677,17 +2684,17 @@ bool ClimateStateResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { } void ClimateStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); - buffer.encode_enum(2, this->mode); + buffer.encode_uint32(2, static_cast(this->mode)); buffer.encode_float(3, this->current_temperature); buffer.encode_float(4, this->target_temperature); buffer.encode_float(5, this->target_temperature_low); buffer.encode_float(6, this->target_temperature_high); buffer.encode_bool(7, this->unused_legacy_away); - buffer.encode_enum(8, this->action); - buffer.encode_enum(9, this->fan_mode); - buffer.encode_enum(10, this->swing_mode); + buffer.encode_uint32(8, static_cast(this->action)); + buffer.encode_uint32(9, static_cast(this->fan_mode)); + buffer.encode_uint32(10, static_cast(this->swing_mode)); buffer.encode_string(11, this->custom_fan_mode); - buffer.encode_enum(12, this->preset); + buffer.encode_uint32(12, static_cast(this->preset)); buffer.encode_string(13, this->custom_preset); buffer.encode_float(14, this->current_humidity); buffer.encode_float(15, this->target_humidity); @@ -2718,7 +2725,7 @@ bool ClimateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) return true; } case 3: { - this->mode = value.as_enum(); + this->mode = static_cast(value.as_uint32()); return true; } case 4: { @@ -2746,7 +2753,7 @@ bool ClimateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) return true; } case 13: { - this->fan_mode = value.as_enum(); + this->fan_mode = static_cast(value.as_uint32()); return true; } case 14: { @@ -2754,7 +2761,7 @@ bool ClimateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) return true; } case 15: { - this->swing_mode = value.as_enum(); + this->swing_mode = static_cast(value.as_uint32()); return true; } case 16: { @@ -2766,7 +2773,7 @@ bool ClimateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) return true; } case 19: { - this->preset = value.as_enum(); + this->preset = static_cast(value.as_uint32()); return true; } case 20: { @@ -2828,7 +2835,7 @@ bool ClimateCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { void ClimateCommandRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); buffer.encode_bool(2, this->has_mode); - buffer.encode_enum(3, this->mode); + buffer.encode_uint32(3, static_cast(this->mode)); buffer.encode_bool(4, this->has_target_temperature); buffer.encode_float(5, this->target_temperature); buffer.encode_bool(6, this->has_target_temperature_low); @@ -2838,13 +2845,13 @@ void ClimateCommandRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(10, this->unused_has_legacy_away); buffer.encode_bool(11, this->unused_legacy_away); buffer.encode_bool(12, this->has_fan_mode); - buffer.encode_enum(13, this->fan_mode); + buffer.encode_uint32(13, static_cast(this->fan_mode)); buffer.encode_bool(14, this->has_swing_mode); - buffer.encode_enum(15, this->swing_mode); + buffer.encode_uint32(15, static_cast(this->swing_mode)); buffer.encode_bool(16, this->has_custom_fan_mode); buffer.encode_string(17, this->custom_fan_mode); buffer.encode_bool(18, this->has_preset); - buffer.encode_enum(19, this->preset); + buffer.encode_uint32(19, static_cast(this->preset)); buffer.encode_bool(20, this->has_custom_preset); buffer.encode_string(21, this->custom_preset); buffer.encode_bool(22, this->has_target_humidity); @@ -2886,11 +2893,11 @@ bool ListEntitiesNumberResponse::decode_varint(uint32_t field_id, ProtoVarInt va return true; } case 10: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 12: { - this->mode = value.as_enum(); + this->mode = static_cast(value.as_uint32()); return true; } case 14: { @@ -2963,9 +2970,9 @@ void ListEntitiesNumberResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_float(7, this->max_value); buffer.encode_float(8, this->step); buffer.encode_bool(9, this->disabled_by_default); - buffer.encode_enum(10, this->entity_category); + buffer.encode_uint32(10, static_cast(this->entity_category)); buffer.encode_string(11, this->unit_of_measurement); - buffer.encode_enum(12, this->mode); + buffer.encode_uint32(12, static_cast(this->mode)); buffer.encode_string(13, this->device_class); buffer.encode_uint32(14, this->device_id); } @@ -3068,7 +3075,7 @@ bool ListEntitiesSelectResponse::decode_varint(uint32_t field_id, ProtoVarInt va return true; } case 8: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 9: { @@ -3125,7 +3132,7 @@ void ListEntitiesSelectResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(6, it, true); } buffer.encode_bool(7, this->disabled_by_default); - buffer.encode_enum(8, this->entity_category); + buffer.encode_uint32(8, static_cast(this->entity_category)); buffer.encode_uint32(9, this->device_id); } void ListEntitiesSelectResponse::calculate_size(uint32_t &total_size) const { @@ -3246,7 +3253,7 @@ bool ListEntitiesSirenResponse::decode_varint(uint32_t field_id, ProtoVarInt val return true; } case 10: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 11: { @@ -3305,7 +3312,7 @@ void ListEntitiesSirenResponse::encode(ProtoWriteBuffer buffer) const { } buffer.encode_bool(8, this->supports_duration); buffer.encode_bool(9, this->supports_volume); - buffer.encode_enum(10, this->entity_category); + buffer.encode_uint32(10, static_cast(this->entity_category)); buffer.encode_uint32(11, this->device_id); } void ListEntitiesSirenResponse::calculate_size(uint32_t &total_size) const { @@ -3450,7 +3457,7 @@ bool ListEntitiesLockResponse::decode_varint(uint32_t field_id, ProtoVarInt valu return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 8: { @@ -3516,7 +3523,7 @@ void ListEntitiesLockResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_bool(8, this->assumed_state); buffer.encode_bool(9, this->supports_open); buffer.encode_bool(10, this->requires_code); @@ -3540,7 +3547,7 @@ void ListEntitiesLockResponse::calculate_size(uint32_t &total_size) const { bool LockStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 2: { - this->state = value.as_enum(); + this->state = static_cast(value.as_uint32()); return true; } case 3: { @@ -3563,7 +3570,7 @@ bool LockStateResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { } void LockStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); - buffer.encode_enum(2, this->state); + buffer.encode_uint32(2, static_cast(this->state)); buffer.encode_uint32(3, this->device_id); } void LockStateResponse::calculate_size(uint32_t &total_size) const { @@ -3574,7 +3581,7 @@ void LockStateResponse::calculate_size(uint32_t &total_size) const { bool LockCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 2: { - this->command = value.as_enum(); + this->command = static_cast(value.as_uint32()); return true; } case 3: { @@ -3611,7 +3618,7 @@ bool LockCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { } void LockCommandRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); - buffer.encode_enum(2, this->command); + buffer.encode_uint32(2, static_cast(this->command)); buffer.encode_bool(3, this->has_code); buffer.encode_string(4, this->code); buffer.encode_uint32(5, this->device_id); @@ -3632,7 +3639,7 @@ bool ListEntitiesButtonResponse::decode_varint(uint32_t field_id, ProtoVarInt va return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 9: { @@ -3686,7 +3693,7 @@ void ListEntitiesButtonResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); buffer.encode_uint32(9, this->device_id); } @@ -3742,7 +3749,7 @@ bool MediaPlayerSupportedFormat::decode_varint(uint32_t field_id, ProtoVarInt va return true; } case 4: { - this->purpose = value.as_enum(); + this->purpose = static_cast(value.as_uint32()); return true; } case 5: { @@ -3767,7 +3774,7 @@ void MediaPlayerSupportedFormat::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->format); buffer.encode_uint32(2, this->sample_rate); buffer.encode_uint32(3, this->num_channels); - buffer.encode_enum(4, this->purpose); + buffer.encode_uint32(4, static_cast(this->purpose)); buffer.encode_uint32(5, this->sample_bytes); } void MediaPlayerSupportedFormat::calculate_size(uint32_t &total_size) const { @@ -3784,7 +3791,7 @@ bool ListEntitiesMediaPlayerResponse::decode_varint(uint32_t field_id, ProtoVarI return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 8: { @@ -3818,7 +3825,8 @@ bool ListEntitiesMediaPlayerResponse::decode_length(uint32_t field_id, ProtoLeng return true; } case 9: { - this->supported_formats.push_back(value.as_message()); + this->supported_formats.emplace_back(); + value.decode_to_message(this->supported_formats.back()); return true; } default: @@ -3842,10 +3850,10 @@ void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_bool(8, this->supports_pause); for (auto &it : this->supported_formats) { - buffer.encode_message(9, it, true); + buffer.encode_message(9, it, true); } buffer.encode_uint32(10, this->device_id); } @@ -3864,7 +3872,7 @@ void ListEntitiesMediaPlayerResponse::calculate_size(uint32_t &total_size) const bool MediaPlayerStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 2: { - this->state = value.as_enum(); + this->state = static_cast(value.as_uint32()); return true; } case 4: { @@ -3895,7 +3903,7 @@ bool MediaPlayerStateResponse::decode_32bit(uint32_t field_id, Proto32Bit value) } void MediaPlayerStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); - buffer.encode_enum(2, this->state); + buffer.encode_uint32(2, static_cast(this->state)); buffer.encode_float(3, this->volume); buffer.encode_bool(4, this->muted); buffer.encode_uint32(5, this->device_id); @@ -3914,7 +3922,7 @@ bool MediaPlayerCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt val return true; } case 3: { - this->command = value.as_enum(); + this->command = static_cast(value.as_uint32()); return true; } case 4: { @@ -3968,7 +3976,7 @@ bool MediaPlayerCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value void MediaPlayerCommandRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); buffer.encode_bool(2, this->has_command); - buffer.encode_enum(3, this->command); + buffer.encode_uint32(3, static_cast(this->command)); buffer.encode_bool(4, this->has_volume); buffer.encode_float(5, this->volume); buffer.encode_bool(6, this->has_media_url); @@ -4076,11 +4084,13 @@ bool BluetoothLEAdvertisementResponse::decode_length(uint32_t field_id, ProtoLen return true; } case 5: { - this->service_data.push_back(value.as_message()); + this->service_data.emplace_back(); + value.decode_to_message(this->service_data.back()); return true; } case 6: { - this->manufacturer_data.push_back(value.as_message()); + this->manufacturer_data.emplace_back(); + value.decode_to_message(this->manufacturer_data.back()); return true; } default: @@ -4095,10 +4105,10 @@ void BluetoothLEAdvertisementResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, it, true); } for (auto &it : this->service_data) { - buffer.encode_message(5, it, true); + buffer.encode_message(5, it, true); } for (auto &it : this->manufacturer_data) { - buffer.encode_message(6, it, true); + buffer.encode_message(6, it, true); } buffer.encode_uint32(7, this->address_type); } @@ -4158,7 +4168,8 @@ void BluetoothLERawAdvertisement::calculate_size(uint32_t &total_size) const { bool BluetoothLERawAdvertisementsResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { case 1: { - this->advertisements.push_back(value.as_message()); + this->advertisements.emplace_back(); + value.decode_to_message(this->advertisements.back()); return true; } default: @@ -4167,7 +4178,7 @@ bool BluetoothLERawAdvertisementsResponse::decode_length(uint32_t field_id, Prot } void BluetoothLERawAdvertisementsResponse::encode(ProtoWriteBuffer buffer) const { for (auto &it : this->advertisements) { - buffer.encode_message(1, it, true); + buffer.encode_message(1, it, true); } } void BluetoothLERawAdvertisementsResponse::calculate_size(uint32_t &total_size) const { @@ -4180,7 +4191,7 @@ bool BluetoothDeviceRequest::decode_varint(uint32_t field_id, ProtoVarInt value) return true; } case 2: { - this->request_type = value.as_enum(); + this->request_type = static_cast(value.as_uint32()); return true; } case 3: { @@ -4197,7 +4208,7 @@ bool BluetoothDeviceRequest::decode_varint(uint32_t field_id, ProtoVarInt value) } void BluetoothDeviceRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); - buffer.encode_enum(2, this->request_type); + buffer.encode_uint32(2, static_cast(this->request_type)); buffer.encode_bool(3, this->has_address_type); buffer.encode_uint32(4, this->address_type); } @@ -4304,7 +4315,8 @@ bool BluetoothGATTCharacteristic::decode_varint(uint32_t field_id, ProtoVarInt v bool BluetoothGATTCharacteristic::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { case 4: { - this->descriptors.push_back(value.as_message()); + this->descriptors.emplace_back(); + value.decode_to_message(this->descriptors.back()); return true; } default: @@ -4318,7 +4330,7 @@ void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(2, this->handle); buffer.encode_uint32(3, this->properties); for (auto &it : this->descriptors) { - buffer.encode_message(4, it, true); + buffer.encode_message(4, it, true); } } void BluetoothGATTCharacteristic::calculate_size(uint32_t &total_size) const { @@ -4348,7 +4360,8 @@ bool BluetoothGATTService::decode_varint(uint32_t field_id, ProtoVarInt value) { bool BluetoothGATTService::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { case 3: { - this->characteristics.push_back(value.as_message()); + this->characteristics.emplace_back(); + value.decode_to_message(this->characteristics.back()); return true; } default: @@ -4361,7 +4374,7 @@ void BluetoothGATTService::encode(ProtoWriteBuffer buffer) const { } buffer.encode_uint32(2, this->handle); for (auto &it : this->characteristics) { - buffer.encode_message(3, it, true); + buffer.encode_message(3, it, true); } } void BluetoothGATTService::calculate_size(uint32_t &total_size) const { @@ -4386,7 +4399,8 @@ bool BluetoothGATTGetServicesResponse::decode_varint(uint32_t field_id, ProtoVar bool BluetoothGATTGetServicesResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { case 2: { - this->services.push_back(value.as_message()); + this->services.emplace_back(); + value.decode_to_message(this->services.back()); return true; } default: @@ -4396,7 +4410,7 @@ bool BluetoothGATTGetServicesResponse::decode_length(uint32_t field_id, ProtoLen void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); for (auto &it : this->services) { - buffer.encode_message(2, it, true); + buffer.encode_message(2, it, true); } } void BluetoothGATTGetServicesResponse::calculate_size(uint32_t &total_size) const { @@ -4826,11 +4840,11 @@ void BluetoothDeviceClearCacheResponse::calculate_size(uint32_t &total_size) con bool BluetoothScannerStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 1: { - this->state = value.as_enum(); + this->state = static_cast(value.as_uint32()); return true; } case 2: { - this->mode = value.as_enum(); + this->mode = static_cast(value.as_uint32()); return true; } default: @@ -4838,8 +4852,8 @@ bool BluetoothScannerStateResponse::decode_varint(uint32_t field_id, ProtoVarInt } } void BluetoothScannerStateResponse::encode(ProtoWriteBuffer buffer) const { - buffer.encode_enum(1, this->state); - buffer.encode_enum(2, this->mode); + buffer.encode_uint32(1, static_cast(this->state)); + buffer.encode_uint32(2, static_cast(this->mode)); } void BluetoothScannerStateResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_enum_field(total_size, 1, static_cast(this->state)); @@ -4848,7 +4862,7 @@ void BluetoothScannerStateResponse::calculate_size(uint32_t &total_size) const { bool BluetoothScannerSetModeRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 1: { - this->mode = value.as_enum(); + this->mode = static_cast(value.as_uint32()); return true; } default: @@ -4856,7 +4870,7 @@ bool BluetoothScannerSetModeRequest::decode_varint(uint32_t field_id, ProtoVarIn } } void BluetoothScannerSetModeRequest::encode(ProtoWriteBuffer buffer) const { - buffer.encode_enum(1, this->mode); + buffer.encode_uint32(1, static_cast(this->mode)); } void BluetoothScannerSetModeRequest::calculate_size(uint32_t &total_size) const { ProtoSize::add_enum_field(total_size, 1, static_cast(this->mode)); @@ -4940,7 +4954,7 @@ bool VoiceAssistantRequest::decode_length(uint32_t field_id, ProtoLengthDelimite return true; } case 4: { - this->audio_settings = value.as_message(); + value.decode_to_message(this->audio_settings); return true; } case 5: { @@ -4955,7 +4969,7 @@ void VoiceAssistantRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->start); buffer.encode_string(2, this->conversation_id); buffer.encode_uint32(3, this->flags); - buffer.encode_message(4, this->audio_settings); + buffer.encode_message(4, this->audio_settings); buffer.encode_string(5, this->wake_word_phrase); } void VoiceAssistantRequest::calculate_size(uint32_t &total_size) const { @@ -5012,7 +5026,7 @@ void VoiceAssistantEventData::calculate_size(uint32_t &total_size) const { bool VoiceAssistantEventResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 1: { - this->event_type = value.as_enum(); + this->event_type = static_cast(value.as_uint32()); return true; } default: @@ -5022,7 +5036,8 @@ bool VoiceAssistantEventResponse::decode_varint(uint32_t field_id, ProtoVarInt v bool VoiceAssistantEventResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { case 2: { - this->data.push_back(value.as_message()); + this->data.emplace_back(); + value.decode_to_message(this->data.back()); return true; } default: @@ -5030,9 +5045,9 @@ bool VoiceAssistantEventResponse::decode_length(uint32_t field_id, ProtoLengthDe } } void VoiceAssistantEventResponse::encode(ProtoWriteBuffer buffer) const { - buffer.encode_enum(1, this->event_type); + buffer.encode_uint32(1, static_cast(this->event_type)); for (auto &it : this->data) { - buffer.encode_message(2, it, true); + buffer.encode_message(2, it, true); } } void VoiceAssistantEventResponse::calculate_size(uint32_t &total_size) const { @@ -5070,7 +5085,7 @@ void VoiceAssistantAudio::calculate_size(uint32_t &total_size) const { bool VoiceAssistantTimerEventResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 1: { - this->event_type = value.as_enum(); + this->event_type = static_cast(value.as_uint32()); return true; } case 4: { @@ -5104,7 +5119,7 @@ bool VoiceAssistantTimerEventResponse::decode_length(uint32_t field_id, ProtoLen } } void VoiceAssistantTimerEventResponse::encode(ProtoWriteBuffer buffer) const { - buffer.encode_enum(1, this->event_type); + buffer.encode_uint32(1, static_cast(this->event_type)); buffer.encode_string(2, this->timer_id); buffer.encode_string(3, this->name); buffer.encode_uint32(4, this->total_seconds); @@ -5220,7 +5235,8 @@ bool VoiceAssistantConfigurationResponse::decode_varint(uint32_t field_id, Proto bool VoiceAssistantConfigurationResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { case 1: { - this->available_wake_words.push_back(value.as_message()); + this->available_wake_words.emplace_back(); + value.decode_to_message(this->available_wake_words.back()); return true; } case 2: { @@ -5233,7 +5249,7 @@ bool VoiceAssistantConfigurationResponse::decode_length(uint32_t field_id, Proto } void VoiceAssistantConfigurationResponse::encode(ProtoWriteBuffer buffer) const { for (auto &it : this->available_wake_words) { - buffer.encode_message(1, it, true); + buffer.encode_message(1, it, true); } for (auto &it : this->active_wake_words) { buffer.encode_string(2, it, true); @@ -5280,7 +5296,7 @@ bool ListEntitiesAlarmControlPanelResponse::decode_varint(uint32_t field_id, Pro return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 8: { @@ -5342,7 +5358,7 @@ void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer buffer) cons buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_uint32(8, this->supported_features); buffer.encode_bool(9, this->requires_code); buffer.encode_bool(10, this->requires_code_to_arm); @@ -5364,7 +5380,7 @@ void ListEntitiesAlarmControlPanelResponse::calculate_size(uint32_t &total_size) bool AlarmControlPanelStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 2: { - this->state = value.as_enum(); + this->state = static_cast(value.as_uint32()); return true; } case 3: { @@ -5387,7 +5403,7 @@ bool AlarmControlPanelStateResponse::decode_32bit(uint32_t field_id, Proto32Bit } void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); - buffer.encode_enum(2, this->state); + buffer.encode_uint32(2, static_cast(this->state)); buffer.encode_uint32(3, this->device_id); } void AlarmControlPanelStateResponse::calculate_size(uint32_t &total_size) const { @@ -5398,7 +5414,7 @@ void AlarmControlPanelStateResponse::calculate_size(uint32_t &total_size) const bool AlarmControlPanelCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 2: { - this->command = value.as_enum(); + this->command = static_cast(value.as_uint32()); return true; } case 4: { @@ -5431,7 +5447,7 @@ bool AlarmControlPanelCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit } void AlarmControlPanelCommandRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); - buffer.encode_enum(2, this->command); + buffer.encode_uint32(2, static_cast(this->command)); buffer.encode_string(3, this->code); buffer.encode_uint32(4, this->device_id); } @@ -5450,7 +5466,7 @@ bool ListEntitiesTextResponse::decode_varint(uint32_t field_id, ProtoVarInt valu return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 8: { @@ -5462,7 +5478,7 @@ bool ListEntitiesTextResponse::decode_varint(uint32_t field_id, ProtoVarInt valu return true; } case 11: { - this->mode = value.as_enum(); + this->mode = static_cast(value.as_uint32()); return true; } case 12: { @@ -5516,11 +5532,11 @@ void ListEntitiesTextResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_uint32(8, this->min_length); buffer.encode_uint32(9, this->max_length); buffer.encode_string(10, this->pattern); - buffer.encode_enum(11, this->mode); + buffer.encode_uint32(11, static_cast(this->mode)); buffer.encode_uint32(12, this->device_id); } void ListEntitiesTextResponse::calculate_size(uint32_t &total_size) const { @@ -5632,7 +5648,7 @@ bool ListEntitiesDateResponse::decode_varint(uint32_t field_id, ProtoVarInt valu return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 8: { @@ -5682,7 +5698,7 @@ void ListEntitiesDateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_uint32(8, this->device_id); } void ListEntitiesDateResponse::calculate_size(uint32_t &total_size) const { @@ -5802,7 +5818,7 @@ bool ListEntitiesTimeResponse::decode_varint(uint32_t field_id, ProtoVarInt valu return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 8: { @@ -5852,7 +5868,7 @@ void ListEntitiesTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_uint32(8, this->device_id); } void ListEntitiesTimeResponse::calculate_size(uint32_t &total_size) const { @@ -5972,7 +5988,7 @@ bool ListEntitiesEventResponse::decode_varint(uint32_t field_id, ProtoVarInt val return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 10: { @@ -6030,7 +6046,7 @@ void ListEntitiesEventResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); for (auto &it : this->event_types) { buffer.encode_string(9, it, true); @@ -6102,7 +6118,7 @@ bool ListEntitiesValveResponse::decode_varint(uint32_t field_id, ProtoVarInt val return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 9: { @@ -6168,7 +6184,7 @@ void ListEntitiesValveResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); buffer.encode_bool(9, this->assumed_state); buffer.encode_bool(10, this->supports_position); @@ -6192,7 +6208,7 @@ void ListEntitiesValveResponse::calculate_size(uint32_t &total_size) const { bool ValveStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 3: { - this->current_operation = value.as_enum(); + this->current_operation = static_cast(value.as_uint32()); return true; } case 4: { @@ -6220,7 +6236,7 @@ bool ValveStateResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { void ValveStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); buffer.encode_float(2, this->position); - buffer.encode_enum(3, this->current_operation); + buffer.encode_uint32(3, static_cast(this->current_operation)); buffer.encode_uint32(4, this->device_id); } void ValveStateResponse::calculate_size(uint32_t &total_size) const { @@ -6284,7 +6300,7 @@ bool ListEntitiesDateTimeResponse::decode_varint(uint32_t field_id, ProtoVarInt return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 8: { @@ -6334,7 +6350,7 @@ void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_uint32(8, this->device_id); } void ListEntitiesDateTimeResponse::calculate_size(uint32_t &total_size) const { @@ -6430,7 +6446,7 @@ bool ListEntitiesUpdateResponse::decode_varint(uint32_t field_id, ProtoVarInt va return true; } case 7: { - this->entity_category = value.as_enum(); + this->entity_category = static_cast(value.as_uint32()); return true; } case 9: { @@ -6484,7 +6500,7 @@ void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->unique_id); buffer.encode_string(5, this->icon); buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_enum(7, this->entity_category); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); buffer.encode_uint32(9, this->device_id); } @@ -6590,7 +6606,7 @@ void UpdateStateResponse::calculate_size(uint32_t &total_size) const { bool UpdateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 2: { - this->command = value.as_enum(); + this->command = static_cast(value.as_uint32()); return true; } case 3: { @@ -6613,7 +6629,7 @@ bool UpdateCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { } void UpdateCommandRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->key); - buffer.encode_enum(2, this->command); + buffer.encode_uint32(2, static_cast(this->command)); buffer.encode_uint32(3, this->device_id); } void UpdateCommandRequest::calculate_size(uint32_t &total_size) const { diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 2271ba7dbd..936e732af1 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -59,7 +59,6 @@ class ProtoVarInt { uint32_t as_uint32() const { return this->value_; } uint64_t as_uint64() const { return this->value_; } bool as_bool() const { return this->value_; } - template T as_enum() const { return static_cast(this->as_uint32()); } int32_t as_int32() const { // Not ZigZag encoded return static_cast(this->as_int64()); @@ -133,15 +132,24 @@ class ProtoVarInt { uint64_t value_; }; +// Forward declaration for decode_to_message and encode_to_writer +class ProtoMessage; + class ProtoLengthDelimited { public: explicit ProtoLengthDelimited(const uint8_t *value, size_t length) : value_(value), length_(length) {} std::string as_string() const { return std::string(reinterpret_cast(this->value_), this->length_); } - template C as_message() const { - auto msg = C(); - msg.decode(this->value_, this->length_); - return msg; - } + + /** + * Decode the length-delimited data into an existing ProtoMessage instance. + * + * This method allows decoding without templates, enabling use in contexts + * where the message type is not known at compile time. The ProtoMessage's + * decode() method will be called with the raw data and length. + * + * @param msg The ProtoMessage instance to decode into + */ + void decode_to_message(ProtoMessage &msg) const; protected: const uint8_t *const value_; @@ -263,9 +271,6 @@ class ProtoWriteBuffer { this->write((value >> 48) & 0xFF); this->write((value >> 56) & 0xFF); } - template void encode_enum(uint32_t field_id, T value, bool force = false) { - this->encode_uint32(field_id, static_cast(value), force); - } void encode_float(uint32_t field_id, float value, bool force = false) { if (value == 0.0f && !force) return; @@ -306,18 +311,7 @@ class ProtoWriteBuffer { } this->encode_uint64(field_id, uvalue, force); } - template void encode_message(uint32_t field_id, const C &value, bool force = false) { - this->encode_field_raw(field_id, 2); // type 2: Length-delimited message - size_t begin = this->buffer_->size(); - - value.encode(*this); - - const uint32_t nested_length = this->buffer_->size() - begin; - // add size varint - std::vector var; - ProtoVarInt(nested_length).encode(var); - this->buffer_->insert(this->buffer_->begin() + begin, var.begin(), var.end()); - } + void encode_message(uint32_t field_id, const ProtoMessage &value, bool force = false); std::vector *get_buffer() const { return buffer_; } protected: @@ -345,6 +339,25 @@ class ProtoMessage { virtual bool decode_64bit(uint32_t field_id, Proto64Bit value) { return false; } }; +// Implementation of encode_message - must be after ProtoMessage is defined +inline void ProtoWriteBuffer::encode_message(uint32_t field_id, const ProtoMessage &value, bool force) { + this->encode_field_raw(field_id, 2); // type 2: Length-delimited message + size_t begin = this->buffer_->size(); + + value.encode(*this); + + const uint32_t nested_length = this->buffer_->size() - begin; + // add size varint + std::vector var; + ProtoVarInt(nested_length).encode(var); + this->buffer_->insert(this->buffer_->begin() + begin, var.begin(), var.end()); +} + +// Implementation of decode_to_message - must be after ProtoMessage is defined +inline void ProtoLengthDelimited::decode_to_message(ProtoMessage &msg) const { + msg.decode(this->value_, this->length_); +} + template const char *proto_enum_to_string(T value); class ProtoService { diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 65c51535c4..1bb8789904 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -536,11 +536,26 @@ class MessageType(TypeInfo): @property def encode_func(self) -> str: - return f"encode_message<{self.cpp_type}>" + return "encode_message" @property def decode_length(self) -> str: - return f"value.as_message<{self.cpp_type}>()" + # Override to return None for message types because we can't use template-based + # decoding when the specific message type isn't known at compile time. + # Instead, we use the non-template decode_to_message() method which allows + # runtime polymorphism through virtual function calls. + return None + + @property + def decode_length_content(self) -> str: + # Custom decode that doesn't use templates + return dedent( + f"""\ + case {self.number}: {{ + value.decode_to_message(this->{self.field_name}); + return true; + }}""" + ) def dump(self, name: str) -> str: o = f"{name}.dump_to(out);" @@ -608,14 +623,18 @@ class EnumType(TypeInfo): @property def decode_varint(self) -> str: - return f"value.as_enum<{self.cpp_type}>()" + return f"static_cast<{self.cpp_type}>(value.as_uint32())" default_value = "" wire_type = WireType.VARINT # Uses wire type 0 @property def encode_func(self) -> str: - return f"encode_enum<{self.cpp_type}>" + return "encode_uint32" + + @property + def encode_content(self) -> str: + return f"buffer.{self.encode_func}({self.number}, static_cast(this->{self.field_name}));" def dump(self, name: str) -> str: o = f"out.append(proto_enum_to_string<{self.cpp_type}>({name}));" @@ -757,6 +776,16 @@ class RepeatedTypeInfo(TypeInfo): @property def decode_length_content(self) -> str: content = self._ti.decode_length + if content is None and isinstance(self._ti, MessageType): + # Special handling for non-template message decoding + return dedent( + f"""\ + case {self.number}: {{ + this->{self.field_name}.emplace_back(); + value.decode_to_message(this->{self.field_name}.back()); + return true; + }}""" + ) if content is None: return None return dedent( @@ -801,7 +830,10 @@ class RepeatedTypeInfo(TypeInfo): @property def encode_content(self) -> str: o = f"for (auto {'' if self._ti_is_bool else '&'}it : this->{self.field_name}) {{\n" - o += f" buffer.{self._ti.encode_func}({self.number}, it, true);\n" + if isinstance(self._ti, EnumType): + o += f" buffer.{self._ti.encode_func}({self.number}, static_cast(it), true);\n" + else: + o += f" buffer.{self._ti.encode_func}({self.number}, it, true);\n" o += "}" return o