From d111b84ca4433161834d127df5d4a56b98e46b8a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 14:03:47 -1000 Subject: [PATCH 01/24] Make ProtoSize an object --- esphome/components/api/api_pb2.cpp | 1118 +++++++++++++-------------- esphome/components/api/api_pb2.h | 166 ++-- esphome/components/api/proto.h | 191 +++-- script/api_protobuf/api_protobuf.py | 46 +- 4 files changed, 773 insertions(+), 748 deletions(-) diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index e2d1cfebd4..9e061d2cf0 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -36,11 +36,11 @@ void HelloResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(3, this->server_info_ref_); buffer.encode_string(4, this->name_ref_); } -void HelloResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint32_field(total_size, 1, this->api_version_major); - ProtoSize::add_uint32_field(total_size, 1, this->api_version_minor); - ProtoSize::add_string_field(total_size, 1, this->server_info_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void HelloResponse::calculate_size(ProtoSize &size) const { + size.add_uint32_field(1, this->api_version_major); + size.add_uint32_field(1, this->api_version_minor); + size.add_string_field(1, this->server_info_ref_.size()); + size.add_string_field(1, this->name_ref_.size()); } bool ConnectRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { @@ -53,17 +53,17 @@ bool ConnectRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value return true; } void ConnectResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->invalid_password); } -void ConnectResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_bool_field(total_size, 1, this->invalid_password); +} +void ConnectResponse::calculate_size(ProtoSize &size) const { size.add_bool_field(1, this->invalid_password); } } #ifdef USE_AREAS void AreaInfo::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(1, this->area_id); buffer.encode_string(2, this->name_ref_); } -void AreaInfo::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint32_field(total_size, 1, this->area_id); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void AreaInfo::calculate_size(ProtoSize &size) const { + size.add_uint32_field(1, this->area_id); + size.add_string_field(1, this->name_ref_.size()); } #endif #ifdef USE_DEVICES @@ -72,10 +72,10 @@ void DeviceInfo::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(2, this->name_ref_); buffer.encode_uint32(3, this->area_id); } -void DeviceInfo::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint32_field(total_size, 1, this->device_id); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); - ProtoSize::add_uint32_field(total_size, 1, this->area_id); +void DeviceInfo::calculate_size(ProtoSize &size) const { + size.add_uint32_field(1, this->device_id); + size.add_string_field(1, this->name_ref_.size()); + size.add_uint32_field(1, this->area_id); } #endif void DeviceInfoResponse::encode(ProtoWriteBuffer buffer) const { @@ -130,52 +130,52 @@ void DeviceInfoResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_message(22, this->area); #endif } -void DeviceInfoResponse::calculate_size(uint32_t &total_size) const { +void DeviceInfoResponse::calculate_size(ProtoSize &size) const { #ifdef USE_API_PASSWORD - ProtoSize::add_bool_field(total_size, 1, this->uses_password); + size.add_bool_field(1, this->uses_password); #endif - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->mac_address_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->esphome_version_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->compilation_time_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->model_ref_.size()); + size.add_string_field(1, this->name_ref_.size()); + size.add_string_field(1, this->mac_address_ref_.size()); + size.add_string_field(1, this->esphome_version_ref_.size()); + size.add_string_field(1, this->compilation_time_ref_.size()); + size.add_string_field(1, this->model_ref_.size()); #ifdef USE_DEEP_SLEEP - ProtoSize::add_bool_field(total_size, 1, this->has_deep_sleep); + size.add_bool_field(1, this->has_deep_sleep); #endif #ifdef ESPHOME_PROJECT_NAME - ProtoSize::add_string_field(total_size, 1, this->project_name_ref_.size()); + size.add_string_field(1, this->project_name_ref_.size()); #endif #ifdef ESPHOME_PROJECT_NAME - ProtoSize::add_string_field(total_size, 1, this->project_version_ref_.size()); + size.add_string_field(1, this->project_version_ref_.size()); #endif #ifdef USE_WEBSERVER - ProtoSize::add_uint32_field(total_size, 1, this->webserver_port); + size.add_uint32_field(1, this->webserver_port); #endif #ifdef USE_BLUETOOTH_PROXY - ProtoSize::add_uint32_field(total_size, 1, this->bluetooth_proxy_feature_flags); + size.add_uint32_field(1, this->bluetooth_proxy_feature_flags); #endif - ProtoSize::add_string_field(total_size, 1, this->manufacturer_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->friendly_name_ref_.size()); + size.add_string_field(1, this->manufacturer_ref_.size()); + size.add_string_field(1, this->friendly_name_ref_.size()); #ifdef USE_VOICE_ASSISTANT - ProtoSize::add_uint32_field(total_size, 2, this->voice_assistant_feature_flags); + size.add_uint32_field(2, this->voice_assistant_feature_flags); #endif #ifdef USE_AREAS - ProtoSize::add_string_field(total_size, 2, this->suggested_area_ref_.size()); + size.add_string_field(2, this->suggested_area_ref_.size()); #endif #ifdef USE_BLUETOOTH_PROXY - ProtoSize::add_string_field(total_size, 2, this->bluetooth_mac_address_ref_.size()); + size.add_string_field(2, this->bluetooth_mac_address_ref_.size()); #endif #ifdef USE_API_NOISE - ProtoSize::add_bool_field(total_size, 2, this->api_encryption_supported); + size.add_bool_field(2, this->api_encryption_supported); #endif #ifdef USE_DEVICES - ProtoSize::add_repeated_message(total_size, 2, this->devices); + size.add_repeated_message(2, this->devices); #endif #ifdef USE_AREAS - ProtoSize::add_repeated_message(total_size, 2, this->areas); + size.add_repeated_message(2, this->areas); #endif #ifdef USE_AREAS - ProtoSize::add_message_object(total_size, 2, this->area); + size.add_message_object(2, this->area); #endif } #ifdef USE_BINARY_SENSOR @@ -194,19 +194,19 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(10, this->device_id); #endif } -void ListEntitiesBinarySensorResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->device_class_ref_.size()); - ProtoSize::add_bool_field(total_size, 1, this->is_status_binary_sensor); - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); +void ListEntitiesBinarySensorResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); + size.add_string_field(1, this->device_class_ref_.size()); + size.add_bool_field(1, this->is_status_binary_sensor); + size.add_bool_field(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); + size.add_enum_field(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void BinarySensorStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -217,12 +217,12 @@ void BinarySensorStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); #endif } -void BinarySensorStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_bool_field(total_size, 1, this->state); - ProtoSize::add_bool_field(total_size, 1, this->missing_state); +void BinarySensorStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_bool_field(1, this->state); + size.add_bool_field(1, this->missing_state); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } #endif @@ -245,22 +245,22 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(13, this->device_id); #endif } -void ListEntitiesCoverResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); - ProtoSize::add_bool_field(total_size, 1, this->assumed_state); - ProtoSize::add_bool_field(total_size, 1, this->supports_position); - ProtoSize::add_bool_field(total_size, 1, this->supports_tilt); - ProtoSize::add_string_field(total_size, 1, this->device_class_ref_.size()); - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); +void ListEntitiesCoverResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); + size.add_bool_field(1, this->assumed_state); + size.add_bool_field(1, this->supports_position); + size.add_bool_field(1, this->supports_tilt); + size.add_string_field(1, this->device_class_ref_.size()); + size.add_bool_field(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_bool_field(total_size, 1, this->supports_stop); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_bool_field(1, this->supports_stop); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void CoverStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -272,13 +272,13 @@ void CoverStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(6, this->device_id); #endif } -void CoverStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_float_field(total_size, 1, this->position); - ProtoSize::add_float_field(total_size, 1, this->tilt); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->current_operation)); +void CoverStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_float_field(1, this->position); + size.add_float_field(1, this->tilt); + size.add_enum_field(1, static_cast(this->current_operation)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool CoverCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -340,26 +340,26 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(13, this->device_id); #endif } -void ListEntitiesFanResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); - ProtoSize::add_bool_field(total_size, 1, this->supports_oscillation); - ProtoSize::add_bool_field(total_size, 1, this->supports_speed); - ProtoSize::add_bool_field(total_size, 1, this->supports_direction); - ProtoSize::add_int32_field(total_size, 1, this->supported_speed_count); - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); +void ListEntitiesFanResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); + size.add_bool_field(1, this->supports_oscillation); + size.add_bool_field(1, this->supports_speed); + size.add_bool_field(1, this->supports_direction); + size.add_int32_field(1, this->supported_speed_count); + size.add_bool_field(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); + size.add_enum_field(1, static_cast(this->entity_category)); if (!this->supported_preset_modes.empty()) { for (const auto &it : this->supported_preset_modes) { - ProtoSize::add_string_field_repeated(total_size, 1, it); + size.add_string_field_repeated(1, it); } } #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void FanStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -373,15 +373,15 @@ void FanStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(8, this->device_id); #endif } -void FanStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_bool_field(total_size, 1, this->state); - ProtoSize::add_bool_field(total_size, 1, this->oscillating); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->direction)); - ProtoSize::add_int32_field(total_size, 1, this->speed_level); - ProtoSize::add_string_field(total_size, 1, this->preset_mode_ref_.size()); +void FanStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_bool_field(1, this->state); + size.add_bool_field(1, this->oscillating); + size.add_enum_field(1, static_cast(this->direction)); + size.add_int32_field(1, this->speed_level); + size.add_string_field(1, this->preset_mode_ref_.size()); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool FanCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -466,29 +466,29 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(16, this->device_id); #endif } -void ListEntitiesLightResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesLightResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); if (!this->supported_color_modes.empty()) { for (const auto &it : this->supported_color_modes) { - ProtoSize::add_enum_field_repeated(total_size, 1, static_cast(it)); + size.add_enum_field_repeated(1, static_cast(it)); } } - ProtoSize::add_float_field(total_size, 1, this->min_mireds); - ProtoSize::add_float_field(total_size, 1, this->max_mireds); + size.add_float_field(1, this->min_mireds); + size.add_float_field(1, this->max_mireds); if (!this->effects.empty()) { for (const auto &it : this->effects) { - ProtoSize::add_string_field_repeated(total_size, 1, it); + size.add_string_field_repeated(1, it); } } - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); + size.add_bool_field(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); + size.add_enum_field(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 2, this->device_id); + size.add_uint32_field(2, this->device_id); #endif } void LightStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -509,22 +509,22 @@ void LightStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(14, this->device_id); #endif } -void LightStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_bool_field(total_size, 1, this->state); - ProtoSize::add_float_field(total_size, 1, this->brightness); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->color_mode)); - ProtoSize::add_float_field(total_size, 1, this->color_brightness); - ProtoSize::add_float_field(total_size, 1, this->red); - ProtoSize::add_float_field(total_size, 1, this->green); - ProtoSize::add_float_field(total_size, 1, this->blue); - ProtoSize::add_float_field(total_size, 1, this->white); - ProtoSize::add_float_field(total_size, 1, this->color_temperature); - ProtoSize::add_float_field(total_size, 1, this->cold_white); - ProtoSize::add_float_field(total_size, 1, this->warm_white); - ProtoSize::add_string_field(total_size, 1, this->effect_ref_.size()); +void LightStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_bool_field(1, this->state); + size.add_float_field(1, this->brightness); + size.add_enum_field(1, static_cast(this->color_mode)); + size.add_float_field(1, this->color_brightness); + size.add_float_field(1, this->red); + size.add_float_field(1, this->green); + size.add_float_field(1, this->blue); + size.add_float_field(1, this->white); + size.add_float_field(1, this->color_temperature); + size.add_float_field(1, this->cold_white); + size.add_float_field(1, this->warm_white); + size.add_string_field(1, this->effect_ref_.size()); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool LightCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -654,22 +654,22 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(14, this->device_id); #endif } -void ListEntitiesSensorResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesSensorResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_string_field(total_size, 1, this->unit_of_measurement_ref_.size()); - ProtoSize::add_int32_field(total_size, 1, this->accuracy_decimals); - ProtoSize::add_bool_field(total_size, 1, this->force_update); - ProtoSize::add_string_field(total_size, 1, this->device_class_ref_.size()); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->state_class)); - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); + size.add_string_field(1, this->unit_of_measurement_ref_.size()); + size.add_int32_field(1, this->accuracy_decimals); + size.add_bool_field(1, this->force_update); + size.add_string_field(1, this->device_class_ref_.size()); + size.add_enum_field(1, static_cast(this->state_class)); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void SensorStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -680,12 +680,12 @@ void SensorStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); #endif } -void SensorStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_float_field(total_size, 1, this->state); - ProtoSize::add_bool_field(total_size, 1, this->missing_state); +void SensorStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_float_field(1, this->state); + size.add_bool_field(1, this->missing_state); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } #endif @@ -705,19 +705,19 @@ void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(10, this->device_id); #endif } -void ListEntitiesSwitchResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesSwitchResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->assumed_state); - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_string_field(total_size, 1, this->device_class_ref_.size()); + size.add_bool_field(1, this->assumed_state); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_string_field(1, this->device_class_ref_.size()); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void SwitchStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -727,11 +727,11 @@ void SwitchStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->device_id); #endif } -void SwitchStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_bool_field(total_size, 1, this->state); +void SwitchStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_bool_field(1, this->state); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool SwitchCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -775,18 +775,18 @@ void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(9, this->device_id); #endif } -void ListEntitiesTextSensorResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesTextSensorResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_string_field(total_size, 1, this->device_class_ref_.size()); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_string_field(1, this->device_class_ref_.size()); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void TextSensorStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -797,12 +797,12 @@ void TextSensorStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); #endif } -void TextSensorStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->state_ref_.size()); - ProtoSize::add_bool_field(total_size, 1, this->missing_state); +void TextSensorStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->state_ref_.size()); + size.add_bool_field(1, this->missing_state); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } #endif @@ -823,9 +823,9 @@ void SubscribeLogsResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(1, static_cast(this->level)); buffer.encode_bytes(3, this->message_ptr_, this->message_len_); } -void SubscribeLogsResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_enum_field(total_size, 1, static_cast(this->level)); - ProtoSize::add_bytes_field(total_size, 1, this->message_len_); +void SubscribeLogsResponse::calculate_size(ProtoSize &size) const { + size.add_enum_field(1, static_cast(this->level)); + size.add_bytes_field(1, this->message_len_); } #ifdef USE_API_NOISE bool NoiseEncryptionSetKeyRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) { @@ -839,17 +839,17 @@ bool NoiseEncryptionSetKeyRequest::decode_length(uint32_t field_id, ProtoLengthD return true; } void NoiseEncryptionSetKeyResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->success); } -void NoiseEncryptionSetKeyResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_bool_field(total_size, 1, this->success); +} +void NoiseEncryptionSetKeyResponse::calculate_size(ProtoSize &size) const { size.add_bool_field(1, this->success); } } #endif void HomeassistantServiceMap::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->key_ref_); buffer.encode_string(2, this->value_ref_); } -void HomeassistantServiceMap::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->key_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->value_ref_.size()); +void HomeassistantServiceMap::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->key_ref_.size()); + size.add_string_field(1, this->value_ref_.size()); } void HomeassistantServiceResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->service_ref_); @@ -864,12 +864,12 @@ void HomeassistantServiceResponse::encode(ProtoWriteBuffer buffer) const { } buffer.encode_bool(5, this->is_event); } -void HomeassistantServiceResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->service_ref_.size()); - ProtoSize::add_repeated_message(total_size, 1, this->data); - ProtoSize::add_repeated_message(total_size, 1, this->data_template); - ProtoSize::add_repeated_message(total_size, 1, this->variables); - ProtoSize::add_bool_field(total_size, 1, this->is_event); +void HomeassistantServiceResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->service_ref_.size()); + size.add_repeated_message(1, this->data); + size.add_repeated_message(1, this->data_template); + size.add_repeated_message(1, this->variables); + size.add_bool_field(1, this->is_event); } #ifdef USE_API_HOMEASSISTANT_STATES void SubscribeHomeAssistantStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -877,10 +877,10 @@ void SubscribeHomeAssistantStateResponse::encode(ProtoWriteBuffer buffer) const buffer.encode_string(2, this->attribute_ref_); buffer.encode_bool(3, this->once); } -void SubscribeHomeAssistantStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->entity_id_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->attribute_ref_.size()); - ProtoSize::add_bool_field(total_size, 1, this->once); +void SubscribeHomeAssistantStateResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->entity_id_ref_.size()); + size.add_string_field(1, this->attribute_ref_.size()); + size.add_bool_field(1, this->once); } bool HomeAssistantStateResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { @@ -910,17 +910,17 @@ bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { return true; } void GetTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->epoch_seconds); } -void GetTimeResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->epoch_seconds); +} +void GetTimeResponse::calculate_size(ProtoSize &size) const { size.add_fixed32_field(1, this->epoch_seconds); } } #ifdef USE_API_SERVICES void ListEntitiesServicesArgument::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->name_ref_); 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_ref_.size()); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->type)); +void ListEntitiesServicesArgument::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->name_ref_.size()); + size.add_enum_field(1, static_cast(this->type)); } void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->name_ref_); @@ -929,10 +929,10 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_message(3, it, true); } } -void ListEntitiesServicesResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_repeated_message(total_size, 1, this->args); +void ListEntitiesServicesResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->name_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_repeated_message(1, this->args); } bool ExecuteServiceArgument::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1018,17 +1018,17 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(8, this->device_id); #endif } -void ListEntitiesCameraResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); +void ListEntitiesCameraResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); + size.add_bool_field(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); + size.add_enum_field(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void CameraImageResponse::encode(ProtoWriteBuffer buffer) const { @@ -1039,12 +1039,12 @@ void CameraImageResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); #endif } -void CameraImageResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_bytes_field(total_size, 1, this->data_len_); - ProtoSize::add_bool_field(total_size, 1, this->done); +void CameraImageResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_bytes_field(1, this->data_len_); + size.add_bool_field(1, this->done); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool CameraImageRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1104,58 +1104,58 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(26, this->device_id); #endif } -void ListEntitiesClimateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); - ProtoSize::add_bool_field(total_size, 1, this->supports_current_temperature); - ProtoSize::add_bool_field(total_size, 1, this->supports_two_point_target_temperature); +void ListEntitiesClimateResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); + size.add_bool_field(1, this->supports_current_temperature); + size.add_bool_field(1, this->supports_two_point_target_temperature); if (!this->supported_modes.empty()) { for (const auto &it : this->supported_modes) { - ProtoSize::add_enum_field_repeated(total_size, 1, static_cast(it)); + size.add_enum_field_repeated(1, static_cast(it)); } } - ProtoSize::add_float_field(total_size, 1, this->visual_min_temperature); - ProtoSize::add_float_field(total_size, 1, this->visual_max_temperature); - ProtoSize::add_float_field(total_size, 1, this->visual_target_temperature_step); - ProtoSize::add_bool_field(total_size, 1, this->supports_action); + size.add_float_field(1, this->visual_min_temperature); + size.add_float_field(1, this->visual_max_temperature); + size.add_float_field(1, this->visual_target_temperature_step); + size.add_bool_field(1, this->supports_action); if (!this->supported_fan_modes.empty()) { for (const auto &it : this->supported_fan_modes) { - ProtoSize::add_enum_field_repeated(total_size, 1, static_cast(it)); + size.add_enum_field_repeated(1, static_cast(it)); } } if (!this->supported_swing_modes.empty()) { for (const auto &it : this->supported_swing_modes) { - ProtoSize::add_enum_field_repeated(total_size, 1, static_cast(it)); + size.add_enum_field_repeated(1, static_cast(it)); } } if (!this->supported_custom_fan_modes.empty()) { for (const auto &it : this->supported_custom_fan_modes) { - ProtoSize::add_string_field_repeated(total_size, 1, it); + size.add_string_field_repeated(1, it); } } if (!this->supported_presets.empty()) { for (const auto &it : this->supported_presets) { - ProtoSize::add_enum_field_repeated(total_size, 2, static_cast(it)); + size.add_enum_field_repeated(2, static_cast(it)); } } if (!this->supported_custom_presets.empty()) { for (const auto &it : this->supported_custom_presets) { - ProtoSize::add_string_field_repeated(total_size, 2, it); + size.add_string_field_repeated(2, it); } } - ProtoSize::add_bool_field(total_size, 2, this->disabled_by_default); + size.add_bool_field(2, this->disabled_by_default); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 2, this->icon_ref_.size()); + size.add_string_field(2, this->icon_ref_.size()); #endif - ProtoSize::add_enum_field(total_size, 2, static_cast(this->entity_category)); - ProtoSize::add_float_field(total_size, 2, this->visual_current_temperature_step); - ProtoSize::add_bool_field(total_size, 2, this->supports_current_humidity); - ProtoSize::add_bool_field(total_size, 2, this->supports_target_humidity); - ProtoSize::add_float_field(total_size, 2, this->visual_min_humidity); - ProtoSize::add_float_field(total_size, 2, this->visual_max_humidity); + size.add_enum_field(2, static_cast(this->entity_category)); + size.add_float_field(2, this->visual_current_temperature_step); + size.add_bool_field(2, this->supports_current_humidity); + size.add_bool_field(2, this->supports_target_humidity); + size.add_float_field(2, this->visual_min_humidity); + size.add_float_field(2, this->visual_max_humidity); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 2, this->device_id); + size.add_uint32_field(2, this->device_id); #endif } void ClimateStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1177,23 +1177,23 @@ void ClimateStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(16, this->device_id); #endif } -void ClimateStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->mode)); - ProtoSize::add_float_field(total_size, 1, this->current_temperature); - ProtoSize::add_float_field(total_size, 1, this->target_temperature); - ProtoSize::add_float_field(total_size, 1, this->target_temperature_low); - ProtoSize::add_float_field(total_size, 1, this->target_temperature_high); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->action)); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->fan_mode)); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->swing_mode)); - ProtoSize::add_string_field(total_size, 1, this->custom_fan_mode_ref_.size()); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->preset)); - ProtoSize::add_string_field(total_size, 1, this->custom_preset_ref_.size()); - ProtoSize::add_float_field(total_size, 1, this->current_humidity); - ProtoSize::add_float_field(total_size, 1, this->target_humidity); +void ClimateStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_enum_field(1, static_cast(this->mode)); + size.add_float_field(1, this->current_temperature); + size.add_float_field(1, this->target_temperature); + size.add_float_field(1, this->target_temperature_low); + size.add_float_field(1, this->target_temperature_high); + size.add_enum_field(1, static_cast(this->action)); + size.add_enum_field(1, static_cast(this->fan_mode)); + size.add_enum_field(1, static_cast(this->swing_mode)); + size.add_string_field(1, this->custom_fan_mode_ref_.size()); + size.add_enum_field(1, static_cast(this->preset)); + size.add_string_field(1, this->custom_preset_ref_.size()); + size.add_float_field(1, this->current_humidity); + size.add_float_field(1, this->target_humidity); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 2, this->device_id); + size.add_uint32_field(2, this->device_id); #endif } bool ClimateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1306,23 +1306,23 @@ void ListEntitiesNumberResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(14, this->device_id); #endif } -void ListEntitiesNumberResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesNumberResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_float_field(total_size, 1, this->min_value); - ProtoSize::add_float_field(total_size, 1, this->max_value); - ProtoSize::add_float_field(total_size, 1, this->step); - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_string_field(total_size, 1, this->unit_of_measurement_ref_.size()); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->mode)); - ProtoSize::add_string_field(total_size, 1, this->device_class_ref_.size()); + size.add_float_field(1, this->min_value); + size.add_float_field(1, this->max_value); + size.add_float_field(1, this->step); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_string_field(1, this->unit_of_measurement_ref_.size()); + size.add_enum_field(1, static_cast(this->mode)); + size.add_string_field(1, this->device_class_ref_.size()); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void NumberStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1333,12 +1333,12 @@ void NumberStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); #endif } -void NumberStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_float_field(total_size, 1, this->state); - ProtoSize::add_bool_field(total_size, 1, this->missing_state); +void NumberStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_float_field(1, this->state); + size.add_bool_field(1, this->missing_state); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool NumberCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1384,22 +1384,22 @@ void ListEntitiesSelectResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(9, this->device_id); #endif } -void ListEntitiesSelectResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesSelectResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif if (!this->options.empty()) { for (const auto &it : this->options) { - ProtoSize::add_string_field_repeated(total_size, 1, it); + size.add_string_field_repeated(1, it); } } - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void SelectStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1410,12 +1410,12 @@ void SelectStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); #endif } -void SelectStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->state_ref_.size()); - ProtoSize::add_bool_field(total_size, 1, this->missing_state); +void SelectStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->state_ref_.size()); + size.add_bool_field(1, this->missing_state); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool SelectCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1470,24 +1470,24 @@ void ListEntitiesSirenResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(11, this->device_id); #endif } -void ListEntitiesSirenResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesSirenResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); + size.add_bool_field(1, this->disabled_by_default); if (!this->tones.empty()) { for (const auto &it : this->tones) { - ProtoSize::add_string_field_repeated(total_size, 1, it); + size.add_string_field_repeated(1, it); } } - ProtoSize::add_bool_field(total_size, 1, this->supports_duration); - ProtoSize::add_bool_field(total_size, 1, this->supports_volume); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); + size.add_bool_field(1, this->supports_duration); + size.add_bool_field(1, this->supports_volume); + size.add_enum_field(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void SirenStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1497,11 +1497,11 @@ void SirenStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->device_id); #endif } -void SirenStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_bool_field(total_size, 1, this->state); +void SirenStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_bool_field(1, this->state); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool SirenCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1576,21 +1576,21 @@ void ListEntitiesLockResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(12, this->device_id); #endif } -void ListEntitiesLockResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesLockResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_bool_field(total_size, 1, this->assumed_state); - ProtoSize::add_bool_field(total_size, 1, this->supports_open); - ProtoSize::add_bool_field(total_size, 1, this->requires_code); - ProtoSize::add_string_field(total_size, 1, this->code_format_ref_.size()); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_bool_field(1, this->assumed_state); + size.add_bool_field(1, this->supports_open); + size.add_bool_field(1, this->requires_code); + size.add_string_field(1, this->code_format_ref_.size()); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void LockStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1600,11 +1600,11 @@ void LockStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->device_id); #endif } -void LockStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->state)); +void LockStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_enum_field(1, static_cast(this->state)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool LockCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1661,18 +1661,18 @@ void ListEntitiesButtonResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(9, this->device_id); #endif } -void ListEntitiesButtonResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesButtonResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_string_field(total_size, 1, this->device_class_ref_.size()); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_string_field(1, this->device_class_ref_.size()); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool ButtonCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1706,12 +1706,12 @@ void MediaPlayerSupportedFormat::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, static_cast(this->purpose)); buffer.encode_uint32(5, this->sample_bytes); } -void MediaPlayerSupportedFormat::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->format_ref_.size()); - ProtoSize::add_uint32_field(total_size, 1, this->sample_rate); - ProtoSize::add_uint32_field(total_size, 1, this->num_channels); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->purpose)); - ProtoSize::add_uint32_field(total_size, 1, this->sample_bytes); +void MediaPlayerSupportedFormat::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->format_ref_.size()); + size.add_uint32_field(1, this->sample_rate); + size.add_uint32_field(1, this->num_channels); + size.add_enum_field(1, static_cast(this->purpose)); + size.add_uint32_field(1, this->sample_bytes); } void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->object_id_ref_); @@ -1730,19 +1730,19 @@ void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(10, this->device_id); #endif } -void ListEntitiesMediaPlayerResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesMediaPlayerResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_bool_field(total_size, 1, this->supports_pause); - ProtoSize::add_repeated_message(total_size, 1, this->supported_formats); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_bool_field(1, this->supports_pause); + size.add_repeated_message(1, this->supported_formats); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void MediaPlayerStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1754,13 +1754,13 @@ void MediaPlayerStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(5, this->device_id); #endif } -void MediaPlayerStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->state)); - ProtoSize::add_float_field(total_size, 1, this->volume); - ProtoSize::add_bool_field(total_size, 1, this->muted); +void MediaPlayerStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_enum_field(1, static_cast(this->state)); + size.add_float_field(1, this->volume); + size.add_bool_field(1, this->muted); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool MediaPlayerCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1834,21 +1834,19 @@ void BluetoothLERawAdvertisement::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->address_type); buffer.encode_bytes(4, this->data, this->data_len); } -void BluetoothLERawAdvertisement::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); - ProtoSize::add_sint32_field(total_size, 1, this->rssi); - ProtoSize::add_uint32_field(total_size, 1, this->address_type); - if (this->data_len != 0) { - total_size += 1 + ProtoSize::varint(static_cast(this->data_len)) + this->data_len; - } +void BluetoothLERawAdvertisement::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); + size.add_sint32_field(1, this->rssi); + size.add_uint32_field(1, this->address_type); + size.add_bytes_field(1, this->data_len); } void BluetoothLERawAdvertisementsResponse::encode(ProtoWriteBuffer buffer) const { for (auto &it : this->advertisements) { buffer.encode_message(1, it, true); } } -void BluetoothLERawAdvertisementsResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_repeated_message(total_size, 1, this->advertisements); +void BluetoothLERawAdvertisementsResponse::calculate_size(ProtoSize &size) const { + size.add_repeated_message(1, this->advertisements); } bool BluetoothDeviceRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1875,11 +1873,11 @@ void BluetoothDeviceConnectionResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->mtu); buffer.encode_int32(4, this->error); } -void BluetoothDeviceConnectionResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); - ProtoSize::add_bool_field(total_size, 1, this->connected); - ProtoSize::add_uint32_field(total_size, 1, this->mtu); - ProtoSize::add_int32_field(total_size, 1, this->error); +void BluetoothDeviceConnectionResponse::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); + size.add_bool_field(1, this->connected); + size.add_uint32_field(1, this->mtu); + size.add_int32_field(1, this->error); } bool BluetoothGATTGetServicesRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1896,10 +1894,10 @@ void BluetoothGATTDescriptor::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->uuid[1], true); buffer.encode_uint32(2, this->handle); } -void BluetoothGATTDescriptor::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[0]); - ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[1]); - ProtoSize::add_uint32_field(total_size, 1, this->handle); +void BluetoothGATTDescriptor::calculate_size(ProtoSize &size) const { + size.add_uint64_field_repeated(1, this->uuid[0]); + size.add_uint64_field_repeated(1, this->uuid[1]); + size.add_uint32_field(1, this->handle); } void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->uuid[0], true); @@ -1910,12 +1908,12 @@ void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer buffer) const { buffer.encode_message(4, it, true); } } -void BluetoothGATTCharacteristic::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[0]); - ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[1]); - ProtoSize::add_uint32_field(total_size, 1, this->handle); - ProtoSize::add_uint32_field(total_size, 1, this->properties); - ProtoSize::add_repeated_message(total_size, 1, this->descriptors); +void BluetoothGATTCharacteristic::calculate_size(ProtoSize &size) const { + size.add_uint64_field_repeated(1, this->uuid[0]); + size.add_uint64_field_repeated(1, this->uuid[1]); + size.add_uint32_field(1, this->handle); + size.add_uint32_field(1, this->properties); + size.add_repeated_message(1, this->descriptors); } void BluetoothGATTService::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->uuid[0], true); @@ -1925,25 +1923,25 @@ void BluetoothGATTService::encode(ProtoWriteBuffer buffer) const { buffer.encode_message(3, it, true); } } -void BluetoothGATTService::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[0]); - ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[1]); - ProtoSize::add_uint32_field(total_size, 1, this->handle); - ProtoSize::add_repeated_message(total_size, 1, this->characteristics); +void BluetoothGATTService::calculate_size(ProtoSize &size) const { + size.add_uint64_field_repeated(1, this->uuid[0]); + size.add_uint64_field_repeated(1, this->uuid[1]); + size.add_uint32_field(1, this->handle); + size.add_repeated_message(1, this->characteristics); } void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); buffer.encode_message(2, this->services[0], true); } -void BluetoothGATTGetServicesResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); - ProtoSize::add_message_object_repeated(total_size, 1, this->services[0]); +void BluetoothGATTGetServicesResponse::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); + size.add_message_object_repeated(1, this->services[0]); } void BluetoothGATTGetServicesDoneResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); } -void BluetoothGATTGetServicesDoneResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); +void BluetoothGATTGetServicesDoneResponse::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); } bool BluetoothGATTReadRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1963,10 +1961,10 @@ void BluetoothGATTReadResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(2, this->handle); buffer.encode_bytes(3, this->data_ptr_, this->data_len_); } -void BluetoothGATTReadResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); - ProtoSize::add_uint32_field(total_size, 1, this->handle); - ProtoSize::add_bytes_field(total_size, 1, this->data_len_); +void BluetoothGATTReadResponse::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); + size.add_uint32_field(1, this->handle); + size.add_bytes_field(1, this->data_len_); } bool BluetoothGATTWriteRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2051,10 +2049,10 @@ void BluetoothGATTNotifyDataResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(2, this->handle); buffer.encode_bytes(3, this->data_ptr_, this->data_len_); } -void BluetoothGATTNotifyDataResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); - ProtoSize::add_uint32_field(total_size, 1, this->handle); - ProtoSize::add_bytes_field(total_size, 1, this->data_len_); +void BluetoothGATTNotifyDataResponse::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); + size.add_uint32_field(1, this->handle); + size.add_bytes_field(1, this->data_len_); } void BluetoothConnectionsFreeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(1, this->free); @@ -2063,12 +2061,12 @@ void BluetoothConnectionsFreeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(3, it, true); } } -void BluetoothConnectionsFreeResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint32_field(total_size, 1, this->free); - ProtoSize::add_uint32_field(total_size, 1, this->limit); +void BluetoothConnectionsFreeResponse::calculate_size(ProtoSize &size) const { + size.add_uint32_field(1, this->free); + size.add_uint32_field(1, this->limit); if (!this->allocated.empty()) { for (const auto &it : this->allocated) { - ProtoSize::add_uint64_field_repeated(total_size, 1, it); + size.add_uint64_field_repeated(1, it); } } } @@ -2077,64 +2075,64 @@ void BluetoothGATTErrorResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(2, this->handle); buffer.encode_int32(3, this->error); } -void BluetoothGATTErrorResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); - ProtoSize::add_uint32_field(total_size, 1, this->handle); - ProtoSize::add_int32_field(total_size, 1, this->error); +void BluetoothGATTErrorResponse::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); + size.add_uint32_field(1, this->handle); + size.add_int32_field(1, this->error); } void BluetoothGATTWriteResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); buffer.encode_uint32(2, this->handle); } -void BluetoothGATTWriteResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); - ProtoSize::add_uint32_field(total_size, 1, this->handle); +void BluetoothGATTWriteResponse::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); + size.add_uint32_field(1, this->handle); } void BluetoothGATTNotifyResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); buffer.encode_uint32(2, this->handle); } -void BluetoothGATTNotifyResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); - ProtoSize::add_uint32_field(total_size, 1, this->handle); +void BluetoothGATTNotifyResponse::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); + size.add_uint32_field(1, this->handle); } void BluetoothDevicePairingResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); buffer.encode_bool(2, this->paired); buffer.encode_int32(3, this->error); } -void BluetoothDevicePairingResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); - ProtoSize::add_bool_field(total_size, 1, this->paired); - ProtoSize::add_int32_field(total_size, 1, this->error); +void BluetoothDevicePairingResponse::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); + size.add_bool_field(1, this->paired); + size.add_int32_field(1, this->error); } void BluetoothDeviceUnpairingResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); buffer.encode_bool(2, this->success); buffer.encode_int32(3, this->error); } -void BluetoothDeviceUnpairingResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); - ProtoSize::add_bool_field(total_size, 1, this->success); - ProtoSize::add_int32_field(total_size, 1, this->error); +void BluetoothDeviceUnpairingResponse::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); + size.add_bool_field(1, this->success); + size.add_int32_field(1, this->error); } void BluetoothDeviceClearCacheResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); buffer.encode_bool(2, this->success); buffer.encode_int32(3, this->error); } -void BluetoothDeviceClearCacheResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint64_field(total_size, 1, this->address); - ProtoSize::add_bool_field(total_size, 1, this->success); - ProtoSize::add_int32_field(total_size, 1, this->error); +void BluetoothDeviceClearCacheResponse::calculate_size(ProtoSize &size) const { + size.add_uint64_field(1, this->address); + size.add_bool_field(1, this->success); + size.add_int32_field(1, this->error); } void BluetoothScannerStateResponse::encode(ProtoWriteBuffer buffer) const { 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)); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->mode)); +void BluetoothScannerStateResponse::calculate_size(ProtoSize &size) const { + size.add_enum_field(1, static_cast(this->state)); + size.add_enum_field(1, static_cast(this->mode)); } bool BluetoothScannerSetModeRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2166,10 +2164,10 @@ void VoiceAssistantAudioSettings::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(2, this->auto_gain); buffer.encode_float(3, this->volume_multiplier); } -void VoiceAssistantAudioSettings::calculate_size(uint32_t &total_size) const { - ProtoSize::add_uint32_field(total_size, 1, this->noise_suppression_level); - ProtoSize::add_uint32_field(total_size, 1, this->auto_gain); - ProtoSize::add_float_field(total_size, 1, this->volume_multiplier); +void VoiceAssistantAudioSettings::calculate_size(ProtoSize &size) const { + size.add_uint32_field(1, this->noise_suppression_level); + size.add_uint32_field(1, this->auto_gain); + size.add_float_field(1, this->volume_multiplier); } void VoiceAssistantRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->start); @@ -2178,12 +2176,12 @@ void VoiceAssistantRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_message(4, this->audio_settings); buffer.encode_string(5, this->wake_word_phrase_ref_); } -void VoiceAssistantRequest::calculate_size(uint32_t &total_size) const { - ProtoSize::add_bool_field(total_size, 1, this->start); - ProtoSize::add_string_field(total_size, 1, this->conversation_id_ref_.size()); - ProtoSize::add_uint32_field(total_size, 1, this->flags); - ProtoSize::add_message_object(total_size, 1, this->audio_settings); - ProtoSize::add_string_field(total_size, 1, this->wake_word_phrase_ref_.size()); +void VoiceAssistantRequest::calculate_size(ProtoSize &size) const { + size.add_bool_field(1, this->start); + size.add_string_field(1, this->conversation_id_ref_.size()); + size.add_uint32_field(1, this->flags); + size.add_message_object(1, this->audio_settings); + size.add_string_field(1, this->wake_word_phrase_ref_.size()); } bool VoiceAssistantResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2256,9 +2254,9 @@ void VoiceAssistantAudio::encode(ProtoWriteBuffer buffer) const { buffer.encode_bytes(1, this->data_ptr_, this->data_len_); buffer.encode_bool(2, this->end); } -void VoiceAssistantAudio::calculate_size(uint32_t &total_size) const { - ProtoSize::add_bytes_field(total_size, 1, this->data_len_); - ProtoSize::add_bool_field(total_size, 1, this->end); +void VoiceAssistantAudio::calculate_size(ProtoSize &size) const { + size.add_bytes_field(1, this->data_len_); + size.add_bool_field(1, this->end); } bool VoiceAssistantTimerEventResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2319,8 +2317,8 @@ bool VoiceAssistantAnnounceRequest::decode_length(uint32_t field_id, ProtoLength return true; } void VoiceAssistantAnnounceFinished::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->success); } -void VoiceAssistantAnnounceFinished::calculate_size(uint32_t &total_size) const { - ProtoSize::add_bool_field(total_size, 1, this->success); +} +void VoiceAssistantAnnounceFinished::calculate_size(ProtoSize &size) const { size.add_bool_field(1, this->success); } } void VoiceAssistantWakeWord::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->id_ref_); @@ -2329,12 +2327,12 @@ void VoiceAssistantWakeWord::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(3, it, true); } } -void VoiceAssistantWakeWord::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->id_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->wake_word_ref_.size()); +void VoiceAssistantWakeWord::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->id_ref_.size()); + size.add_string_field(1, this->wake_word_ref_.size()); if (!this->trained_languages.empty()) { for (const auto &it : this->trained_languages) { - ProtoSize::add_string_field_repeated(total_size, 1, it); + size.add_string_field_repeated(1, it); } } } @@ -2347,14 +2345,14 @@ void VoiceAssistantConfigurationResponse::encode(ProtoWriteBuffer buffer) const } buffer.encode_uint32(3, this->max_active_wake_words); } -void VoiceAssistantConfigurationResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_repeated_message(total_size, 1, this->available_wake_words); +void VoiceAssistantConfigurationResponse::calculate_size(ProtoSize &size) const { + size.add_repeated_message(1, this->available_wake_words); if (!this->active_wake_words.empty()) { for (const auto &it : this->active_wake_words) { - ProtoSize::add_string_field_repeated(total_size, 1, it); + size.add_string_field_repeated(1, it); } } - ProtoSize::add_uint32_field(total_size, 1, this->max_active_wake_words); + size.add_uint32_field(1, this->max_active_wake_words); } bool VoiceAssistantSetConfiguration::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { @@ -2384,20 +2382,20 @@ void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer buffer) cons buffer.encode_uint32(11, this->device_id); #endif } -void ListEntitiesAlarmControlPanelResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesAlarmControlPanelResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_uint32_field(total_size, 1, this->supported_features); - ProtoSize::add_bool_field(total_size, 1, this->requires_code); - ProtoSize::add_bool_field(total_size, 1, this->requires_code_to_arm); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_uint32_field(1, this->supported_features); + size.add_bool_field(1, this->requires_code); + size.add_bool_field(1, this->requires_code_to_arm); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2407,11 +2405,11 @@ void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->device_id); #endif } -void AlarmControlPanelStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->state)); +void AlarmControlPanelStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_enum_field(1, static_cast(this->state)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool AlarmControlPanelCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2468,21 +2466,21 @@ void ListEntitiesTextResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(12, this->device_id); #endif } -void ListEntitiesTextResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesTextResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_uint32_field(total_size, 1, this->min_length); - ProtoSize::add_uint32_field(total_size, 1, this->max_length); - ProtoSize::add_string_field(total_size, 1, this->pattern_ref_.size()); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->mode)); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_uint32_field(1, this->min_length); + size.add_uint32_field(1, this->max_length); + size.add_string_field(1, this->pattern_ref_.size()); + size.add_enum_field(1, static_cast(this->mode)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void TextStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2493,12 +2491,12 @@ void TextStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); #endif } -void TextStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->state_ref_.size()); - ProtoSize::add_bool_field(total_size, 1, this->missing_state); +void TextStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->state_ref_.size()); + size.add_bool_field(1, this->missing_state); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool TextCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2548,17 +2546,17 @@ void ListEntitiesDateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(8, this->device_id); #endif } -void ListEntitiesDateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesDateResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void DateStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2571,14 +2569,14 @@ void DateStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(6, this->device_id); #endif } -void DateStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_bool_field(total_size, 1, this->missing_state); - ProtoSize::add_uint32_field(total_size, 1, this->year); - ProtoSize::add_uint32_field(total_size, 1, this->month); - ProtoSize::add_uint32_field(total_size, 1, this->day); +void DateStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_bool_field(1, this->missing_state); + size.add_uint32_field(1, this->year); + size.add_uint32_field(1, this->month); + size.add_uint32_field(1, this->day); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool DateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2627,17 +2625,17 @@ void ListEntitiesTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(8, this->device_id); #endif } -void ListEntitiesTimeResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesTimeResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void TimeStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2650,14 +2648,14 @@ void TimeStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(6, this->device_id); #endif } -void TimeStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_bool_field(total_size, 1, this->missing_state); - ProtoSize::add_uint32_field(total_size, 1, this->hour); - ProtoSize::add_uint32_field(total_size, 1, this->minute); - ProtoSize::add_uint32_field(total_size, 1, this->second); +void TimeStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_bool_field(1, this->missing_state); + size.add_uint32_field(1, this->hour); + size.add_uint32_field(1, this->minute); + size.add_uint32_field(1, this->second); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool TimeCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2710,23 +2708,23 @@ void ListEntitiesEventResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(10, this->device_id); #endif } -void ListEntitiesEventResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesEventResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_string_field(total_size, 1, this->device_class_ref_.size()); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_string_field(1, this->device_class_ref_.size()); if (!this->event_types.empty()) { for (const auto &it : this->event_types) { - ProtoSize::add_string_field_repeated(total_size, 1, it); + size.add_string_field_repeated(1, it); } } #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void EventResponse::encode(ProtoWriteBuffer buffer) const { @@ -2736,11 +2734,11 @@ void EventResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->device_id); #endif } -void EventResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->event_type_ref_.size()); +void EventResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->event_type_ref_.size()); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } #endif @@ -2762,21 +2760,21 @@ void ListEntitiesValveResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(12, this->device_id); #endif } -void ListEntitiesValveResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesValveResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_string_field(total_size, 1, this->device_class_ref_.size()); - ProtoSize::add_bool_field(total_size, 1, this->assumed_state); - ProtoSize::add_bool_field(total_size, 1, this->supports_position); - ProtoSize::add_bool_field(total_size, 1, this->supports_stop); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_string_field(1, this->device_class_ref_.size()); + size.add_bool_field(1, this->assumed_state); + size.add_bool_field(1, this->supports_position); + size.add_bool_field(1, this->supports_stop); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void ValveStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2787,12 +2785,12 @@ void ValveStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); #endif } -void ValveStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_float_field(total_size, 1, this->position); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->current_operation)); +void ValveStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_float_field(1, this->position); + size.add_enum_field(1, static_cast(this->current_operation)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool ValveCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2841,17 +2839,17 @@ void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(8, this->device_id); #endif } -void ListEntitiesDateTimeResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesDateTimeResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void DateTimeStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2862,12 +2860,12 @@ void DateTimeStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); #endif } -void DateTimeStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_bool_field(total_size, 1, this->missing_state); - ProtoSize::add_fixed32_field(total_size, 1, this->epoch_seconds); +void DateTimeStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_bool_field(1, this->missing_state); + size.add_fixed32_field(1, this->epoch_seconds); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool DateTimeCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2911,18 +2909,18 @@ void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(9, this->device_id); #endif } -void ListEntitiesUpdateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_string_field(total_size, 1, this->object_id_ref_.size()); - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_string_field(total_size, 1, this->name_ref_.size()); +void ListEntitiesUpdateResponse::calculate_size(ProtoSize &size) const { + size.add_string_field(1, this->object_id_ref_.size()); + size.add_fixed32_field(1, this->key); + size.add_string_field(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - ProtoSize::add_string_field(total_size, 1, this->icon_ref_.size()); + size.add_string_field(1, this->icon_ref_.size()); #endif - ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); - ProtoSize::add_enum_field(total_size, 1, static_cast(this->entity_category)); - ProtoSize::add_string_field(total_size, 1, this->device_class_ref_.size()); + size.add_bool_field(1, this->disabled_by_default); + size.add_enum_field(1, static_cast(this->entity_category)); + size.add_string_field(1, this->device_class_ref_.size()); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } void UpdateStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2940,19 +2938,19 @@ void UpdateStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(11, this->device_id); #endif } -void UpdateStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed32_field(total_size, 1, this->key); - ProtoSize::add_bool_field(total_size, 1, this->missing_state); - ProtoSize::add_bool_field(total_size, 1, this->in_progress); - ProtoSize::add_bool_field(total_size, 1, this->has_progress); - ProtoSize::add_float_field(total_size, 1, this->progress); - ProtoSize::add_string_field(total_size, 1, this->current_version_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->latest_version_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->title_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->release_summary_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->release_url_ref_.size()); +void UpdateStateResponse::calculate_size(ProtoSize &size) const { + size.add_fixed32_field(1, this->key); + size.add_bool_field(1, this->missing_state); + size.add_bool_field(1, this->in_progress); + size.add_bool_field(1, this->has_progress); + size.add_float_field(1, this->progress); + size.add_string_field(1, this->current_version_ref_.size()); + size.add_string_field(1, this->latest_version_ref_.size()); + size.add_string_field(1, this->title_ref_.size()); + size.add_string_field(1, this->release_summary_ref_.size()); + size.add_string_field(1, this->release_url_ref_.size()); #ifdef USE_DEVICES - ProtoSize::add_uint32_field(total_size, 1, this->device_id); + size.add_uint32_field(1, this->device_id); #endif } bool UpdateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index b7d8945e8e..f204acdebe 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -340,7 +340,7 @@ class HelloResponse : public ProtoMessage { StringRef name_ref_{}; void set_name(const StringRef &ref) { this->name_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -371,7 +371,7 @@ class ConnectResponse : public ProtoMessage { #endif bool invalid_password{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -450,7 +450,7 @@ class AreaInfo : public ProtoMessage { StringRef name_ref_{}; void set_name(const StringRef &ref) { this->name_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -466,7 +466,7 @@ class DeviceInfo : public ProtoMessage { void set_name(const StringRef &ref) { this->name_ref_ = ref; } uint32_t area_id{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -539,7 +539,7 @@ class DeviceInfoResponse : public ProtoMessage { AreaInfo area{}; #endif void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -597,7 +597,7 @@ class ListEntitiesBinarySensorResponse : public InfoResponseProtoMessage { void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; } bool is_status_binary_sensor{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -614,7 +614,7 @@ class BinarySensorStateResponse : public StateResponseProtoMessage { bool state{false}; bool missing_state{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -637,7 +637,7 @@ class ListEntitiesCoverResponse : public InfoResponseProtoMessage { void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; } bool supports_stop{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -655,7 +655,7 @@ class CoverStateResponse : public StateResponseProtoMessage { float tilt{0.0f}; enums::CoverOperation current_operation{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -697,7 +697,7 @@ class ListEntitiesFanResponse : public InfoResponseProtoMessage { int32_t supported_speed_count{0}; std::vector supported_preset_modes{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -718,7 +718,7 @@ class FanStateResponse : public StateResponseProtoMessage { StringRef preset_mode_ref_{}; void set_preset_mode(const StringRef &ref) { this->preset_mode_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -765,7 +765,7 @@ class ListEntitiesLightResponse : public InfoResponseProtoMessage { float max_mireds{0.0f}; std::vector effects{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -793,7 +793,7 @@ class LightStateResponse : public StateResponseProtoMessage { StringRef effect_ref_{}; void set_effect(const StringRef &ref) { this->effect_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -859,7 +859,7 @@ class ListEntitiesSensorResponse : public InfoResponseProtoMessage { void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; } enums::SensorStateClass state_class{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -876,7 +876,7 @@ class SensorStateResponse : public StateResponseProtoMessage { float state{0.0f}; bool missing_state{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -896,7 +896,7 @@ class ListEntitiesSwitchResponse : public InfoResponseProtoMessage { StringRef device_class_ref_{}; void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -912,7 +912,7 @@ class SwitchStateResponse : public StateResponseProtoMessage { #endif bool state{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -947,7 +947,7 @@ class ListEntitiesTextSensorResponse : public InfoResponseProtoMessage { StringRef device_class_ref_{}; void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -965,7 +965,7 @@ class TextSensorStateResponse : public StateResponseProtoMessage { void set_state(const StringRef &ref) { this->state_ref_ = ref; } bool missing_state{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1004,7 +1004,7 @@ class SubscribeLogsResponse : public ProtoMessage { this->message_len_ = len; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1036,7 +1036,7 @@ class NoiseEncryptionSetKeyResponse : public ProtoMessage { #endif bool success{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1064,7 +1064,7 @@ class HomeassistantServiceMap : public ProtoMessage { StringRef value_ref_{}; void set_value(const StringRef &ref) { this->value_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1085,7 +1085,7 @@ class HomeassistantServiceResponse : public ProtoMessage { std::vector variables{}; bool is_event{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1119,7 +1119,7 @@ class SubscribeHomeAssistantStateResponse : public ProtoMessage { void set_attribute(const StringRef &ref) { this->attribute_ref_ = ref; } bool once{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1166,7 +1166,7 @@ class GetTimeResponse : public ProtoDecodableMessage { #endif uint32_t epoch_seconds{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1181,7 +1181,7 @@ class ListEntitiesServicesArgument : public ProtoMessage { void set_name(const StringRef &ref) { this->name_ref_ = ref; } enums::ServiceArgType type{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1200,7 +1200,7 @@ class ListEntitiesServicesResponse : public ProtoMessage { uint32_t key{0}; std::vector args{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1254,7 +1254,7 @@ class ListEntitiesCameraResponse : public InfoResponseProtoMessage { const char *message_name() const override { return "list_entities_camera_response"; } #endif void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1276,7 +1276,7 @@ class CameraImageResponse : public StateResponseProtoMessage { } bool done{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1326,7 +1326,7 @@ class ListEntitiesClimateResponse : public InfoResponseProtoMessage { float visual_min_humidity{0.0f}; float visual_max_humidity{0.0f}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1356,7 +1356,7 @@ class ClimateStateResponse : public StateResponseProtoMessage { float current_humidity{0.0f}; float target_humidity{0.0f}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1417,7 +1417,7 @@ class ListEntitiesNumberResponse : public InfoResponseProtoMessage { StringRef device_class_ref_{}; void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1434,7 +1434,7 @@ class NumberStateResponse : public StateResponseProtoMessage { float state{0.0f}; bool missing_state{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1468,7 +1468,7 @@ class ListEntitiesSelectResponse : public InfoResponseProtoMessage { #endif std::vector options{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1486,7 +1486,7 @@ class SelectStateResponse : public StateResponseProtoMessage { void set_state(const StringRef &ref) { this->state_ref_ = ref; } bool missing_state{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1523,7 +1523,7 @@ class ListEntitiesSirenResponse : public InfoResponseProtoMessage { bool supports_duration{false}; bool supports_volume{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1539,7 +1539,7 @@ class SirenStateResponse : public StateResponseProtoMessage { #endif bool state{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1585,7 +1585,7 @@ class ListEntitiesLockResponse : public InfoResponseProtoMessage { StringRef code_format_ref_{}; void set_code_format(const StringRef &ref) { this->code_format_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1601,7 +1601,7 @@ class LockStateResponse : public StateResponseProtoMessage { #endif enums::LockState state{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1639,7 +1639,7 @@ class ListEntitiesButtonResponse : public InfoResponseProtoMessage { StringRef device_class_ref_{}; void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1672,7 +1672,7 @@ class MediaPlayerSupportedFormat : public ProtoMessage { enums::MediaPlayerFormatPurpose purpose{}; uint32_t sample_bytes{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1689,7 +1689,7 @@ class ListEntitiesMediaPlayerResponse : public InfoResponseProtoMessage { bool supports_pause{false}; std::vector supported_formats{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1707,7 +1707,7 @@ class MediaPlayerStateResponse : public StateResponseProtoMessage { float volume{0.0f}; bool muted{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1763,7 +1763,7 @@ class BluetoothLERawAdvertisement : public ProtoMessage { uint8_t data[62]{}; uint8_t data_len{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1779,7 +1779,7 @@ class BluetoothLERawAdvertisementsResponse : public ProtoMessage { #endif std::vector advertisements{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1816,7 +1816,7 @@ class BluetoothDeviceConnectionResponse : public ProtoMessage { uint32_t mtu{0}; int32_t error{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1843,7 +1843,7 @@ class BluetoothGATTDescriptor : public ProtoMessage { std::array uuid{}; uint32_t handle{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1857,7 +1857,7 @@ class BluetoothGATTCharacteristic : public ProtoMessage { uint32_t properties{0}; std::vector descriptors{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1870,7 +1870,7 @@ class BluetoothGATTService : public ProtoMessage { uint32_t handle{0}; std::vector characteristics{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1887,7 +1887,7 @@ class BluetoothGATTGetServicesResponse : public ProtoMessage { uint64_t address{0}; std::array services{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1903,7 +1903,7 @@ class BluetoothGATTGetServicesDoneResponse : public ProtoMessage { #endif uint64_t address{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -1942,7 +1942,7 @@ class BluetoothGATTReadResponse : public ProtoMessage { this->data_len_ = len; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2035,7 +2035,7 @@ class BluetoothGATTNotifyDataResponse : public ProtoMessage { this->data_len_ = len; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2066,7 +2066,7 @@ class BluetoothConnectionsFreeResponse : public ProtoMessage { uint32_t limit{0}; std::vector allocated{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2084,7 +2084,7 @@ class BluetoothGATTErrorResponse : public ProtoMessage { uint32_t handle{0}; int32_t error{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2101,7 +2101,7 @@ class BluetoothGATTWriteResponse : public ProtoMessage { uint64_t address{0}; uint32_t handle{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2118,7 +2118,7 @@ class BluetoothGATTNotifyResponse : public ProtoMessage { uint64_t address{0}; uint32_t handle{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2136,7 +2136,7 @@ class BluetoothDevicePairingResponse : public ProtoMessage { bool paired{false}; int32_t error{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2154,7 +2154,7 @@ class BluetoothDeviceUnpairingResponse : public ProtoMessage { bool success{false}; int32_t error{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2185,7 +2185,7 @@ class BluetoothDeviceClearCacheResponse : public ProtoMessage { bool success{false}; int32_t error{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2202,7 +2202,7 @@ class BluetoothScannerStateResponse : public ProtoMessage { enums::BluetoothScannerState state{}; enums::BluetoothScannerMode mode{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2248,7 +2248,7 @@ class VoiceAssistantAudioSettings : public ProtoMessage { uint32_t auto_gain{0}; float volume_multiplier{0.0f}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2270,7 +2270,7 @@ class VoiceAssistantRequest : public ProtoMessage { StringRef wake_word_phrase_ref_{}; void set_wake_word_phrase(const StringRef &ref) { this->wake_word_phrase_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2337,7 +2337,7 @@ class VoiceAssistantAudio : public ProtoDecodableMessage { } bool end{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2395,7 +2395,7 @@ class VoiceAssistantAnnounceFinished : public ProtoMessage { #endif bool success{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2410,7 +2410,7 @@ class VoiceAssistantWakeWord : public ProtoMessage { void set_wake_word(const StringRef &ref) { this->wake_word_ref_ = ref; } std::vector trained_languages{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2441,7 +2441,7 @@ class VoiceAssistantConfigurationResponse : public ProtoMessage { std::vector active_wake_words{}; uint32_t max_active_wake_words{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2476,7 +2476,7 @@ class ListEntitiesAlarmControlPanelResponse : public InfoResponseProtoMessage { bool requires_code{false}; bool requires_code_to_arm{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2492,7 +2492,7 @@ class AlarmControlPanelStateResponse : public StateResponseProtoMessage { #endif enums::AlarmControlPanelState state{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2532,7 +2532,7 @@ class ListEntitiesTextResponse : public InfoResponseProtoMessage { void set_pattern(const StringRef &ref) { this->pattern_ref_ = ref; } enums::TextMode mode{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2550,7 +2550,7 @@ class TextStateResponse : public StateResponseProtoMessage { void set_state(const StringRef &ref) { this->state_ref_ = ref; } bool missing_state{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2584,7 +2584,7 @@ class ListEntitiesDateResponse : public InfoResponseProtoMessage { const char *message_name() const override { return "list_entities_date_response"; } #endif void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2603,7 +2603,7 @@ class DateStateResponse : public StateResponseProtoMessage { uint32_t month{0}; uint32_t day{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2638,7 +2638,7 @@ class ListEntitiesTimeResponse : public InfoResponseProtoMessage { const char *message_name() const override { return "list_entities_time_response"; } #endif void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2657,7 +2657,7 @@ class TimeStateResponse : public StateResponseProtoMessage { uint32_t minute{0}; uint32_t second{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2695,7 +2695,7 @@ class ListEntitiesEventResponse : public InfoResponseProtoMessage { void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; } std::vector event_types{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2712,7 +2712,7 @@ class EventResponse : public StateResponseProtoMessage { StringRef event_type_ref_{}; void set_event_type(const StringRef &ref) { this->event_type_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2734,7 +2734,7 @@ class ListEntitiesValveResponse : public InfoResponseProtoMessage { bool supports_position{false}; bool supports_stop{false}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2751,7 +2751,7 @@ class ValveStateResponse : public StateResponseProtoMessage { float position{0.0f}; enums::ValveOperation current_operation{}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2786,7 +2786,7 @@ class ListEntitiesDateTimeResponse : public InfoResponseProtoMessage { const char *message_name() const override { return "list_entities_date_time_response"; } #endif void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2803,7 +2803,7 @@ class DateTimeStateResponse : public StateResponseProtoMessage { bool missing_state{false}; uint32_t epoch_seconds{0}; void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2838,7 +2838,7 @@ class ListEntitiesUpdateResponse : public InfoResponseProtoMessage { StringRef device_class_ref_{}; void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif @@ -2867,7 +2867,7 @@ class UpdateStateResponse : public StateResponseProtoMessage { StringRef release_url_ref_{}; void set_release_url(const StringRef &ref) { this->release_url_ref_ = ref; } void encode(ProtoWriteBuffer buffer) const override; - void calculate_size(uint32_t &total_size) const override; + void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; #endif diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 771eaa98d1..74a918751c 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -340,7 +340,7 @@ class ProtoMessage { // Default implementation for messages with no fields virtual void encode(ProtoWriteBuffer buffer) const {} // Default implementation for messages with no fields - virtual void calculate_size(uint32_t &total_size) const {} + virtual void calculate_size(ProtoSize &size) const {} #ifdef HAS_PROTO_MESSAGE_DUMP std::string dump() const; virtual void dump_to(std::string &out) const = 0; @@ -361,24 +361,32 @@ class ProtoDecodableMessage : public ProtoMessage { }; class ProtoSize { + private: + uint32_t total_size_ = 0; + public: /** * @brief ProtoSize class for Protocol Buffer serialization size calculation * - * This class provides static methods to calculate the exact byte counts needed - * for encoding various Protocol Buffer field types. All methods are designed to be - * efficient for the common case where many fields have default values. + * This class provides methods to calculate the exact byte counts needed + * for encoding various Protocol Buffer field types. The class now uses an + * object-based approach to reduce parameter passing overhead while keeping + * varint calculation methods static for external use. * * Implements Protocol Buffer encoding size calculation according to: * https://protobuf.dev/programming-guides/encoding/ * * Key features: + * - Object-based approach reduces flash usage by eliminating parameter passing * - Early-return optimization for zero/default values - * - Direct total_size updates to avoid unnecessary additions + * - Static varint methods for external callers * - Specialized handling for different field types according to protobuf spec - * - Templated helpers for repeated fields and messages */ + ProtoSize() = default; + + uint32_t get_size() const { return total_size_; } + /** * @brief Calculates the size in bytes needed to encode a uint32_t value as a varint * @@ -479,9 +487,7 @@ class ProtoSize { * @brief Common parameters for all add_*_field methods * * All add_*_field methods follow these common patterns: - * - * @param total_size Reference to the total message size to update - * @param field_id_size Pre-calculated size of the field ID in bytes + * * @param field_id_size Pre-calculated size of the field ID in bytes * @param value The value to calculate size for (type varies) * @param force Whether to calculate size even if the value is default/zero/empty * @@ -494,85 +500,85 @@ class ProtoSize { /** * @brief Calculates and adds the size of an int32 field to the total message size */ - static inline void add_int32_field(uint32_t &total_size, uint32_t field_id_size, int32_t value) { + inline void add_int32_field(uint32_t field_id_size, int32_t value) { // Skip calculation if value is zero if (value == 0) { - return; // No need to update total_size + return; // No need to update total_size_ } // Calculate and directly add to total_size if (value < 0) { // Negative values are encoded as 10-byte varints in protobuf - total_size += field_id_size + 10; + total_size_ += field_id_size + 10; } else { // For non-negative values, use the standard varint size - total_size += field_id_size + varint(static_cast(value)); + total_size_ += field_id_size + varint(static_cast(value)); } } /** * @brief Calculates and adds the size of an int32 field to the total message size (repeated field version) */ - static inline void add_int32_field_repeated(uint32_t &total_size, uint32_t field_id_size, int32_t value) { + inline void add_int32_field_repeated(uint32_t field_id_size, int32_t value) { // Always calculate size for repeated fields if (value < 0) { // Negative values are encoded as 10-byte varints in protobuf - total_size += field_id_size + 10; + total_size_ += field_id_size + 10; } else { // For non-negative values, use the standard varint size - total_size += field_id_size + varint(static_cast(value)); + total_size_ += field_id_size + varint(static_cast(value)); } } /** * @brief Calculates and adds the size of a uint32 field to the total message size */ - static inline void add_uint32_field(uint32_t &total_size, uint32_t field_id_size, uint32_t value) { + inline void add_uint32_field(uint32_t field_id_size, uint32_t value) { // Skip calculation if value is zero if (value == 0) { - return; // No need to update total_size + return; // No need to update total_size_ } // Calculate and directly add to total_size - total_size += field_id_size + varint(value); + total_size_ += field_id_size + varint(value); } /** * @brief Calculates and adds the size of a uint32 field to the total message size (repeated field version) */ - static inline void add_uint32_field_repeated(uint32_t &total_size, uint32_t field_id_size, uint32_t value) { + inline void add_uint32_field_repeated(uint32_t field_id_size, uint32_t value) { // Always calculate size for repeated fields - total_size += field_id_size + varint(value); + total_size_ += field_id_size + varint(value); } /** * @brief Calculates and adds the size of a boolean field to the total message size */ - static inline void add_bool_field(uint32_t &total_size, uint32_t field_id_size, bool value) { + inline void add_bool_field(uint32_t field_id_size, bool value) { // Skip calculation if value is false if (!value) { - return; // No need to update total_size + return; // No need to update total_size_ } // Boolean fields always use 1 byte when true - total_size += field_id_size + 1; + total_size_ += field_id_size + 1; } /** * @brief Calculates and adds the size of a boolean field to the total message size (repeated field version) */ - static inline void add_bool_field_repeated(uint32_t &total_size, uint32_t field_id_size, bool value) { + inline void add_bool_field_repeated(uint32_t field_id_size, bool value) { // Always calculate size for repeated fields // Boolean fields always use 1 byte - total_size += field_id_size + 1; + total_size_ += field_id_size + 1; } /** * @brief Calculates and adds the size of a float field to the total message size */ - static inline void add_float_field(uint32_t &total_size, uint32_t field_id_size, float value) { + inline void add_float_field(uint32_t field_id_size, float value) { if (value != 0.0f) { - total_size += field_id_size + 4; + total_size_ += field_id_size + 4; } } @@ -582,9 +588,9 @@ class ProtoSize { /** * @brief Calculates and adds the size of a fixed32 field to the total message size */ - static inline void add_fixed32_field(uint32_t &total_size, uint32_t field_id_size, uint32_t value) { + inline void add_fixed32_field(uint32_t field_id_size, uint32_t value) { if (value != 0) { - total_size += field_id_size + 4; + total_size_ += field_id_size + 4; } } @@ -594,9 +600,9 @@ class ProtoSize { /** * @brief Calculates and adds the size of a sfixed32 field to the total message size */ - static inline void add_sfixed32_field(uint32_t &total_size, uint32_t field_id_size, int32_t value) { + inline void add_sfixed32_field(uint32_t field_id_size, int32_t value) { if (value != 0) { - total_size += field_id_size + 4; + total_size_ += field_id_size + 4; } } @@ -608,14 +614,14 @@ class ProtoSize { * * Enum fields are encoded as uint32 varints. */ - static inline void add_enum_field(uint32_t &total_size, uint32_t field_id_size, uint32_t value) { + inline void add_enum_field(uint32_t field_id_size, uint32_t value) { // Skip calculation if value is zero if (value == 0) { - return; // No need to update total_size + return; // No need to update total_size_ } // Enums are encoded as uint32 - total_size += field_id_size + varint(value); + total_size_ += field_id_size + varint(value); } /** @@ -623,10 +629,10 @@ class ProtoSize { * * Enum fields are encoded as uint32 varints. */ - static inline void add_enum_field_repeated(uint32_t &total_size, uint32_t field_id_size, uint32_t value) { + inline void add_enum_field_repeated(uint32_t field_id_size, uint32_t value) { // Always calculate size for repeated fields // Enums are encoded as uint32 - total_size += field_id_size + varint(value); + total_size_ += field_id_size + varint(value); } /** @@ -634,15 +640,15 @@ class ProtoSize { * * Sint32 fields use ZigZag encoding, which is more efficient for negative values. */ - static inline void add_sint32_field(uint32_t &total_size, uint32_t field_id_size, int32_t value) { + inline void add_sint32_field(uint32_t field_id_size, int32_t value) { // Skip calculation if value is zero if (value == 0) { - return; // No need to update total_size + return; // No need to update total_size_ } // ZigZag encoding for sint32: (n << 1) ^ (n >> 31) uint32_t zigzag = (static_cast(value) << 1) ^ (static_cast(value >> 31)); - total_size += field_id_size + varint(zigzag); + total_size_ += field_id_size + varint(zigzag); } /** @@ -650,53 +656,53 @@ class ProtoSize { * * Sint32 fields use ZigZag encoding, which is more efficient for negative values. */ - static inline void add_sint32_field_repeated(uint32_t &total_size, uint32_t field_id_size, int32_t value) { + inline void add_sint32_field_repeated(uint32_t field_id_size, int32_t value) { // Always calculate size for repeated fields // ZigZag encoding for sint32: (n << 1) ^ (n >> 31) uint32_t zigzag = (static_cast(value) << 1) ^ (static_cast(value >> 31)); - total_size += field_id_size + varint(zigzag); + total_size_ += field_id_size + varint(zigzag); } /** * @brief Calculates and adds the size of an int64 field to the total message size */ - static inline void add_int64_field(uint32_t &total_size, uint32_t field_id_size, int64_t value) { + inline void add_int64_field(uint32_t field_id_size, int64_t value) { // Skip calculation if value is zero if (value == 0) { - return; // No need to update total_size + return; // No need to update total_size_ } // Calculate and directly add to total_size - total_size += field_id_size + varint(value); + total_size_ += field_id_size + varint(value); } /** * @brief Calculates and adds the size of an int64 field to the total message size (repeated field version) */ - static inline void add_int64_field_repeated(uint32_t &total_size, uint32_t field_id_size, int64_t value) { + inline void add_int64_field_repeated(uint32_t field_id_size, int64_t value) { // Always calculate size for repeated fields - total_size += field_id_size + varint(value); + total_size_ += field_id_size + varint(value); } /** * @brief Calculates and adds the size of a uint64 field to the total message size */ - static inline void add_uint64_field(uint32_t &total_size, uint32_t field_id_size, uint64_t value) { + inline void add_uint64_field(uint32_t field_id_size, uint64_t value) { // Skip calculation if value is zero if (value == 0) { - return; // No need to update total_size + return; // No need to update total_size_ } // Calculate and directly add to total_size - total_size += field_id_size + varint(value); + total_size_ += field_id_size + varint(value); } /** * @brief Calculates and adds the size of a uint64 field to the total message size (repeated field version) */ - static inline void add_uint64_field_repeated(uint32_t &total_size, uint32_t field_id_size, uint64_t value) { + inline void add_uint64_field_repeated(uint32_t field_id_size, uint64_t value) { // Always calculate size for repeated fields - total_size += field_id_size + varint(value); + total_size_ += field_id_size + varint(value); } // NOTE: sint64 support functions (add_sint64_field, add_sint64_field_repeated) removed @@ -705,38 +711,57 @@ class ProtoSize { /** * @brief Calculates and adds the size of a string field using length */ - static inline void add_string_field(uint32_t &total_size, uint32_t field_id_size, size_t len) { + inline void add_string_field(uint32_t field_id_size, size_t len) { // Skip calculation if string is empty if (len == 0) { - return; // No need to update total_size + return; // No need to update total_size_ } // Field ID + length varint + string bytes - total_size += field_id_size + varint(static_cast(len)) + static_cast(len); + total_size_ += field_id_size + varint(static_cast(len)) + static_cast(len); } /** * @brief Calculates and adds the size of a string/bytes field to the total message size (repeated field version) */ - static inline void add_string_field_repeated(uint32_t &total_size, uint32_t field_id_size, const std::string &str) { + inline void add_string_field_repeated(uint32_t field_id_size, const std::string &str) { // Always calculate size for repeated fields const uint32_t str_size = static_cast(str.size()); - total_size += field_id_size + varint(str_size) + str_size; + total_size_ += field_id_size + varint(str_size) + str_size; } /** * @brief Calculates and adds the size of a bytes field to the total message size */ - static inline void add_bytes_field(uint32_t &total_size, uint32_t field_id_size, size_t len) { + inline void add_bytes_field(uint32_t field_id_size, size_t len) { // Skip calculation if bytes is empty if (len == 0) { - return; // No need to update total_size + return; // No need to update total_size_ } // Field ID + length varint + data bytes - total_size += field_id_size + varint(static_cast(len)) + static_cast(len); + total_size_ += field_id_size + varint(static_cast(len)) + static_cast(len); } + /** + * @brief Calculates and adds the size of a bytes field to the total message size (repeated field version) + */ + inline void add_bytes_field_repeated(uint32_t field_id_size, size_t len) { + // Always calculate size for repeated fields + // Field ID + length varint + data bytes + total_size_ += field_id_size + varint(static_cast(len)) + static_cast(len); + } + + /** + * @brief Adds a pre-calculated size directly to the total + * + * This is used when we can calculate the total size by multiplying the number + * of elements by the bytes per element (for repeated fixed-size types like float, fixed32, etc.) + * + * @param size The pre-calculated total size to add + */ + inline void add_precalculated_size(uint32_t size) { total_size_ += size; } + /** * @brief Calculates and adds the size of a nested message field to the total message size * @@ -745,15 +770,15 @@ class ProtoSize { * * @param nested_size The pre-calculated size of the nested message */ - static inline void add_message_field(uint32_t &total_size, uint32_t field_id_size, uint32_t nested_size) { + inline void add_message_field(uint32_t field_id_size, uint32_t nested_size) { // Skip calculation if nested message is empty if (nested_size == 0) { - return; // No need to update total_size + return; // No need to update total_size_ } // Calculate and directly add to total_size // Field ID + length varint + nested message content - total_size += field_id_size + varint(nested_size) + nested_size; + total_size_ += field_id_size + varint(nested_size) + nested_size; } /** @@ -761,10 +786,10 @@ class ProtoSize { * * @param nested_size The pre-calculated size of the nested message */ - static inline void add_message_field_repeated(uint32_t &total_size, uint32_t field_id_size, uint32_t nested_size) { + inline void add_message_field_repeated(uint32_t field_id_size, uint32_t nested_size) { // Always calculate size for repeated fields // Field ID + length varint + nested message content - total_size += field_id_size + varint(nested_size) + nested_size; + total_size_ += field_id_size + varint(nested_size) + nested_size; } /** @@ -776,12 +801,14 @@ class ProtoSize { * * @param message The nested message object */ - static inline void add_message_object(uint32_t &total_size, uint32_t field_id_size, const ProtoMessage &message) { - uint32_t nested_size = 0; - message.calculate_size(nested_size); + inline void add_message_object(uint32_t field_id_size, const ProtoMessage &message) { + // Calculate nested message size by creating a temporary ProtoSize + ProtoSize nested_calc; + message.calculate_size(nested_calc); + uint32_t nested_size = nested_calc.get_size(); // Use the base implementation with the calculated nested_size - add_message_field(total_size, field_id_size, nested_size); + add_message_field(field_id_size, nested_size); } /** @@ -789,13 +816,14 @@ class ProtoSize { * * @param message The nested message object */ - static inline void add_message_object_repeated(uint32_t &total_size, uint32_t field_id_size, - const ProtoMessage &message) { - uint32_t nested_size = 0; - message.calculate_size(nested_size); + inline void add_message_object_repeated(uint32_t field_id_size, const ProtoMessage &message) { + // Calculate nested message size by creating a temporary ProtoSize + ProtoSize nested_calc; + message.calculate_size(nested_calc); + uint32_t nested_size = nested_calc.get_size(); // Use the base implementation with the calculated nested_size - add_message_field_repeated(total_size, field_id_size, nested_size); + add_message_field_repeated(field_id_size, nested_size); } /** @@ -808,8 +836,7 @@ class ProtoSize { * @param messages Vector of message objects */ template - static inline void add_repeated_message(uint32_t &total_size, uint32_t field_id_size, - const std::vector &messages) { + inline void add_repeated_message(uint32_t field_id_size, const std::vector &messages) { // Skip if the vector is empty if (messages.empty()) { return; @@ -817,7 +844,7 @@ class ProtoSize { // Use the repeated field version for all messages for (const auto &message : messages) { - add_message_object_repeated(total_size, field_id_size, message); + add_message_object_repeated(field_id_size, message); } } }; @@ -827,8 +854,9 @@ inline void ProtoWriteBuffer::encode_message(uint32_t field_id, const ProtoMessa this->encode_field_raw(field_id, 2); // type 2: Length-delimited message // Calculate the message size first - uint32_t msg_length_bytes = 0; - value.calculate_size(msg_length_bytes); + ProtoSize msg_size; + value.calculate_size(msg_size); + uint32_t msg_length_bytes = msg_size.get_size(); // Calculate how many bytes the length varint needs uint32_t varint_length_bytes = ProtoSize::varint(msg_length_bytes); @@ -877,8 +905,9 @@ class ProtoService { // Optimized method that pre-allocates buffer based on message size bool send_message_(const ProtoMessage &msg, uint8_t message_type) { - uint32_t msg_size = 0; - msg.calculate_size(msg_size); + ProtoSize size; + msg.calculate_size(size); + uint32_t msg_size = size.get_size(); // Create a pre-sized buffer auto buffer = this->create_buffer(msg_size); diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 92c85d2366..2783d3bad7 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -281,7 +281,7 @@ class TypeInfo(ABC): field_id_size = self.calculate_field_id_size() method = f"{base_method}_repeated" if force else base_method value = value_expr if value_expr else name - return f"ProtoSize::{method}(total_size, {field_id_size}, {value});" + return f"size.{method}({field_id_size}, {value});" @abstractmethod def get_size_calculation(self, name: str, force: bool = False) -> str: @@ -389,7 +389,7 @@ class DoubleType(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"ProtoSize::add_double_field(total_size, {field_id_size}, {name});" + return f"size.add_double_field({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8 @@ -413,7 +413,7 @@ class FloatType(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"ProtoSize::add_float_field(total_size, {field_id_size}, {name});" + return f"size.add_float_field({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -497,7 +497,7 @@ class Fixed64Type(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"ProtoSize::add_fixed64_field(total_size, {field_id_size}, {name});" + return f"size.add_fixed64_field({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8 @@ -521,7 +521,7 @@ class Fixed32Type(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"ProtoSize::add_fixed32_field(total_size, {field_id_size}, {name});" + return f"size.add_fixed32_field({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -631,11 +631,11 @@ class StringType(TypeInfo): if name == "it": # For repeated fields, we need to use add_string_field_repeated which includes field ID field_id_size = self.calculate_field_id_size() - return f"ProtoSize::add_string_field_repeated(total_size, {field_id_size}, it);" + return f"size.add_string_field_repeated({field_id_size}, it);" # For messages that need encoding, use the StringRef size field_id_size = self.calculate_field_id_size() - return f"ProtoSize::add_string_field(total_size, {field_id_size}, this->{self.field_name}_ref_.size());" + return f"size.add_string_field({field_id_size}, this->{self.field_name}_ref_.size());" def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 8 # field ID + 8 bytes typical string @@ -770,7 +770,7 @@ class BytesType(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return f"ProtoSize::add_bytes_field(total_size, {self.calculate_field_id_size()}, this->{self.field_name}_len_);" + return f"size.add_bytes_field({self.calculate_field_id_size()}, this->{self.field_name}_len_);" def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 8 # field ID + 8 bytes typical bytes @@ -845,15 +845,11 @@ class FixedArrayBytesType(TypeInfo): field_id_size = self.calculate_field_id_size() if force: - # For repeated fields, always calculate size - return f"total_size += {field_id_size} + ProtoSize::varint(static_cast({length_field})) + {length_field};" + # For repeated fields, always calculate size (no zero check) + return f"size.add_bytes_field_repeated({field_id_size}, {length_field});" else: - # For non-repeated fields, skip if length is 0 (matching encode_string behavior) - return ( - f"if ({length_field} != 0) {{\n" - f" total_size += {field_id_size} + ProtoSize::varint(static_cast({length_field})) + {length_field};\n" - f"}}" - ) + # For non-repeated fields, add_bytes_field already checks for zero + return f"size.add_bytes_field({field_id_size}, {length_field});" def get_estimated_size(self) -> int: # Estimate based on typical BLE advertisement size @@ -939,7 +935,7 @@ class SFixed32Type(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"ProtoSize::add_sfixed32_field(total_size, {field_id_size}, {name});" + return f"size.add_sfixed32_field({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -963,7 +959,7 @@ class SFixed64Type(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"ProtoSize::add_sfixed64_field(total_size, {field_id_size}, {name});" + return f"size.add_sfixed64_field({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8 @@ -1240,7 +1236,7 @@ class RepeatedTypeInfo(TypeInfo): if isinstance(self._ti, MessageType): # For repeated messages, use the dedicated helper that handles iteration internally field_id_size = self._ti.calculate_field_id_size() - o = f"ProtoSize::add_repeated_message(total_size, {field_id_size}, {name});" + o = f"size.add_repeated_message({field_id_size}, {name});" return o # For other repeated types, use the underlying type's size calculation with force=True @@ -1253,7 +1249,9 @@ class RepeatedTypeInfo(TypeInfo): field_id_size = self._ti.calculate_field_id_size() # Pre-calculate the total bytes per element bytes_per_element = field_id_size + num_bytes - o += f" total_size += {name}.size() * {bytes_per_element};\n" + o += ( + f" size.add_precalculated_size({name}.size() * {bytes_per_element});\n" + ) else: # Other types need the actual value o += f" for (const auto {'' if self._ti_is_bool else '&'}it : {name}) {{\n" @@ -1685,7 +1683,7 @@ def build_message_type( if needs_encode and encode: o = f"void {desc.name}::encode(ProtoWriteBuffer buffer) const {{" if len(encode) == 1 and len(encode[0]) + len(o) + 3 < 120: - o += f" {encode[0]} " + o += f" {encode[0]} }}\n" else: o += "\n" o += indent("\n".join(encode)) + "\n" @@ -1697,17 +1695,17 @@ def build_message_type( # Add calculate_size method only if this message needs encoding and has fields if needs_encode and size_calc: - o = f"void {desc.name}::calculate_size(uint32_t &total_size) const {{" + o = f"void {desc.name}::calculate_size(ProtoSize &size) const {{" # For a single field, just inline it for simplicity if len(size_calc) == 1 and len(size_calc[0]) + len(o) + 3 < 120: - o += f" {size_calc[0]} " + o += f" {size_calc[0]} }}\n" else: # For multiple fields o += "\n" o += indent("\n".join(size_calc)) + "\n" o += "}\n" cpp += o - prot = "void calculate_size(uint32_t &total_size) const override;" + prot = "void calculate_size(ProtoSize &size) const override;" public_content.append(prot) # If no fields to calculate size for or message doesn't need encoding, the default implementation in ProtoMessage will be used From 1032e5c220630e4cf4c4bf36fb5ccc5d3abc158c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 14:08:43 -1000 Subject: [PATCH 02/24] Make ProtoSize an object --- esphome/components/api/api_pb2.cpp | 8 -------- esphome/components/api/proto.h | 3 +++ script/api_protobuf/api_protobuf.py | 4 ++-- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 9e061d2cf0..5526fc442f 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -53,9 +53,7 @@ bool ConnectRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value return true; } void ConnectResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->invalid_password); } -} void ConnectResponse::calculate_size(ProtoSize &size) const { size.add_bool_field(1, this->invalid_password); } -} #ifdef USE_AREAS void AreaInfo::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(1, this->area_id); @@ -839,9 +837,7 @@ bool NoiseEncryptionSetKeyRequest::decode_length(uint32_t field_id, ProtoLengthD return true; } void NoiseEncryptionSetKeyResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->success); } -} void NoiseEncryptionSetKeyResponse::calculate_size(ProtoSize &size) const { size.add_bool_field(1, this->success); } -} #endif void HomeassistantServiceMap::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->key_ref_); @@ -910,9 +906,7 @@ bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { return true; } void GetTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->epoch_seconds); } -} void GetTimeResponse::calculate_size(ProtoSize &size) const { size.add_fixed32_field(1, this->epoch_seconds); } -} #ifdef USE_API_SERVICES void ListEntitiesServicesArgument::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->name_ref_); @@ -2317,9 +2311,7 @@ bool VoiceAssistantAnnounceRequest::decode_length(uint32_t field_id, ProtoLength return true; } void VoiceAssistantAnnounceFinished::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->success); } -} void VoiceAssistantAnnounceFinished::calculate_size(ProtoSize &size) const { size.add_bool_field(1, this->success); } -} void VoiceAssistantWakeWord::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->id_ref_); buffer.encode_string(2, this->wake_word_ref_); diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 74a918751c..eb3d5c7302 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -334,6 +334,9 @@ class ProtoWriteBuffer { std::vector *buffer_; }; +// Forward declaration +class ProtoSize; + class ProtoMessage { public: virtual ~ProtoMessage() = default; diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 2783d3bad7..56f74aeaad 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -1687,7 +1687,7 @@ def build_message_type( else: o += "\n" o += indent("\n".join(encode)) + "\n" - o += "}\n" + o += "}\n" cpp += o prot = "void encode(ProtoWriteBuffer buffer) const override;" public_content.append(prot) @@ -1703,7 +1703,7 @@ def build_message_type( # For multiple fields o += "\n" o += indent("\n".join(size_calc)) + "\n" - o += "}\n" + o += "}\n" cpp += o prot = "void calculate_size(ProtoSize &size) const override;" public_content.append(prot) From 33ec5e195f89636092bd5cbeb15cd84e2c6b6013 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 14:09:17 -1000 Subject: [PATCH 03/24] Make ProtoSize an object --- esphome/components/api/api_connection.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 0d3b99cd41..e21fcb694a 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -290,8 +290,9 @@ uint16_t APIConnection::encode_message_to_buffer(ProtoMessage &msg, uint8_t mess #endif // Calculate size - uint32_t calculated_size = 0; - msg.calculate_size(calculated_size); + ProtoSize size_calc; + msg.calculate_size(size_calc); + uint32_t calculated_size = size_calc.get_size(); // Cache frame sizes to avoid repeated virtual calls const uint8_t header_padding = conn->helper_->frame_header_padding(); From 09a30689e9412b94ce896661b6f581f02fa9fe5e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 14:19:35 -1000 Subject: [PATCH 04/24] preen --- esphome/components/api/api_pb2.cpp | 924 ++++++++++++++-------------- esphome/components/api/proto.h | 42 +- script/api_protobuf/api_protobuf.py | 46 +- 3 files changed, 505 insertions(+), 507 deletions(-) diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 5526fc442f..ab6c5513de 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -37,10 +37,10 @@ void HelloResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(4, this->name_ref_); } void HelloResponse::calculate_size(ProtoSize &size) const { - size.add_uint32_field(1, this->api_version_major); - size.add_uint32_field(1, this->api_version_minor); - size.add_string_field(1, this->server_info_ref_.size()); - size.add_string_field(1, this->name_ref_.size()); + size.add_uint32(1, this->api_version_major); + size.add_uint32(1, this->api_version_minor); + size.add_string(1, this->server_info_ref_.size()); + size.add_string(1, this->name_ref_.size()); } bool ConnectRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { @@ -53,15 +53,15 @@ bool ConnectRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value return true; } void ConnectResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->invalid_password); } -void ConnectResponse::calculate_size(ProtoSize &size) const { size.add_bool_field(1, this->invalid_password); } +void ConnectResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->invalid_password); } #ifdef USE_AREAS void AreaInfo::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(1, this->area_id); buffer.encode_string(2, this->name_ref_); } void AreaInfo::calculate_size(ProtoSize &size) const { - size.add_uint32_field(1, this->area_id); - size.add_string_field(1, this->name_ref_.size()); + size.add_uint32(1, this->area_id); + size.add_string(1, this->name_ref_.size()); } #endif #ifdef USE_DEVICES @@ -71,9 +71,9 @@ void DeviceInfo::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->area_id); } void DeviceInfo::calculate_size(ProtoSize &size) const { - size.add_uint32_field(1, this->device_id); - size.add_string_field(1, this->name_ref_.size()); - size.add_uint32_field(1, this->area_id); + size.add_uint32(1, this->device_id); + size.add_string(1, this->name_ref_.size()); + size.add_uint32(1, this->area_id); } #endif void DeviceInfoResponse::encode(ProtoWriteBuffer buffer) const { @@ -130,41 +130,41 @@ void DeviceInfoResponse::encode(ProtoWriteBuffer buffer) const { } void DeviceInfoResponse::calculate_size(ProtoSize &size) const { #ifdef USE_API_PASSWORD - size.add_bool_field(1, this->uses_password); + size.add_bool(1, this->uses_password); #endif - size.add_string_field(1, this->name_ref_.size()); - size.add_string_field(1, this->mac_address_ref_.size()); - size.add_string_field(1, this->esphome_version_ref_.size()); - size.add_string_field(1, this->compilation_time_ref_.size()); - size.add_string_field(1, this->model_ref_.size()); + size.add_string(1, this->name_ref_.size()); + size.add_string(1, this->mac_address_ref_.size()); + size.add_string(1, this->esphome_version_ref_.size()); + size.add_string(1, this->compilation_time_ref_.size()); + size.add_string(1, this->model_ref_.size()); #ifdef USE_DEEP_SLEEP - size.add_bool_field(1, this->has_deep_sleep); + size.add_bool(1, this->has_deep_sleep); #endif #ifdef ESPHOME_PROJECT_NAME - size.add_string_field(1, this->project_name_ref_.size()); + size.add_string(1, this->project_name_ref_.size()); #endif #ifdef ESPHOME_PROJECT_NAME - size.add_string_field(1, this->project_version_ref_.size()); + size.add_string(1, this->project_version_ref_.size()); #endif #ifdef USE_WEBSERVER - size.add_uint32_field(1, this->webserver_port); + size.add_uint32(1, this->webserver_port); #endif #ifdef USE_BLUETOOTH_PROXY - size.add_uint32_field(1, this->bluetooth_proxy_feature_flags); + size.add_uint32(1, this->bluetooth_proxy_feature_flags); #endif - size.add_string_field(1, this->manufacturer_ref_.size()); - size.add_string_field(1, this->friendly_name_ref_.size()); + size.add_string(1, this->manufacturer_ref_.size()); + size.add_string(1, this->friendly_name_ref_.size()); #ifdef USE_VOICE_ASSISTANT - size.add_uint32_field(2, this->voice_assistant_feature_flags); + size.add_uint32(2, this->voice_assistant_feature_flags); #endif #ifdef USE_AREAS - size.add_string_field(2, this->suggested_area_ref_.size()); + size.add_string(2, this->suggested_area_ref_.size()); #endif #ifdef USE_BLUETOOTH_PROXY - size.add_string_field(2, this->bluetooth_mac_address_ref_.size()); + size.add_string(2, this->bluetooth_mac_address_ref_.size()); #endif #ifdef USE_API_NOISE - size.add_bool_field(2, this->api_encryption_supported); + size.add_bool(2, this->api_encryption_supported); #endif #ifdef USE_DEVICES size.add_repeated_message(2, this->devices); @@ -193,18 +193,18 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesBinarySensorResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); - size.add_string_field(1, this->device_class_ref_.size()); - size.add_bool_field(1, this->is_status_binary_sensor); - size.add_bool_field(1, this->disabled_by_default); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); + size.add_string(1, this->device_class_ref_.size()); + size.add_bool(1, this->is_status_binary_sensor); + size.add_bool(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_enum_field(1, static_cast(this->entity_category)); + size.add_enum(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void BinarySensorStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -216,11 +216,11 @@ void BinarySensorStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void BinarySensorStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_bool_field(1, this->state); - size.add_bool_field(1, this->missing_state); + size.add_fixed32(1, this->key); + size.add_bool(1, this->state); + size.add_bool(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } #endif @@ -244,21 +244,21 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesCoverResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); - size.add_bool_field(1, this->assumed_state); - size.add_bool_field(1, this->supports_position); - size.add_bool_field(1, this->supports_tilt); - size.add_string_field(1, this->device_class_ref_.size()); - size.add_bool_field(1, this->disabled_by_default); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); + size.add_bool(1, this->assumed_state); + size.add_bool(1, this->supports_position); + size.add_bool(1, this->supports_tilt); + size.add_string(1, this->device_class_ref_.size()); + size.add_bool(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_bool_field(1, this->supports_stop); + size.add_enum(1, static_cast(this->entity_category)); + size.add_bool(1, this->supports_stop); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void CoverStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -271,12 +271,12 @@ void CoverStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void CoverStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_float_field(1, this->position); - size.add_float_field(1, this->tilt); - size.add_enum_field(1, static_cast(this->current_operation)); + size.add_fixed32(1, this->key); + size.add_float(1, this->position); + size.add_float(1, this->tilt); + size.add_enum(1, static_cast(this->current_operation)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool CoverCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -339,25 +339,25 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesFanResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); - size.add_bool_field(1, this->supports_oscillation); - size.add_bool_field(1, this->supports_speed); - size.add_bool_field(1, this->supports_direction); - size.add_int32_field(1, this->supported_speed_count); - size.add_bool_field(1, this->disabled_by_default); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); + size.add_bool(1, this->supports_oscillation); + size.add_bool(1, this->supports_speed); + size.add_bool(1, this->supports_direction); + size.add_int32(1, this->supported_speed_count); + size.add_bool(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_enum_field(1, static_cast(this->entity_category)); + size.add_enum(1, static_cast(this->entity_category)); if (!this->supported_preset_modes.empty()) { for (const auto &it : this->supported_preset_modes) { - size.add_string_field_repeated(1, it); + size.add_string_repeated(1, it); } } #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void FanStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -372,14 +372,14 @@ void FanStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void FanStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_bool_field(1, this->state); - size.add_bool_field(1, this->oscillating); - size.add_enum_field(1, static_cast(this->direction)); - size.add_int32_field(1, this->speed_level); - size.add_string_field(1, this->preset_mode_ref_.size()); + size.add_fixed32(1, this->key); + size.add_bool(1, this->state); + size.add_bool(1, this->oscillating); + size.add_enum(1, static_cast(this->direction)); + size.add_int32(1, this->speed_level); + size.add_string(1, this->preset_mode_ref_.size()); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool FanCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -465,28 +465,28 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesLightResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); if (!this->supported_color_modes.empty()) { for (const auto &it : this->supported_color_modes) { - size.add_enum_field_repeated(1, static_cast(it)); + size.add_enum_repeated(1, static_cast(it)); } } - size.add_float_field(1, this->min_mireds); - size.add_float_field(1, this->max_mireds); + size.add_float(1, this->min_mireds); + size.add_float(1, this->max_mireds); if (!this->effects.empty()) { for (const auto &it : this->effects) { - size.add_string_field_repeated(1, it); + size.add_string_repeated(1, it); } } - size.add_bool_field(1, this->disabled_by_default); + size.add_bool(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_enum_field(1, static_cast(this->entity_category)); + size.add_enum(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32_field(2, this->device_id); + size.add_uint32(2, this->device_id); #endif } void LightStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -508,21 +508,21 @@ void LightStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void LightStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_bool_field(1, this->state); - size.add_float_field(1, this->brightness); - size.add_enum_field(1, static_cast(this->color_mode)); - size.add_float_field(1, this->color_brightness); - size.add_float_field(1, this->red); - size.add_float_field(1, this->green); - size.add_float_field(1, this->blue); - size.add_float_field(1, this->white); - size.add_float_field(1, this->color_temperature); - size.add_float_field(1, this->cold_white); - size.add_float_field(1, this->warm_white); - size.add_string_field(1, this->effect_ref_.size()); + size.add_fixed32(1, this->key); + size.add_bool(1, this->state); + size.add_float(1, this->brightness); + size.add_enum(1, static_cast(this->color_mode)); + size.add_float(1, this->color_brightness); + size.add_float(1, this->red); + size.add_float(1, this->green); + size.add_float(1, this->blue); + size.add_float(1, this->white); + size.add_float(1, this->color_temperature); + size.add_float(1, this->cold_white); + size.add_float(1, this->warm_white); + size.add_string(1, this->effect_ref_.size()); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool LightCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -653,21 +653,21 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesSensorResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_string_field(1, this->unit_of_measurement_ref_.size()); - size.add_int32_field(1, this->accuracy_decimals); - size.add_bool_field(1, this->force_update); - size.add_string_field(1, this->device_class_ref_.size()); - size.add_enum_field(1, static_cast(this->state_class)); - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); + size.add_string(1, this->unit_of_measurement_ref_.size()); + size.add_int32(1, this->accuracy_decimals); + size.add_bool(1, this->force_update); + size.add_string(1, this->device_class_ref_.size()); + size.add_enum(1, static_cast(this->state_class)); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void SensorStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -679,11 +679,11 @@ void SensorStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void SensorStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_float_field(1, this->state); - size.add_bool_field(1, this->missing_state); + size.add_fixed32(1, this->key); + size.add_float(1, this->state); + size.add_bool(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } #endif @@ -704,18 +704,18 @@ void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesSwitchResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->assumed_state); - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_string_field(1, this->device_class_ref_.size()); + size.add_bool(1, this->assumed_state); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); + size.add_string(1, this->device_class_ref_.size()); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void SwitchStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -726,10 +726,10 @@ void SwitchStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void SwitchStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_bool_field(1, this->state); + size.add_fixed32(1, this->key); + size.add_bool(1, this->state); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool SwitchCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -774,17 +774,17 @@ void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesTextSensorResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_string_field(1, this->device_class_ref_.size()); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); + size.add_string(1, this->device_class_ref_.size()); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void TextSensorStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -796,11 +796,11 @@ void TextSensorStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void TextSensorStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->state_ref_.size()); - size.add_bool_field(1, this->missing_state); + size.add_fixed32(1, this->key); + size.add_string(1, this->state_ref_.size()); + size.add_bool(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } #endif @@ -822,8 +822,8 @@ void SubscribeLogsResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bytes(3, this->message_ptr_, this->message_len_); } void SubscribeLogsResponse::calculate_size(ProtoSize &size) const { - size.add_enum_field(1, static_cast(this->level)); - size.add_bytes_field(1, this->message_len_); + size.add_enum(1, static_cast(this->level)); + size.add_bytes(1, this->message_len_); } #ifdef USE_API_NOISE bool NoiseEncryptionSetKeyRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) { @@ -837,15 +837,15 @@ bool NoiseEncryptionSetKeyRequest::decode_length(uint32_t field_id, ProtoLengthD return true; } void NoiseEncryptionSetKeyResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->success); } -void NoiseEncryptionSetKeyResponse::calculate_size(ProtoSize &size) const { size.add_bool_field(1, this->success); } +void NoiseEncryptionSetKeyResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->success); } #endif void HomeassistantServiceMap::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->key_ref_); buffer.encode_string(2, this->value_ref_); } void HomeassistantServiceMap::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->key_ref_.size()); - size.add_string_field(1, this->value_ref_.size()); + size.add_string(1, this->key_ref_.size()); + size.add_string(1, this->value_ref_.size()); } void HomeassistantServiceResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->service_ref_); @@ -861,11 +861,11 @@ void HomeassistantServiceResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(5, this->is_event); } void HomeassistantServiceResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->service_ref_.size()); + size.add_string(1, this->service_ref_.size()); size.add_repeated_message(1, this->data); size.add_repeated_message(1, this->data_template); size.add_repeated_message(1, this->variables); - size.add_bool_field(1, this->is_event); + size.add_bool(1, this->is_event); } #ifdef USE_API_HOMEASSISTANT_STATES void SubscribeHomeAssistantStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -874,9 +874,9 @@ void SubscribeHomeAssistantStateResponse::encode(ProtoWriteBuffer buffer) const buffer.encode_bool(3, this->once); } void SubscribeHomeAssistantStateResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->entity_id_ref_.size()); - size.add_string_field(1, this->attribute_ref_.size()); - size.add_bool_field(1, this->once); + size.add_string(1, this->entity_id_ref_.size()); + size.add_string(1, this->attribute_ref_.size()); + size.add_bool(1, this->once); } bool HomeAssistantStateResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { @@ -906,15 +906,15 @@ bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { return true; } void GetTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->epoch_seconds); } -void GetTimeResponse::calculate_size(ProtoSize &size) const { size.add_fixed32_field(1, this->epoch_seconds); } +void GetTimeResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->epoch_seconds); } #ifdef USE_API_SERVICES void ListEntitiesServicesArgument::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->name_ref_); buffer.encode_uint32(2, static_cast(this->type)); } void ListEntitiesServicesArgument::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->name_ref_.size()); - size.add_enum_field(1, static_cast(this->type)); + size.add_string(1, this->name_ref_.size()); + size.add_enum(1, static_cast(this->type)); } void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->name_ref_); @@ -924,8 +924,8 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const { } } void ListEntitiesServicesResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->name_ref_.size()); - size.add_fixed32_field(1, this->key); + size.add_string(1, this->name_ref_.size()); + size.add_fixed32(1, this->key); size.add_repeated_message(1, this->args); } bool ExecuteServiceArgument::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1013,16 +1013,16 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesCameraResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); - size.add_bool_field(1, this->disabled_by_default); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); + size.add_bool(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_enum_field(1, static_cast(this->entity_category)); + size.add_enum(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void CameraImageResponse::encode(ProtoWriteBuffer buffer) const { @@ -1034,11 +1034,11 @@ void CameraImageResponse::encode(ProtoWriteBuffer buffer) const { #endif } void CameraImageResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_bytes_field(1, this->data_len_); - size.add_bool_field(1, this->done); + size.add_fixed32(1, this->key); + size.add_bytes(1, this->data_len_); + size.add_bool(1, this->done); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool CameraImageRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1099,57 +1099,57 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesClimateResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); - size.add_bool_field(1, this->supports_current_temperature); - size.add_bool_field(1, this->supports_two_point_target_temperature); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); + size.add_bool(1, this->supports_current_temperature); + size.add_bool(1, this->supports_two_point_target_temperature); if (!this->supported_modes.empty()) { for (const auto &it : this->supported_modes) { - size.add_enum_field_repeated(1, static_cast(it)); + size.add_enum_repeated(1, static_cast(it)); } } - size.add_float_field(1, this->visual_min_temperature); - size.add_float_field(1, this->visual_max_temperature); - size.add_float_field(1, this->visual_target_temperature_step); - size.add_bool_field(1, this->supports_action); + size.add_float(1, this->visual_min_temperature); + size.add_float(1, this->visual_max_temperature); + size.add_float(1, this->visual_target_temperature_step); + size.add_bool(1, this->supports_action); if (!this->supported_fan_modes.empty()) { for (const auto &it : this->supported_fan_modes) { - size.add_enum_field_repeated(1, static_cast(it)); + size.add_enum_repeated(1, static_cast(it)); } } if (!this->supported_swing_modes.empty()) { for (const auto &it : this->supported_swing_modes) { - size.add_enum_field_repeated(1, static_cast(it)); + size.add_enum_repeated(1, static_cast(it)); } } if (!this->supported_custom_fan_modes.empty()) { for (const auto &it : this->supported_custom_fan_modes) { - size.add_string_field_repeated(1, it); + size.add_string_repeated(1, it); } } if (!this->supported_presets.empty()) { for (const auto &it : this->supported_presets) { - size.add_enum_field_repeated(2, static_cast(it)); + size.add_enum_repeated(2, static_cast(it)); } } if (!this->supported_custom_presets.empty()) { for (const auto &it : this->supported_custom_presets) { - size.add_string_field_repeated(2, it); + size.add_string_repeated(2, it); } } - size.add_bool_field(2, this->disabled_by_default); + size.add_bool(2, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string_field(2, this->icon_ref_.size()); + size.add_string(2, this->icon_ref_.size()); #endif - size.add_enum_field(2, static_cast(this->entity_category)); - size.add_float_field(2, this->visual_current_temperature_step); - size.add_bool_field(2, this->supports_current_humidity); - size.add_bool_field(2, this->supports_target_humidity); - size.add_float_field(2, this->visual_min_humidity); - size.add_float_field(2, this->visual_max_humidity); + size.add_enum(2, static_cast(this->entity_category)); + size.add_float(2, this->visual_current_temperature_step); + size.add_bool(2, this->supports_current_humidity); + size.add_bool(2, this->supports_target_humidity); + size.add_float(2, this->visual_min_humidity); + size.add_float(2, this->visual_max_humidity); #ifdef USE_DEVICES - size.add_uint32_field(2, this->device_id); + size.add_uint32(2, this->device_id); #endif } void ClimateStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1172,22 +1172,22 @@ void ClimateStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ClimateStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_enum_field(1, static_cast(this->mode)); - size.add_float_field(1, this->current_temperature); - size.add_float_field(1, this->target_temperature); - size.add_float_field(1, this->target_temperature_low); - size.add_float_field(1, this->target_temperature_high); - size.add_enum_field(1, static_cast(this->action)); - size.add_enum_field(1, static_cast(this->fan_mode)); - size.add_enum_field(1, static_cast(this->swing_mode)); - size.add_string_field(1, this->custom_fan_mode_ref_.size()); - size.add_enum_field(1, static_cast(this->preset)); - size.add_string_field(1, this->custom_preset_ref_.size()); - size.add_float_field(1, this->current_humidity); - size.add_float_field(1, this->target_humidity); + size.add_fixed32(1, this->key); + size.add_enum(1, static_cast(this->mode)); + size.add_float(1, this->current_temperature); + size.add_float(1, this->target_temperature); + size.add_float(1, this->target_temperature_low); + size.add_float(1, this->target_temperature_high); + size.add_enum(1, static_cast(this->action)); + size.add_enum(1, static_cast(this->fan_mode)); + size.add_enum(1, static_cast(this->swing_mode)); + size.add_string(1, this->custom_fan_mode_ref_.size()); + size.add_enum(1, static_cast(this->preset)); + size.add_string(1, this->custom_preset_ref_.size()); + size.add_float(1, this->current_humidity); + size.add_float(1, this->target_humidity); #ifdef USE_DEVICES - size.add_uint32_field(2, this->device_id); + size.add_uint32(2, this->device_id); #endif } bool ClimateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1301,22 +1301,22 @@ void ListEntitiesNumberResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesNumberResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_float_field(1, this->min_value); - size.add_float_field(1, this->max_value); - size.add_float_field(1, this->step); - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_string_field(1, this->unit_of_measurement_ref_.size()); - size.add_enum_field(1, static_cast(this->mode)); - size.add_string_field(1, this->device_class_ref_.size()); + size.add_float(1, this->min_value); + size.add_float(1, this->max_value); + size.add_float(1, this->step); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); + size.add_string(1, this->unit_of_measurement_ref_.size()); + size.add_enum(1, static_cast(this->mode)); + size.add_string(1, this->device_class_ref_.size()); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void NumberStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1328,11 +1328,11 @@ void NumberStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void NumberStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_float_field(1, this->state); - size.add_bool_field(1, this->missing_state); + size.add_fixed32(1, this->key); + size.add_float(1, this->state); + size.add_bool(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool NumberCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1379,21 +1379,21 @@ void ListEntitiesSelectResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesSelectResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif if (!this->options.empty()) { for (const auto &it : this->options) { - size.add_string_field_repeated(1, it); + size.add_string_repeated(1, it); } } - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void SelectStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1405,11 +1405,11 @@ void SelectStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void SelectStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->state_ref_.size()); - size.add_bool_field(1, this->missing_state); + size.add_fixed32(1, this->key); + size.add_string(1, this->state_ref_.size()); + size.add_bool(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool SelectCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1465,23 +1465,23 @@ void ListEntitiesSirenResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesSirenResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); + size.add_bool(1, this->disabled_by_default); if (!this->tones.empty()) { for (const auto &it : this->tones) { - size.add_string_field_repeated(1, it); + size.add_string_repeated(1, it); } } - size.add_bool_field(1, this->supports_duration); - size.add_bool_field(1, this->supports_volume); - size.add_enum_field(1, static_cast(this->entity_category)); + size.add_bool(1, this->supports_duration); + size.add_bool(1, this->supports_volume); + size.add_enum(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void SirenStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1492,10 +1492,10 @@ void SirenStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void SirenStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_bool_field(1, this->state); + size.add_fixed32(1, this->key); + size.add_bool(1, this->state); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool SirenCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1571,20 +1571,20 @@ void ListEntitiesLockResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesLockResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_bool_field(1, this->assumed_state); - size.add_bool_field(1, this->supports_open); - size.add_bool_field(1, this->requires_code); - size.add_string_field(1, this->code_format_ref_.size()); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); + size.add_bool(1, this->assumed_state); + size.add_bool(1, this->supports_open); + size.add_bool(1, this->requires_code); + size.add_string(1, this->code_format_ref_.size()); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void LockStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1595,10 +1595,10 @@ void LockStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void LockStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_enum_field(1, static_cast(this->state)); + size.add_fixed32(1, this->key); + size.add_enum(1, static_cast(this->state)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool LockCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1656,17 +1656,17 @@ void ListEntitiesButtonResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesButtonResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_string_field(1, this->device_class_ref_.size()); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); + size.add_string(1, this->device_class_ref_.size()); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool ButtonCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1701,11 +1701,11 @@ void MediaPlayerSupportedFormat::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(5, this->sample_bytes); } void MediaPlayerSupportedFormat::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->format_ref_.size()); - size.add_uint32_field(1, this->sample_rate); - size.add_uint32_field(1, this->num_channels); - size.add_enum_field(1, static_cast(this->purpose)); - size.add_uint32_field(1, this->sample_bytes); + size.add_string(1, this->format_ref_.size()); + size.add_uint32(1, this->sample_rate); + size.add_uint32(1, this->num_channels); + size.add_enum(1, static_cast(this->purpose)); + size.add_uint32(1, this->sample_bytes); } void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->object_id_ref_); @@ -1725,18 +1725,18 @@ void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesMediaPlayerResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_bool_field(1, this->supports_pause); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); + size.add_bool(1, this->supports_pause); size.add_repeated_message(1, this->supported_formats); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void MediaPlayerStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1749,12 +1749,12 @@ void MediaPlayerStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void MediaPlayerStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_enum_field(1, static_cast(this->state)); - size.add_float_field(1, this->volume); - size.add_bool_field(1, this->muted); + size.add_fixed32(1, this->key); + size.add_enum(1, static_cast(this->state)); + size.add_float(1, this->volume); + size.add_bool(1, this->muted); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool MediaPlayerCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1829,10 +1829,10 @@ void BluetoothLERawAdvertisement::encode(ProtoWriteBuffer buffer) const { buffer.encode_bytes(4, this->data, this->data_len); } void BluetoothLERawAdvertisement::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); - size.add_sint32_field(1, this->rssi); - size.add_uint32_field(1, this->address_type); - size.add_bytes_field(1, this->data_len); + size.add_uint64(1, this->address); + size.add_sint32(1, this->rssi); + size.add_uint32(1, this->address_type); + size.add_bytes(1, this->data_len); } void BluetoothLERawAdvertisementsResponse::encode(ProtoWriteBuffer buffer) const { for (auto &it : this->advertisements) { @@ -1868,10 +1868,10 @@ void BluetoothDeviceConnectionResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_int32(4, this->error); } void BluetoothDeviceConnectionResponse::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); - size.add_bool_field(1, this->connected); - size.add_uint32_field(1, this->mtu); - size.add_int32_field(1, this->error); + size.add_uint64(1, this->address); + size.add_bool(1, this->connected); + size.add_uint32(1, this->mtu); + size.add_int32(1, this->error); } bool BluetoothGATTGetServicesRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1889,9 +1889,9 @@ void BluetoothGATTDescriptor::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(2, this->handle); } void BluetoothGATTDescriptor::calculate_size(ProtoSize &size) const { - size.add_uint64_field_repeated(1, this->uuid[0]); - size.add_uint64_field_repeated(1, this->uuid[1]); - size.add_uint32_field(1, this->handle); + size.add_uint64_repeated(1, this->uuid[0]); + size.add_uint64_repeated(1, this->uuid[1]); + size.add_uint32(1, this->handle); } void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->uuid[0], true); @@ -1903,10 +1903,10 @@ void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer buffer) const { } } void BluetoothGATTCharacteristic::calculate_size(ProtoSize &size) const { - size.add_uint64_field_repeated(1, this->uuid[0]); - size.add_uint64_field_repeated(1, this->uuid[1]); - size.add_uint32_field(1, this->handle); - size.add_uint32_field(1, this->properties); + size.add_uint64_repeated(1, this->uuid[0]); + size.add_uint64_repeated(1, this->uuid[1]); + size.add_uint32(1, this->handle); + size.add_uint32(1, this->properties); size.add_repeated_message(1, this->descriptors); } void BluetoothGATTService::encode(ProtoWriteBuffer buffer) const { @@ -1918,9 +1918,9 @@ void BluetoothGATTService::encode(ProtoWriteBuffer buffer) const { } } void BluetoothGATTService::calculate_size(ProtoSize &size) const { - size.add_uint64_field_repeated(1, this->uuid[0]); - size.add_uint64_field_repeated(1, this->uuid[1]); - size.add_uint32_field(1, this->handle); + size.add_uint64_repeated(1, this->uuid[0]); + size.add_uint64_repeated(1, this->uuid[1]); + size.add_uint32(1, this->handle); size.add_repeated_message(1, this->characteristics); } void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer buffer) const { @@ -1928,15 +1928,13 @@ void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_message(2, this->services[0], true); } void BluetoothGATTGetServicesResponse::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); + size.add_uint64(1, this->address); size.add_message_object_repeated(1, this->services[0]); } void BluetoothGATTGetServicesDoneResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); } -void BluetoothGATTGetServicesDoneResponse::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); -} +void BluetoothGATTGetServicesDoneResponse::calculate_size(ProtoSize &size) const { size.add_uint64(1, this->address); } bool BluetoothGATTReadRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { case 1: @@ -1956,9 +1954,9 @@ void BluetoothGATTReadResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bytes(3, this->data_ptr_, this->data_len_); } void BluetoothGATTReadResponse::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); - size.add_uint32_field(1, this->handle); - size.add_bytes_field(1, this->data_len_); + size.add_uint64(1, this->address); + size.add_uint32(1, this->handle); + size.add_bytes(1, this->data_len_); } bool BluetoothGATTWriteRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2044,9 +2042,9 @@ void BluetoothGATTNotifyDataResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bytes(3, this->data_ptr_, this->data_len_); } void BluetoothGATTNotifyDataResponse::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); - size.add_uint32_field(1, this->handle); - size.add_bytes_field(1, this->data_len_); + size.add_uint64(1, this->address); + size.add_uint32(1, this->handle); + size.add_bytes(1, this->data_len_); } void BluetoothConnectionsFreeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(1, this->free); @@ -2056,11 +2054,11 @@ void BluetoothConnectionsFreeResponse::encode(ProtoWriteBuffer buffer) const { } } void BluetoothConnectionsFreeResponse::calculate_size(ProtoSize &size) const { - size.add_uint32_field(1, this->free); - size.add_uint32_field(1, this->limit); + size.add_uint32(1, this->free); + size.add_uint32(1, this->limit); if (!this->allocated.empty()) { for (const auto &it : this->allocated) { - size.add_uint64_field_repeated(1, it); + size.add_uint64_repeated(1, it); } } } @@ -2070,25 +2068,25 @@ void BluetoothGATTErrorResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_int32(3, this->error); } void BluetoothGATTErrorResponse::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); - size.add_uint32_field(1, this->handle); - size.add_int32_field(1, this->error); + size.add_uint64(1, this->address); + size.add_uint32(1, this->handle); + size.add_int32(1, this->error); } void BluetoothGATTWriteResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); buffer.encode_uint32(2, this->handle); } void BluetoothGATTWriteResponse::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); - size.add_uint32_field(1, this->handle); + size.add_uint64(1, this->address); + size.add_uint32(1, this->handle); } void BluetoothGATTNotifyResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); buffer.encode_uint32(2, this->handle); } void BluetoothGATTNotifyResponse::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); - size.add_uint32_field(1, this->handle); + size.add_uint64(1, this->address); + size.add_uint32(1, this->handle); } void BluetoothDevicePairingResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); @@ -2096,9 +2094,9 @@ void BluetoothDevicePairingResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_int32(3, this->error); } void BluetoothDevicePairingResponse::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); - size.add_bool_field(1, this->paired); - size.add_int32_field(1, this->error); + size.add_uint64(1, this->address); + size.add_bool(1, this->paired); + size.add_int32(1, this->error); } void BluetoothDeviceUnpairingResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); @@ -2106,9 +2104,9 @@ void BluetoothDeviceUnpairingResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_int32(3, this->error); } void BluetoothDeviceUnpairingResponse::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); - size.add_bool_field(1, this->success); - size.add_int32_field(1, this->error); + size.add_uint64(1, this->address); + size.add_bool(1, this->success); + size.add_int32(1, this->error); } void BluetoothDeviceClearCacheResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); @@ -2116,17 +2114,17 @@ void BluetoothDeviceClearCacheResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_int32(3, this->error); } void BluetoothDeviceClearCacheResponse::calculate_size(ProtoSize &size) const { - size.add_uint64_field(1, this->address); - size.add_bool_field(1, this->success); - size.add_int32_field(1, this->error); + size.add_uint64(1, this->address); + size.add_bool(1, this->success); + size.add_int32(1, this->error); } void BluetoothScannerStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(1, static_cast(this->state)); buffer.encode_uint32(2, static_cast(this->mode)); } void BluetoothScannerStateResponse::calculate_size(ProtoSize &size) const { - size.add_enum_field(1, static_cast(this->state)); - size.add_enum_field(1, static_cast(this->mode)); + size.add_enum(1, static_cast(this->state)); + size.add_enum(1, static_cast(this->mode)); } bool BluetoothScannerSetModeRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2159,9 +2157,9 @@ void VoiceAssistantAudioSettings::encode(ProtoWriteBuffer buffer) const { buffer.encode_float(3, this->volume_multiplier); } void VoiceAssistantAudioSettings::calculate_size(ProtoSize &size) const { - size.add_uint32_field(1, this->noise_suppression_level); - size.add_uint32_field(1, this->auto_gain); - size.add_float_field(1, this->volume_multiplier); + size.add_uint32(1, this->noise_suppression_level); + size.add_uint32(1, this->auto_gain); + size.add_float(1, this->volume_multiplier); } void VoiceAssistantRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->start); @@ -2171,11 +2169,11 @@ void VoiceAssistantRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(5, this->wake_word_phrase_ref_); } void VoiceAssistantRequest::calculate_size(ProtoSize &size) const { - size.add_bool_field(1, this->start); - size.add_string_field(1, this->conversation_id_ref_.size()); - size.add_uint32_field(1, this->flags); + size.add_bool(1, this->start); + size.add_string(1, this->conversation_id_ref_.size()); + size.add_uint32(1, this->flags); size.add_message_object(1, this->audio_settings); - size.add_string_field(1, this->wake_word_phrase_ref_.size()); + size.add_string(1, this->wake_word_phrase_ref_.size()); } bool VoiceAssistantResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2249,8 +2247,8 @@ void VoiceAssistantAudio::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(2, this->end); } void VoiceAssistantAudio::calculate_size(ProtoSize &size) const { - size.add_bytes_field(1, this->data_len_); - size.add_bool_field(1, this->end); + size.add_bytes(1, this->data_len_); + size.add_bool(1, this->end); } bool VoiceAssistantTimerEventResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2311,7 +2309,7 @@ bool VoiceAssistantAnnounceRequest::decode_length(uint32_t field_id, ProtoLength return true; } void VoiceAssistantAnnounceFinished::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->success); } -void VoiceAssistantAnnounceFinished::calculate_size(ProtoSize &size) const { size.add_bool_field(1, this->success); } +void VoiceAssistantAnnounceFinished::calculate_size(ProtoSize &size) const { size.add_bool(1, this->success); } void VoiceAssistantWakeWord::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->id_ref_); buffer.encode_string(2, this->wake_word_ref_); @@ -2320,11 +2318,11 @@ void VoiceAssistantWakeWord::encode(ProtoWriteBuffer buffer) const { } } void VoiceAssistantWakeWord::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->id_ref_.size()); - size.add_string_field(1, this->wake_word_ref_.size()); + size.add_string(1, this->id_ref_.size()); + size.add_string(1, this->wake_word_ref_.size()); if (!this->trained_languages.empty()) { for (const auto &it : this->trained_languages) { - size.add_string_field_repeated(1, it); + size.add_string_repeated(1, it); } } } @@ -2341,10 +2339,10 @@ void VoiceAssistantConfigurationResponse::calculate_size(ProtoSize &size) const size.add_repeated_message(1, this->available_wake_words); if (!this->active_wake_words.empty()) { for (const auto &it : this->active_wake_words) { - size.add_string_field_repeated(1, it); + size.add_string_repeated(1, it); } } - size.add_uint32_field(1, this->max_active_wake_words); + size.add_uint32(1, this->max_active_wake_words); } bool VoiceAssistantSetConfiguration::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { @@ -2375,19 +2373,19 @@ void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer buffer) cons #endif } void ListEntitiesAlarmControlPanelResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_uint32_field(1, this->supported_features); - size.add_bool_field(1, this->requires_code); - size.add_bool_field(1, this->requires_code_to_arm); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, this->supported_features); + size.add_bool(1, this->requires_code); + size.add_bool(1, this->requires_code_to_arm); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2398,10 +2396,10 @@ void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void AlarmControlPanelStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_enum_field(1, static_cast(this->state)); + size.add_fixed32(1, this->key); + size.add_enum(1, static_cast(this->state)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool AlarmControlPanelCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2459,20 +2457,20 @@ void ListEntitiesTextResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesTextResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_uint32_field(1, this->min_length); - size.add_uint32_field(1, this->max_length); - size.add_string_field(1, this->pattern_ref_.size()); - size.add_enum_field(1, static_cast(this->mode)); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, this->min_length); + size.add_uint32(1, this->max_length); + size.add_string(1, this->pattern_ref_.size()); + size.add_enum(1, static_cast(this->mode)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void TextStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2484,11 +2482,11 @@ void TextStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void TextStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->state_ref_.size()); - size.add_bool_field(1, this->missing_state); + size.add_fixed32(1, this->key); + size.add_string(1, this->state_ref_.size()); + size.add_bool(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool TextCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2539,16 +2537,16 @@ void ListEntitiesDateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesDateResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void DateStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2562,13 +2560,13 @@ void DateStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void DateStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_bool_field(1, this->missing_state); - size.add_uint32_field(1, this->year); - size.add_uint32_field(1, this->month); - size.add_uint32_field(1, this->day); + size.add_fixed32(1, this->key); + size.add_bool(1, this->missing_state); + size.add_uint32(1, this->year); + size.add_uint32(1, this->month); + size.add_uint32(1, this->day); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool DateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2618,16 +2616,16 @@ void ListEntitiesTimeResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesTimeResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void TimeStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2641,13 +2639,13 @@ void TimeStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void TimeStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_bool_field(1, this->missing_state); - size.add_uint32_field(1, this->hour); - size.add_uint32_field(1, this->minute); - size.add_uint32_field(1, this->second); + size.add_fixed32(1, this->key); + size.add_bool(1, this->missing_state); + size.add_uint32(1, this->hour); + size.add_uint32(1, this->minute); + size.add_uint32(1, this->second); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool TimeCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2701,22 +2699,22 @@ void ListEntitiesEventResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesEventResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_string_field(1, this->device_class_ref_.size()); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); + size.add_string(1, this->device_class_ref_.size()); if (!this->event_types.empty()) { for (const auto &it : this->event_types) { - size.add_string_field_repeated(1, it); + size.add_string_repeated(1, it); } } #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void EventResponse::encode(ProtoWriteBuffer buffer) const { @@ -2727,10 +2725,10 @@ void EventResponse::encode(ProtoWriteBuffer buffer) const { #endif } void EventResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->event_type_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->event_type_ref_.size()); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } #endif @@ -2753,20 +2751,20 @@ void ListEntitiesValveResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesValveResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_string_field(1, this->device_class_ref_.size()); - size.add_bool_field(1, this->assumed_state); - size.add_bool_field(1, this->supports_position); - size.add_bool_field(1, this->supports_stop); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); + size.add_string(1, this->device_class_ref_.size()); + size.add_bool(1, this->assumed_state); + size.add_bool(1, this->supports_position); + size.add_bool(1, this->supports_stop); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void ValveStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2778,11 +2776,11 @@ void ValveStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ValveStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_float_field(1, this->position); - size.add_enum_field(1, static_cast(this->current_operation)); + size.add_fixed32(1, this->key); + size.add_float(1, this->position); + size.add_enum(1, static_cast(this->current_operation)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool ValveCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2832,16 +2830,16 @@ void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesDateTimeResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void DateTimeStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2853,11 +2851,11 @@ void DateTimeStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void DateTimeStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_bool_field(1, this->missing_state); - size.add_fixed32_field(1, this->epoch_seconds); + size.add_fixed32(1, this->key); + size.add_bool(1, this->missing_state); + size.add_fixed32(1, this->epoch_seconds); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool DateTimeCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2902,17 +2900,17 @@ void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesUpdateResponse::calculate_size(ProtoSize &size) const { - size.add_string_field(1, this->object_id_ref_.size()); - size.add_fixed32_field(1, this->key); - size.add_string_field(1, this->name_ref_.size()); + size.add_string(1, this->object_id_ref_.size()); + size.add_fixed32(1, this->key); + size.add_string(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string_field(1, this->icon_ref_.size()); + size.add_string(1, this->icon_ref_.size()); #endif - size.add_bool_field(1, this->disabled_by_default); - size.add_enum_field(1, static_cast(this->entity_category)); - size.add_string_field(1, this->device_class_ref_.size()); + size.add_bool(1, this->disabled_by_default); + size.add_enum(1, static_cast(this->entity_category)); + size.add_string(1, this->device_class_ref_.size()); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } void UpdateStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -2931,18 +2929,18 @@ void UpdateStateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void UpdateStateResponse::calculate_size(ProtoSize &size) const { - size.add_fixed32_field(1, this->key); - size.add_bool_field(1, this->missing_state); - size.add_bool_field(1, this->in_progress); - size.add_bool_field(1, this->has_progress); - size.add_float_field(1, this->progress); - size.add_string_field(1, this->current_version_ref_.size()); - size.add_string_field(1, this->latest_version_ref_.size()); - size.add_string_field(1, this->title_ref_.size()); - size.add_string_field(1, this->release_summary_ref_.size()); - size.add_string_field(1, this->release_url_ref_.size()); + size.add_fixed32(1, this->key); + size.add_bool(1, this->missing_state); + size.add_bool(1, this->in_progress); + size.add_bool(1, this->has_progress); + size.add_float(1, this->progress); + size.add_string(1, this->current_version_ref_.size()); + size.add_string(1, this->latest_version_ref_.size()); + size.add_string(1, this->title_ref_.size()); + size.add_string(1, this->release_summary_ref_.size()); + size.add_string(1, this->release_url_ref_.size()); #ifdef USE_DEVICES - size.add_uint32_field(1, this->device_id); + size.add_uint32(1, this->device_id); #endif } bool UpdateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index eb3d5c7302..9073477fa3 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -503,7 +503,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of an int32 field to the total message size */ - inline void add_int32_field(uint32_t field_id_size, int32_t value) { + inline void add_int32(uint32_t field_id_size, int32_t value) { // Skip calculation if value is zero if (value == 0) { return; // No need to update total_size_ @@ -522,7 +522,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of an int32 field to the total message size (repeated field version) */ - inline void add_int32_field_repeated(uint32_t field_id_size, int32_t value) { + inline void add_int32_repeated(uint32_t field_id_size, int32_t value) { // Always calculate size for repeated fields if (value < 0) { // Negative values are encoded as 10-byte varints in protobuf @@ -536,7 +536,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a uint32 field to the total message size */ - inline void add_uint32_field(uint32_t field_id_size, uint32_t value) { + inline void add_uint32(uint32_t field_id_size, uint32_t value) { // Skip calculation if value is zero if (value == 0) { return; // No need to update total_size_ @@ -549,7 +549,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a uint32 field to the total message size (repeated field version) */ - inline void add_uint32_field_repeated(uint32_t field_id_size, uint32_t value) { + inline void add_uint32_repeated(uint32_t field_id_size, uint32_t value) { // Always calculate size for repeated fields total_size_ += field_id_size + varint(value); } @@ -557,7 +557,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a boolean field to the total message size */ - inline void add_bool_field(uint32_t field_id_size, bool value) { + inline void add_bool(uint32_t field_id_size, bool value) { // Skip calculation if value is false if (!value) { return; // No need to update total_size_ @@ -570,7 +570,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a boolean field to the total message size (repeated field version) */ - inline void add_bool_field_repeated(uint32_t field_id_size, bool value) { + inline void add_bool_repeated(uint32_t field_id_size, bool value) { // Always calculate size for repeated fields // Boolean fields always use 1 byte total_size_ += field_id_size + 1; @@ -579,7 +579,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a float field to the total message size */ - inline void add_float_field(uint32_t field_id_size, float value) { + inline void add_float(uint32_t field_id_size, float value) { if (value != 0.0f) { total_size_ += field_id_size + 4; } @@ -591,7 +591,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a fixed32 field to the total message size */ - inline void add_fixed32_field(uint32_t field_id_size, uint32_t value) { + inline void add_fixed32(uint32_t field_id_size, uint32_t value) { if (value != 0) { total_size_ += field_id_size + 4; } @@ -603,7 +603,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a sfixed32 field to the total message size */ - inline void add_sfixed32_field(uint32_t field_id_size, int32_t value) { + inline void add_sfixed32(uint32_t field_id_size, int32_t value) { if (value != 0) { total_size_ += field_id_size + 4; } @@ -617,7 +617,7 @@ class ProtoSize { * * Enum fields are encoded as uint32 varints. */ - inline void add_enum_field(uint32_t field_id_size, uint32_t value) { + inline void add_enum(uint32_t field_id_size, uint32_t value) { // Skip calculation if value is zero if (value == 0) { return; // No need to update total_size_ @@ -632,7 +632,7 @@ class ProtoSize { * * Enum fields are encoded as uint32 varints. */ - inline void add_enum_field_repeated(uint32_t field_id_size, uint32_t value) { + inline void add_enum_repeated(uint32_t field_id_size, uint32_t value) { // Always calculate size for repeated fields // Enums are encoded as uint32 total_size_ += field_id_size + varint(value); @@ -643,7 +643,7 @@ class ProtoSize { * * Sint32 fields use ZigZag encoding, which is more efficient for negative values. */ - inline void add_sint32_field(uint32_t field_id_size, int32_t value) { + inline void add_sint32(uint32_t field_id_size, int32_t value) { // Skip calculation if value is zero if (value == 0) { return; // No need to update total_size_ @@ -659,7 +659,7 @@ class ProtoSize { * * Sint32 fields use ZigZag encoding, which is more efficient for negative values. */ - inline void add_sint32_field_repeated(uint32_t field_id_size, int32_t value) { + inline void add_sint32_repeated(uint32_t field_id_size, int32_t value) { // Always calculate size for repeated fields // ZigZag encoding for sint32: (n << 1) ^ (n >> 31) uint32_t zigzag = (static_cast(value) << 1) ^ (static_cast(value >> 31)); @@ -669,7 +669,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of an int64 field to the total message size */ - inline void add_int64_field(uint32_t field_id_size, int64_t value) { + inline void add_int64(uint32_t field_id_size, int64_t value) { // Skip calculation if value is zero if (value == 0) { return; // No need to update total_size_ @@ -682,7 +682,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of an int64 field to the total message size (repeated field version) */ - inline void add_int64_field_repeated(uint32_t field_id_size, int64_t value) { + inline void add_int64_repeated(uint32_t field_id_size, int64_t value) { // Always calculate size for repeated fields total_size_ += field_id_size + varint(value); } @@ -690,7 +690,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a uint64 field to the total message size */ - inline void add_uint64_field(uint32_t field_id_size, uint64_t value) { + inline void add_uint64(uint32_t field_id_size, uint64_t value) { // Skip calculation if value is zero if (value == 0) { return; // No need to update total_size_ @@ -703,7 +703,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a uint64 field to the total message size (repeated field version) */ - inline void add_uint64_field_repeated(uint32_t field_id_size, uint64_t value) { + inline void add_uint64_repeated(uint32_t field_id_size, uint64_t value) { // Always calculate size for repeated fields total_size_ += field_id_size + varint(value); } @@ -714,7 +714,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a string field using length */ - inline void add_string_field(uint32_t field_id_size, size_t len) { + inline void add_string(uint32_t field_id_size, size_t len) { // Skip calculation if string is empty if (len == 0) { return; // No need to update total_size_ @@ -727,7 +727,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a string/bytes field to the total message size (repeated field version) */ - inline void add_string_field_repeated(uint32_t field_id_size, const std::string &str) { + inline void add_string_repeated(uint32_t field_id_size, const std::string &str) { // Always calculate size for repeated fields const uint32_t str_size = static_cast(str.size()); total_size_ += field_id_size + varint(str_size) + str_size; @@ -736,7 +736,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a bytes field to the total message size */ - inline void add_bytes_field(uint32_t field_id_size, size_t len) { + inline void add_bytes(uint32_t field_id_size, size_t len) { // Skip calculation if bytes is empty if (len == 0) { return; // No need to update total_size_ @@ -749,7 +749,7 @@ class ProtoSize { /** * @brief Calculates and adds the size of a bytes field to the total message size (repeated field version) */ - inline void add_bytes_field_repeated(uint32_t field_id_size, size_t len) { + inline void add_bytes_repeated(uint32_t field_id_size, size_t len) { // Always calculate size for repeated fields // Field ID + length varint + data bytes total_size_ += field_id_size + varint(static_cast(len)) + static_cast(len); diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 56f74aeaad..a732c1733f 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -275,7 +275,7 @@ class TypeInfo(ABC): Args: name: Field name force: Whether this is for a repeated field - base_method: Base method name (e.g., "add_int32_field") + base_method: Base method name (e.g., "add_int32") value_expr: Optional value expression (defaults to name) """ field_id_size = self.calculate_field_id_size() @@ -389,7 +389,7 @@ class DoubleType(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"size.add_double_field({field_id_size}, {name});" + return f"size.add_double({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8 @@ -413,7 +413,7 @@ class FloatType(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"size.add_float_field({field_id_size}, {name});" + return f"size.add_float({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -436,7 +436,7 @@ class Int64Type(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_simple_size_calculation(name, force, "add_int64_field") + return self._get_simple_size_calculation(name, force, "add_int64") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint @@ -456,7 +456,7 @@ class UInt64Type(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_simple_size_calculation(name, force, "add_uint64_field") + return self._get_simple_size_calculation(name, force, "add_uint64") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint @@ -476,7 +476,7 @@ class Int32Type(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_simple_size_calculation(name, force, "add_int32_field") + return self._get_simple_size_calculation(name, force, "add_int32") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint @@ -497,7 +497,7 @@ class Fixed64Type(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"size.add_fixed64_field({field_id_size}, {name});" + return f"size.add_fixed64({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8 @@ -521,7 +521,7 @@ class Fixed32Type(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"size.add_fixed32_field({field_id_size}, {name});" + return f"size.add_fixed32({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -543,7 +543,7 @@ class BoolType(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_simple_size_calculation(name, force, "add_bool_field") + return self._get_simple_size_calculation(name, force, "add_bool") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 1 # field ID + 1 byte @@ -624,18 +624,18 @@ class StringType(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: # For SOURCE_CLIENT only messages, use the string field directly if not self._needs_encode: - return self._get_simple_size_calculation(name, force, "add_string_field") + return self._get_simple_size_calculation(name, force, "add_string") # Check if this is being called from a repeated field context # In that case, 'name' will be 'it' and we need to use the repeated version if name == "it": - # For repeated fields, we need to use add_string_field_repeated which includes field ID + # For repeated fields, we need to use add_string_repeated which includes field ID field_id_size = self.calculate_field_id_size() - return f"size.add_string_field_repeated({field_id_size}, it);" + return f"size.add_string_repeated({field_id_size}, it);" # For messages that need encoding, use the StringRef size field_id_size = self.calculate_field_id_size() - return f"size.add_string_field({field_id_size}, this->{self.field_name}_ref_.size());" + return f"size.add_string({field_id_size}, this->{self.field_name}_ref_.size());" def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 8 # field ID + 8 bytes typical string @@ -770,7 +770,7 @@ class BytesType(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return f"size.add_bytes_field({self.calculate_field_id_size()}, this->{self.field_name}_len_);" + return f"size.add_bytes({self.calculate_field_id_size()}, this->{self.field_name}_len_);" def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 8 # field ID + 8 bytes typical bytes @@ -846,10 +846,10 @@ class FixedArrayBytesType(TypeInfo): if force: # For repeated fields, always calculate size (no zero check) - return f"size.add_bytes_field_repeated({field_id_size}, {length_field});" + return f"size.add_bytes_repeated({field_id_size}, {length_field});" else: - # For non-repeated fields, add_bytes_field already checks for zero - return f"size.add_bytes_field({field_id_size}, {length_field});" + # For non-repeated fields, add_bytes already checks for zero + return f"size.add_bytes({field_id_size}, {length_field});" def get_estimated_size(self) -> int: # Estimate based on typical BLE advertisement size @@ -876,7 +876,7 @@ class UInt32Type(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_simple_size_calculation(name, force, "add_uint32_field") + return self._get_simple_size_calculation(name, force, "add_uint32") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint @@ -913,7 +913,7 @@ class EnumType(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: return self._get_simple_size_calculation( - name, force, "add_enum_field", f"static_cast({name})" + name, force, "add_enum", f"static_cast({name})" ) def get_estimated_size(self) -> int: @@ -935,7 +935,7 @@ class SFixed32Type(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"size.add_sfixed32_field({field_id_size}, {name});" + return f"size.add_sfixed32({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -959,7 +959,7 @@ class SFixed64Type(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: field_id_size = self.calculate_field_id_size() - return f"size.add_sfixed64_field({field_id_size}, {name});" + return f"size.add_sfixed64({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8 @@ -982,7 +982,7 @@ class SInt32Type(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_simple_size_calculation(name, force, "add_sint32_field") + return self._get_simple_size_calculation(name, force, "add_sint32") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint @@ -1002,7 +1002,7 @@ class SInt64Type(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_simple_size_calculation(name, force, "add_sint64_field") + return self._get_simple_size_calculation(name, force, "add_sint64") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint From e94f5bffa33db8c1b65e4ac0cd478478e2c20f74 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 14:45:26 -1000 Subject: [PATCH 05/24] preen --- esphome/components/api/api_pb2.cpp | 96 ++++++++++++++--------------- esphome/components/api/proto.h | 26 -------- script/api_protobuf/api_protobuf.py | 2 +- 3 files changed, 49 insertions(+), 75 deletions(-) diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index ab6c5513de..86ba1dae57 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -202,7 +202,7 @@ void ListEntitiesBinarySensorResponse::calculate_size(ProtoSize &size) const { #ifdef USE_ENTITY_ICON size.add_string(1, this->icon_ref_.size()); #endif - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -255,7 +255,7 @@ void ListEntitiesCoverResponse::calculate_size(ProtoSize &size) const { #ifdef USE_ENTITY_ICON size.add_string(1, this->icon_ref_.size()); #endif - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_bool(1, this->supports_stop); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); @@ -274,7 +274,7 @@ void CoverStateResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); size.add_float(1, this->position); size.add_float(1, this->tilt); - size.add_enum(1, static_cast(this->current_operation)); + size.add_uint32(1, static_cast(this->current_operation)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -350,7 +350,7 @@ void ListEntitiesFanResponse::calculate_size(ProtoSize &size) const { #ifdef USE_ENTITY_ICON size.add_string(1, this->icon_ref_.size()); #endif - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); if (!this->supported_preset_modes.empty()) { for (const auto &it : this->supported_preset_modes) { size.add_string_repeated(1, it); @@ -375,7 +375,7 @@ void FanStateResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); size.add_bool(1, this->state); size.add_bool(1, this->oscillating); - size.add_enum(1, static_cast(this->direction)); + size.add_uint32(1, static_cast(this->direction)); size.add_int32(1, this->speed_level); size.add_string(1, this->preset_mode_ref_.size()); #ifdef USE_DEVICES @@ -470,7 +470,7 @@ void ListEntitiesLightResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->name_ref_.size()); if (!this->supported_color_modes.empty()) { for (const auto &it : this->supported_color_modes) { - size.add_enum_repeated(1, static_cast(it)); + size.add_uint32_repeated(1, static_cast(it)); } } size.add_float(1, this->min_mireds); @@ -484,7 +484,7 @@ void ListEntitiesLightResponse::calculate_size(ProtoSize &size) const { #ifdef USE_ENTITY_ICON size.add_string(1, this->icon_ref_.size()); #endif - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES size.add_uint32(2, this->device_id); #endif @@ -511,7 +511,7 @@ void LightStateResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); size.add_bool(1, this->state); size.add_float(1, this->brightness); - size.add_enum(1, static_cast(this->color_mode)); + size.add_uint32(1, static_cast(this->color_mode)); size.add_float(1, this->color_brightness); size.add_float(1, this->red); size.add_float(1, this->green); @@ -663,9 +663,9 @@ void ListEntitiesSensorResponse::calculate_size(ProtoSize &size) const { size.add_int32(1, this->accuracy_decimals); size.add_bool(1, this->force_update); size.add_string(1, this->device_class_ref_.size()); - size.add_enum(1, static_cast(this->state_class)); + size.add_uint32(1, static_cast(this->state_class)); size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -712,7 +712,7 @@ void ListEntitiesSwitchResponse::calculate_size(ProtoSize &size) const { #endif size.add_bool(1, this->assumed_state); size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_string(1, this->device_class_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); @@ -781,7 +781,7 @@ void ListEntitiesTextSensorResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_string(1, this->device_class_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); @@ -822,7 +822,7 @@ void SubscribeLogsResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bytes(3, this->message_ptr_, this->message_len_); } void SubscribeLogsResponse::calculate_size(ProtoSize &size) const { - size.add_enum(1, static_cast(this->level)); + size.add_uint32(1, static_cast(this->level)); size.add_bytes(1, this->message_len_); } #ifdef USE_API_NOISE @@ -914,7 +914,7 @@ void ListEntitiesServicesArgument::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesServicesArgument::calculate_size(ProtoSize &size) const { size.add_string(1, this->name_ref_.size()); - size.add_enum(1, static_cast(this->type)); + size.add_uint32(1, static_cast(this->type)); } void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->name_ref_); @@ -1020,7 +1020,7 @@ void ListEntitiesCameraResponse::calculate_size(ProtoSize &size) const { #ifdef USE_ENTITY_ICON size.add_string(1, this->icon_ref_.size()); #endif - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -1106,7 +1106,7 @@ void ListEntitiesClimateResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->supports_two_point_target_temperature); if (!this->supported_modes.empty()) { for (const auto &it : this->supported_modes) { - size.add_enum_repeated(1, static_cast(it)); + size.add_uint32_repeated(1, static_cast(it)); } } size.add_float(1, this->visual_min_temperature); @@ -1115,12 +1115,12 @@ void ListEntitiesClimateResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->supports_action); if (!this->supported_fan_modes.empty()) { for (const auto &it : this->supported_fan_modes) { - size.add_enum_repeated(1, static_cast(it)); + size.add_uint32_repeated(1, static_cast(it)); } } if (!this->supported_swing_modes.empty()) { for (const auto &it : this->supported_swing_modes) { - size.add_enum_repeated(1, static_cast(it)); + size.add_uint32_repeated(1, static_cast(it)); } } if (!this->supported_custom_fan_modes.empty()) { @@ -1130,7 +1130,7 @@ void ListEntitiesClimateResponse::calculate_size(ProtoSize &size) const { } if (!this->supported_presets.empty()) { for (const auto &it : this->supported_presets) { - size.add_enum_repeated(2, static_cast(it)); + size.add_uint32_repeated(2, static_cast(it)); } } if (!this->supported_custom_presets.empty()) { @@ -1142,7 +1142,7 @@ void ListEntitiesClimateResponse::calculate_size(ProtoSize &size) const { #ifdef USE_ENTITY_ICON size.add_string(2, this->icon_ref_.size()); #endif - size.add_enum(2, static_cast(this->entity_category)); + size.add_uint32(2, static_cast(this->entity_category)); size.add_float(2, this->visual_current_temperature_step); size.add_bool(2, this->supports_current_humidity); size.add_bool(2, this->supports_target_humidity); @@ -1173,16 +1173,16 @@ void ClimateStateResponse::encode(ProtoWriteBuffer buffer) const { } void ClimateStateResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); - size.add_enum(1, static_cast(this->mode)); + size.add_uint32(1, static_cast(this->mode)); size.add_float(1, this->current_temperature); size.add_float(1, this->target_temperature); size.add_float(1, this->target_temperature_low); size.add_float(1, this->target_temperature_high); - size.add_enum(1, static_cast(this->action)); - size.add_enum(1, static_cast(this->fan_mode)); - size.add_enum(1, static_cast(this->swing_mode)); + size.add_uint32(1, static_cast(this->action)); + size.add_uint32(1, static_cast(this->fan_mode)); + size.add_uint32(1, static_cast(this->swing_mode)); size.add_string(1, this->custom_fan_mode_ref_.size()); - size.add_enum(1, static_cast(this->preset)); + size.add_uint32(1, static_cast(this->preset)); size.add_string(1, this->custom_preset_ref_.size()); size.add_float(1, this->current_humidity); size.add_float(1, this->target_humidity); @@ -1311,9 +1311,9 @@ void ListEntitiesNumberResponse::calculate_size(ProtoSize &size) const { size.add_float(1, this->max_value); size.add_float(1, this->step); size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_string(1, this->unit_of_measurement_ref_.size()); - size.add_enum(1, static_cast(this->mode)); + size.add_uint32(1, static_cast(this->mode)); size.add_string(1, this->device_class_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); @@ -1391,7 +1391,7 @@ void ListEntitiesSelectResponse::calculate_size(ProtoSize &size) const { } } size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -1479,7 +1479,7 @@ void ListEntitiesSirenResponse::calculate_size(ProtoSize &size) const { } size.add_bool(1, this->supports_duration); size.add_bool(1, this->supports_volume); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -1578,7 +1578,7 @@ void ListEntitiesLockResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_bool(1, this->assumed_state); size.add_bool(1, this->supports_open); size.add_bool(1, this->requires_code); @@ -1596,7 +1596,7 @@ void LockStateResponse::encode(ProtoWriteBuffer buffer) const { } void LockStateResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); - size.add_enum(1, static_cast(this->state)); + size.add_uint32(1, static_cast(this->state)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -1663,7 +1663,7 @@ void ListEntitiesButtonResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_string(1, this->device_class_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); @@ -1704,7 +1704,7 @@ void MediaPlayerSupportedFormat::calculate_size(ProtoSize &size) const { size.add_string(1, this->format_ref_.size()); size.add_uint32(1, this->sample_rate); size.add_uint32(1, this->num_channels); - size.add_enum(1, static_cast(this->purpose)); + size.add_uint32(1, static_cast(this->purpose)); size.add_uint32(1, this->sample_bytes); } void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer buffer) const { @@ -1732,7 +1732,7 @@ void ListEntitiesMediaPlayerResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_bool(1, this->supports_pause); size.add_repeated_message(1, this->supported_formats); #ifdef USE_DEVICES @@ -1750,7 +1750,7 @@ void MediaPlayerStateResponse::encode(ProtoWriteBuffer buffer) const { } void MediaPlayerStateResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); - size.add_enum(1, static_cast(this->state)); + size.add_uint32(1, static_cast(this->state)); size.add_float(1, this->volume); size.add_bool(1, this->muted); #ifdef USE_DEVICES @@ -2123,8 +2123,8 @@ void BluetoothScannerStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(2, static_cast(this->mode)); } void BluetoothScannerStateResponse::calculate_size(ProtoSize &size) const { - size.add_enum(1, static_cast(this->state)); - size.add_enum(1, static_cast(this->mode)); + size.add_uint32(1, static_cast(this->state)); + size.add_uint32(1, static_cast(this->mode)); } bool BluetoothScannerSetModeRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2380,7 +2380,7 @@ void ListEntitiesAlarmControlPanelResponse::calculate_size(ProtoSize &size) cons size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_uint32(1, this->supported_features); size.add_bool(1, this->requires_code); size.add_bool(1, this->requires_code_to_arm); @@ -2397,7 +2397,7 @@ void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer buffer) const { } void AlarmControlPanelStateResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); - size.add_enum(1, static_cast(this->state)); + size.add_uint32(1, static_cast(this->state)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -2464,11 +2464,11 @@ void ListEntitiesTextResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_uint32(1, this->min_length); size.add_uint32(1, this->max_length); size.add_string(1, this->pattern_ref_.size()); - size.add_enum(1, static_cast(this->mode)); + size.add_uint32(1, static_cast(this->mode)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -2544,7 +2544,7 @@ void ListEntitiesDateResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -2623,7 +2623,7 @@ void ListEntitiesTimeResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -2706,7 +2706,7 @@ void ListEntitiesEventResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_string(1, this->device_class_ref_.size()); if (!this->event_types.empty()) { for (const auto &it : this->event_types) { @@ -2758,7 +2758,7 @@ void ListEntitiesValveResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_string(1, this->device_class_ref_.size()); size.add_bool(1, this->assumed_state); size.add_bool(1, this->supports_position); @@ -2778,7 +2778,7 @@ void ValveStateResponse::encode(ProtoWriteBuffer buffer) const { void ValveStateResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); size.add_float(1, this->position); - size.add_enum(1, static_cast(this->current_operation)); + size.add_uint32(1, static_cast(this->current_operation)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -2837,7 +2837,7 @@ void ListEntitiesDateTimeResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -2907,7 +2907,7 @@ void ListEntitiesUpdateResponse::calculate_size(ProtoSize &size) const { size.add_string(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); - size.add_enum(1, static_cast(this->entity_category)); + size.add_uint32(1, static_cast(this->entity_category)); size.add_string(1, this->device_class_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 9073477fa3..aab17bef0b 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -612,32 +612,6 @@ class ProtoSize { // NOTE: add_sfixed64_field removed - wire type 1 (64-bit: sfixed64) not supported // to reduce overhead on embedded systems - /** - * @brief Calculates and adds the size of an enum field to the total message size - * - * Enum fields are encoded as uint32 varints. - */ - inline void add_enum(uint32_t field_id_size, uint32_t value) { - // Skip calculation if value is zero - if (value == 0) { - return; // No need to update total_size_ - } - - // Enums are encoded as uint32 - total_size_ += field_id_size + varint(value); - } - - /** - * @brief Calculates and adds the size of an enum field to the total message size (repeated field version) - * - * Enum fields are encoded as uint32 varints. - */ - inline void add_enum_repeated(uint32_t field_id_size, uint32_t value) { - // Always calculate size for repeated fields - // Enums are encoded as uint32 - total_size_ += field_id_size + varint(value); - } - /** * @brief Calculates and adds the size of a sint32 field to the total message size * diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index a732c1733f..717c1d4009 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -913,7 +913,7 @@ class EnumType(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: return self._get_simple_size_calculation( - name, force, "add_enum", f"static_cast({name})" + name, force, "add_uint32", f"static_cast({name})" ) def get_estimated_size(self) -> int: From d98a3fca964e1f05d4b167650337063468892249 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 14:49:43 -1000 Subject: [PATCH 06/24] dry --- esphome/components/api/api_pb2.cpp | 276 ++++++++++++++-------------- esphome/components/api/proto.h | 33 +--- script/api_protobuf/api_protobuf.py | 16 +- 3 files changed, 152 insertions(+), 173 deletions(-) diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 86ba1dae57..e3794ae56b 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -39,8 +39,8 @@ void HelloResponse::encode(ProtoWriteBuffer buffer) const { void HelloResponse::calculate_size(ProtoSize &size) const { size.add_uint32(1, this->api_version_major); size.add_uint32(1, this->api_version_minor); - size.add_string(1, this->server_info_ref_.size()); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->server_info_ref_.size()); + size.add_length(1, this->name_ref_.size()); } bool ConnectRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { @@ -61,7 +61,7 @@ void AreaInfo::encode(ProtoWriteBuffer buffer) const { } void AreaInfo::calculate_size(ProtoSize &size) const { size.add_uint32(1, this->area_id); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); } #endif #ifdef USE_DEVICES @@ -72,7 +72,7 @@ void DeviceInfo::encode(ProtoWriteBuffer buffer) const { } void DeviceInfo::calculate_size(ProtoSize &size) const { size.add_uint32(1, this->device_id); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); size.add_uint32(1, this->area_id); } #endif @@ -132,19 +132,19 @@ void DeviceInfoResponse::calculate_size(ProtoSize &size) const { #ifdef USE_API_PASSWORD size.add_bool(1, this->uses_password); #endif - size.add_string(1, this->name_ref_.size()); - size.add_string(1, this->mac_address_ref_.size()); - size.add_string(1, this->esphome_version_ref_.size()); - size.add_string(1, this->compilation_time_ref_.size()); - size.add_string(1, this->model_ref_.size()); + size.add_length(1, this->name_ref_.size()); + size.add_length(1, this->mac_address_ref_.size()); + size.add_length(1, this->esphome_version_ref_.size()); + size.add_length(1, this->compilation_time_ref_.size()); + size.add_length(1, this->model_ref_.size()); #ifdef USE_DEEP_SLEEP size.add_bool(1, this->has_deep_sleep); #endif #ifdef ESPHOME_PROJECT_NAME - size.add_string(1, this->project_name_ref_.size()); + size.add_length(1, this->project_name_ref_.size()); #endif #ifdef ESPHOME_PROJECT_NAME - size.add_string(1, this->project_version_ref_.size()); + size.add_length(1, this->project_version_ref_.size()); #endif #ifdef USE_WEBSERVER size.add_uint32(1, this->webserver_port); @@ -152,16 +152,16 @@ void DeviceInfoResponse::calculate_size(ProtoSize &size) const { #ifdef USE_BLUETOOTH_PROXY size.add_uint32(1, this->bluetooth_proxy_feature_flags); #endif - size.add_string(1, this->manufacturer_ref_.size()); - size.add_string(1, this->friendly_name_ref_.size()); + size.add_length(1, this->manufacturer_ref_.size()); + size.add_length(1, this->friendly_name_ref_.size()); #ifdef USE_VOICE_ASSISTANT size.add_uint32(2, this->voice_assistant_feature_flags); #endif #ifdef USE_AREAS - size.add_string(2, this->suggested_area_ref_.size()); + size.add_length(2, this->suggested_area_ref_.size()); #endif #ifdef USE_BLUETOOTH_PROXY - size.add_string(2, this->bluetooth_mac_address_ref_.size()); + size.add_length(2, this->bluetooth_mac_address_ref_.size()); #endif #ifdef USE_API_NOISE size.add_bool(2, this->api_encryption_supported); @@ -193,14 +193,14 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesBinarySensorResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); - size.add_string(1, this->device_class_ref_.size()); + size.add_length(1, this->name_ref_.size()); + size.add_length(1, this->device_class_ref_.size()); size.add_bool(1, this->is_status_binary_sensor); size.add_bool(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES @@ -244,16 +244,16 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesCoverResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); size.add_bool(1, this->assumed_state); size.add_bool(1, this->supports_position); size.add_bool(1, this->supports_tilt); - size.add_string(1, this->device_class_ref_.size()); + size.add_length(1, this->device_class_ref_.size()); size.add_bool(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_uint32(1, static_cast(this->entity_category)); size.add_bool(1, this->supports_stop); @@ -339,21 +339,21 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesFanResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); size.add_bool(1, this->supports_oscillation); size.add_bool(1, this->supports_speed); size.add_bool(1, this->supports_direction); size.add_int32(1, this->supported_speed_count); size.add_bool(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_uint32(1, static_cast(this->entity_category)); if (!this->supported_preset_modes.empty()) { for (const auto &it : this->supported_preset_modes) { - size.add_string_repeated(1, it); + size.add_length_repeated(1, it.size()); } } #ifdef USE_DEVICES @@ -377,7 +377,7 @@ void FanStateResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->oscillating); size.add_uint32(1, static_cast(this->direction)); size.add_int32(1, this->speed_level); - size.add_string(1, this->preset_mode_ref_.size()); + size.add_length(1, this->preset_mode_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -465,9 +465,9 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesLightResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); if (!this->supported_color_modes.empty()) { for (const auto &it : this->supported_color_modes) { size.add_uint32_repeated(1, static_cast(it)); @@ -477,12 +477,12 @@ void ListEntitiesLightResponse::calculate_size(ProtoSize &size) const { size.add_float(1, this->max_mireds); if (!this->effects.empty()) { for (const auto &it : this->effects) { - size.add_string_repeated(1, it); + size.add_length_repeated(1, it.size()); } } size.add_bool(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES @@ -520,7 +520,7 @@ void LightStateResponse::calculate_size(ProtoSize &size) const { size.add_float(1, this->color_temperature); size.add_float(1, this->cold_white); size.add_float(1, this->warm_white); - size.add_string(1, this->effect_ref_.size()); + size.add_length(1, this->effect_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -653,16 +653,16 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesSensorResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif - size.add_string(1, this->unit_of_measurement_ref_.size()); + size.add_length(1, this->unit_of_measurement_ref_.size()); size.add_int32(1, this->accuracy_decimals); size.add_bool(1, this->force_update); - size.add_string(1, this->device_class_ref_.size()); + size.add_length(1, this->device_class_ref_.size()); size.add_uint32(1, static_cast(this->state_class)); size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); @@ -704,16 +704,16 @@ void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesSwitchResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->assumed_state); size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); - size.add_string(1, this->device_class_ref_.size()); + size.add_length(1, this->device_class_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -774,15 +774,15 @@ void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesTextSensorResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); - size.add_string(1, this->device_class_ref_.size()); + size.add_length(1, this->device_class_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -797,7 +797,7 @@ void TextSensorStateResponse::encode(ProtoWriteBuffer buffer) const { } void TextSensorStateResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); - size.add_string(1, this->state_ref_.size()); + size.add_length(1, this->state_ref_.size()); size.add_bool(1, this->missing_state); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); @@ -823,7 +823,7 @@ void SubscribeLogsResponse::encode(ProtoWriteBuffer buffer) const { } void SubscribeLogsResponse::calculate_size(ProtoSize &size) const { size.add_uint32(1, static_cast(this->level)); - size.add_bytes(1, this->message_len_); + size.add_length(1, this->message_len_); } #ifdef USE_API_NOISE bool NoiseEncryptionSetKeyRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) { @@ -844,8 +844,8 @@ void HomeassistantServiceMap::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(2, this->value_ref_); } void HomeassistantServiceMap::calculate_size(ProtoSize &size) const { - size.add_string(1, this->key_ref_.size()); - size.add_string(1, this->value_ref_.size()); + size.add_length(1, this->key_ref_.size()); + size.add_length(1, this->value_ref_.size()); } void HomeassistantServiceResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->service_ref_); @@ -861,7 +861,7 @@ void HomeassistantServiceResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(5, this->is_event); } void HomeassistantServiceResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->service_ref_.size()); + size.add_length(1, this->service_ref_.size()); size.add_repeated_message(1, this->data); size.add_repeated_message(1, this->data_template); size.add_repeated_message(1, this->variables); @@ -874,8 +874,8 @@ void SubscribeHomeAssistantStateResponse::encode(ProtoWriteBuffer buffer) const buffer.encode_bool(3, this->once); } void SubscribeHomeAssistantStateResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->entity_id_ref_.size()); - size.add_string(1, this->attribute_ref_.size()); + size.add_length(1, this->entity_id_ref_.size()); + size.add_length(1, this->attribute_ref_.size()); size.add_bool(1, this->once); } bool HomeAssistantStateResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) { @@ -913,7 +913,7 @@ void ListEntitiesServicesArgument::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(2, static_cast(this->type)); } void ListEntitiesServicesArgument::calculate_size(ProtoSize &size) const { - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); size.add_uint32(1, static_cast(this->type)); } void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const { @@ -924,7 +924,7 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const { } } void ListEntitiesServicesResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); size.add_fixed32(1, this->key); size.add_repeated_message(1, this->args); } @@ -1013,12 +1013,12 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesCameraResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); size.add_bool(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES @@ -1035,7 +1035,7 @@ void CameraImageResponse::encode(ProtoWriteBuffer buffer) const { } void CameraImageResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); - size.add_bytes(1, this->data_len_); + size.add_length(1, this->data_len_); size.add_bool(1, this->done); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); @@ -1099,9 +1099,9 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesClimateResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); size.add_bool(1, this->supports_current_temperature); size.add_bool(1, this->supports_two_point_target_temperature); if (!this->supported_modes.empty()) { @@ -1125,7 +1125,7 @@ void ListEntitiesClimateResponse::calculate_size(ProtoSize &size) const { } if (!this->supported_custom_fan_modes.empty()) { for (const auto &it : this->supported_custom_fan_modes) { - size.add_string_repeated(1, it); + size.add_length_repeated(1, it.size()); } } if (!this->supported_presets.empty()) { @@ -1135,12 +1135,12 @@ void ListEntitiesClimateResponse::calculate_size(ProtoSize &size) const { } if (!this->supported_custom_presets.empty()) { for (const auto &it : this->supported_custom_presets) { - size.add_string_repeated(2, it); + size.add_length_repeated(2, it.size()); } } size.add_bool(2, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_string(2, this->icon_ref_.size()); + size.add_length(2, this->icon_ref_.size()); #endif size.add_uint32(2, static_cast(this->entity_category)); size.add_float(2, this->visual_current_temperature_step); @@ -1181,9 +1181,9 @@ void ClimateStateResponse::calculate_size(ProtoSize &size) const { size.add_uint32(1, static_cast(this->action)); size.add_uint32(1, static_cast(this->fan_mode)); size.add_uint32(1, static_cast(this->swing_mode)); - size.add_string(1, this->custom_fan_mode_ref_.size()); + size.add_length(1, this->custom_fan_mode_ref_.size()); size.add_uint32(1, static_cast(this->preset)); - size.add_string(1, this->custom_preset_ref_.size()); + size.add_length(1, this->custom_preset_ref_.size()); size.add_float(1, this->current_humidity); size.add_float(1, this->target_humidity); #ifdef USE_DEVICES @@ -1301,20 +1301,20 @@ void ListEntitiesNumberResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesNumberResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_float(1, this->min_value); size.add_float(1, this->max_value); size.add_float(1, this->step); size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); - size.add_string(1, this->unit_of_measurement_ref_.size()); + size.add_length(1, this->unit_of_measurement_ref_.size()); size.add_uint32(1, static_cast(this->mode)); - size.add_string(1, this->device_class_ref_.size()); + size.add_length(1, this->device_class_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -1379,15 +1379,15 @@ void ListEntitiesSelectResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesSelectResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif if (!this->options.empty()) { for (const auto &it : this->options) { - size.add_string_repeated(1, it); + size.add_length_repeated(1, it.size()); } } size.add_bool(1, this->disabled_by_default); @@ -1406,7 +1406,7 @@ void SelectStateResponse::encode(ProtoWriteBuffer buffer) const { } void SelectStateResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); - size.add_string(1, this->state_ref_.size()); + size.add_length(1, this->state_ref_.size()); size.add_bool(1, this->missing_state); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); @@ -1465,16 +1465,16 @@ void ListEntitiesSirenResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesSirenResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); if (!this->tones.empty()) { for (const auto &it : this->tones) { - size.add_string_repeated(1, it); + size.add_length_repeated(1, it.size()); } } size.add_bool(1, this->supports_duration); @@ -1571,18 +1571,18 @@ void ListEntitiesLockResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesLockResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); size.add_bool(1, this->assumed_state); size.add_bool(1, this->supports_open); size.add_bool(1, this->requires_code); - size.add_string(1, this->code_format_ref_.size()); + size.add_length(1, this->code_format_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -1656,15 +1656,15 @@ void ListEntitiesButtonResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesButtonResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); - size.add_string(1, this->device_class_ref_.size()); + size.add_length(1, this->device_class_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -1701,7 +1701,7 @@ void MediaPlayerSupportedFormat::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(5, this->sample_bytes); } void MediaPlayerSupportedFormat::calculate_size(ProtoSize &size) const { - size.add_string(1, this->format_ref_.size()); + size.add_length(1, this->format_ref_.size()); size.add_uint32(1, this->sample_rate); size.add_uint32(1, this->num_channels); size.add_uint32(1, static_cast(this->purpose)); @@ -1725,11 +1725,11 @@ void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesMediaPlayerResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); @@ -1832,7 +1832,7 @@ void BluetoothLERawAdvertisement::calculate_size(ProtoSize &size) const { size.add_uint64(1, this->address); size.add_sint32(1, this->rssi); size.add_uint32(1, this->address_type); - size.add_bytes(1, this->data_len); + size.add_length(1, this->data_len); } void BluetoothLERawAdvertisementsResponse::encode(ProtoWriteBuffer buffer) const { for (auto &it : this->advertisements) { @@ -1956,7 +1956,7 @@ void BluetoothGATTReadResponse::encode(ProtoWriteBuffer buffer) const { void BluetoothGATTReadResponse::calculate_size(ProtoSize &size) const { size.add_uint64(1, this->address); size.add_uint32(1, this->handle); - size.add_bytes(1, this->data_len_); + size.add_length(1, this->data_len_); } bool BluetoothGATTWriteRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2044,7 +2044,7 @@ void BluetoothGATTNotifyDataResponse::encode(ProtoWriteBuffer buffer) const { void BluetoothGATTNotifyDataResponse::calculate_size(ProtoSize &size) const { size.add_uint64(1, this->address); size.add_uint32(1, this->handle); - size.add_bytes(1, this->data_len_); + size.add_length(1, this->data_len_); } void BluetoothConnectionsFreeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(1, this->free); @@ -2170,10 +2170,10 @@ void VoiceAssistantRequest::encode(ProtoWriteBuffer buffer) const { } void VoiceAssistantRequest::calculate_size(ProtoSize &size) const { size.add_bool(1, this->start); - size.add_string(1, this->conversation_id_ref_.size()); + size.add_length(1, this->conversation_id_ref_.size()); size.add_uint32(1, this->flags); size.add_message_object(1, this->audio_settings); - size.add_string(1, this->wake_word_phrase_ref_.size()); + size.add_length(1, this->wake_word_phrase_ref_.size()); } bool VoiceAssistantResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2247,7 +2247,7 @@ void VoiceAssistantAudio::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(2, this->end); } void VoiceAssistantAudio::calculate_size(ProtoSize &size) const { - size.add_bytes(1, this->data_len_); + size.add_length(1, this->data_len_); size.add_bool(1, this->end); } bool VoiceAssistantTimerEventResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -2318,11 +2318,11 @@ void VoiceAssistantWakeWord::encode(ProtoWriteBuffer buffer) const { } } void VoiceAssistantWakeWord::calculate_size(ProtoSize &size) const { - size.add_string(1, this->id_ref_.size()); - size.add_string(1, this->wake_word_ref_.size()); + size.add_length(1, this->id_ref_.size()); + size.add_length(1, this->wake_word_ref_.size()); if (!this->trained_languages.empty()) { for (const auto &it : this->trained_languages) { - size.add_string_repeated(1, it); + size.add_length_repeated(1, it.size()); } } } @@ -2339,7 +2339,7 @@ void VoiceAssistantConfigurationResponse::calculate_size(ProtoSize &size) const size.add_repeated_message(1, this->available_wake_words); if (!this->active_wake_words.empty()) { for (const auto &it : this->active_wake_words) { - size.add_string_repeated(1, it); + size.add_length_repeated(1, it.size()); } } size.add_uint32(1, this->max_active_wake_words); @@ -2373,11 +2373,11 @@ void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer buffer) cons #endif } void ListEntitiesAlarmControlPanelResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); @@ -2457,17 +2457,17 @@ void ListEntitiesTextResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesTextResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); size.add_uint32(1, this->min_length); size.add_uint32(1, this->max_length); - size.add_string(1, this->pattern_ref_.size()); + size.add_length(1, this->pattern_ref_.size()); size.add_uint32(1, static_cast(this->mode)); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); @@ -2483,7 +2483,7 @@ void TextStateResponse::encode(ProtoWriteBuffer buffer) const { } void TextStateResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); - size.add_string(1, this->state_ref_.size()); + size.add_length(1, this->state_ref_.size()); size.add_bool(1, this->missing_state); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); @@ -2537,11 +2537,11 @@ void ListEntitiesDateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesDateResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); @@ -2616,11 +2616,11 @@ void ListEntitiesTimeResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesTimeResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); @@ -2699,18 +2699,18 @@ void ListEntitiesEventResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesEventResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); - size.add_string(1, this->device_class_ref_.size()); + size.add_length(1, this->device_class_ref_.size()); if (!this->event_types.empty()) { for (const auto &it : this->event_types) { - size.add_string_repeated(1, it); + size.add_length_repeated(1, it.size()); } } #ifdef USE_DEVICES @@ -2726,7 +2726,7 @@ void EventResponse::encode(ProtoWriteBuffer buffer) const { } void EventResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->key); - size.add_string(1, this->event_type_ref_.size()); + size.add_length(1, this->event_type_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -2751,15 +2751,15 @@ void ListEntitiesValveResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesValveResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); - size.add_string(1, this->device_class_ref_.size()); + size.add_length(1, this->device_class_ref_.size()); size.add_bool(1, this->assumed_state); size.add_bool(1, this->supports_position); size.add_bool(1, this->supports_stop); @@ -2830,11 +2830,11 @@ void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesDateTimeResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); @@ -2900,15 +2900,15 @@ void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer buffer) const { #endif } void ListEntitiesUpdateResponse::calculate_size(ProtoSize &size) const { - size.add_string(1, this->object_id_ref_.size()); + size.add_length(1, this->object_id_ref_.size()); size.add_fixed32(1, this->key); - size.add_string(1, this->name_ref_.size()); + size.add_length(1, this->name_ref_.size()); #ifdef USE_ENTITY_ICON - size.add_string(1, this->icon_ref_.size()); + size.add_length(1, this->icon_ref_.size()); #endif size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); - size.add_string(1, this->device_class_ref_.size()); + size.add_length(1, this->device_class_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -2934,11 +2934,11 @@ void UpdateStateResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->in_progress); size.add_bool(1, this->has_progress); size.add_float(1, this->progress); - size.add_string(1, this->current_version_ref_.size()); - size.add_string(1, this->latest_version_ref_.size()); - size.add_string(1, this->title_ref_.size()); - size.add_string(1, this->release_summary_ref_.size()); - size.add_string(1, this->release_url_ref_.size()); + size.add_length(1, this->current_version_ref_.size()); + size.add_length(1, this->latest_version_ref_.size()); + size.add_length(1, this->title_ref_.size()); + size.add_length(1, this->release_summary_ref_.size()); + size.add_length(1, this->release_url_ref_.size()); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index aab17bef0b..eaf3f587af 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -686,32 +686,10 @@ class ProtoSize { // sint64 type is not supported by ESPHome API to reduce overhead on embedded systems /** - * @brief Calculates and adds the size of a string field using length + * @brief Calculates and adds the size of a length-delimited field (string/bytes) to the total message size */ - inline void add_string(uint32_t field_id_size, size_t len) { - // Skip calculation if string is empty - if (len == 0) { - return; // No need to update total_size_ - } - - // Field ID + length varint + string bytes - total_size_ += field_id_size + varint(static_cast(len)) + static_cast(len); - } - - /** - * @brief Calculates and adds the size of a string/bytes field to the total message size (repeated field version) - */ - inline void add_string_repeated(uint32_t field_id_size, const std::string &str) { - // Always calculate size for repeated fields - const uint32_t str_size = static_cast(str.size()); - total_size_ += field_id_size + varint(str_size) + str_size; - } - - /** - * @brief Calculates and adds the size of a bytes field to the total message size - */ - inline void add_bytes(uint32_t field_id_size, size_t len) { - // Skip calculation if bytes is empty + inline void add_length(uint32_t field_id_size, size_t len) { + // Skip calculation if length is zero if (len == 0) { return; // No need to update total_size_ } @@ -721,9 +699,10 @@ class ProtoSize { } /** - * @brief Calculates and adds the size of a bytes field to the total message size (repeated field version) + * @brief Calculates and adds the size of a length-delimited field (string/bytes) to the total message size (repeated + * field version) */ - inline void add_bytes_repeated(uint32_t field_id_size, size_t len) { + inline void add_length_repeated(uint32_t field_id_size, size_t len) { // Always calculate size for repeated fields // Field ID + length varint + data bytes total_size_ += field_id_size + varint(static_cast(len)) + static_cast(len); diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 717c1d4009..ce24b2a652 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -624,18 +624,18 @@ class StringType(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: # For SOURCE_CLIENT only messages, use the string field directly if not self._needs_encode: - return self._get_simple_size_calculation(name, force, "add_string") + return self._get_simple_size_calculation(name, force, "add_length") # Check if this is being called from a repeated field context # In that case, 'name' will be 'it' and we need to use the repeated version if name == "it": - # For repeated fields, we need to use add_string_repeated which includes field ID + # For repeated fields, we need to use add_length_repeated which includes field ID field_id_size = self.calculate_field_id_size() - return f"size.add_string_repeated({field_id_size}, it);" + return f"size.add_length_repeated({field_id_size}, it.size());" # For messages that need encoding, use the StringRef size field_id_size = self.calculate_field_id_size() - return f"size.add_string({field_id_size}, this->{self.field_name}_ref_.size());" + return f"size.add_length({field_id_size}, this->{self.field_name}_ref_.size());" def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 8 # field ID + 8 bytes typical string @@ -770,7 +770,7 @@ class BytesType(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return f"size.add_bytes({self.calculate_field_id_size()}, this->{self.field_name}_len_);" + return f"size.add_length({self.calculate_field_id_size()}, this->{self.field_name}_len_);" def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 8 # field ID + 8 bytes typical bytes @@ -846,10 +846,10 @@ class FixedArrayBytesType(TypeInfo): if force: # For repeated fields, always calculate size (no zero check) - return f"size.add_bytes_repeated({field_id_size}, {length_field});" + return f"size.add_length_repeated({field_id_size}, {length_field});" else: - # For non-repeated fields, add_bytes already checks for zero - return f"size.add_bytes({field_id_size}, {length_field});" + # For non-repeated fields, add_length already checks for zero + return f"size.add_length({field_id_size}, {length_field});" def get_estimated_size(self) -> int: # Estimate based on typical BLE advertisement size From 2e16b3ea312a6baeca01e9f5d5e6e278626e19df Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 14:52:15 -1000 Subject: [PATCH 07/24] dry --- esphome/components/api/proto.h | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index eaf3f587af..03fa5e0452 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -541,9 +541,8 @@ class ProtoSize { if (value == 0) { return; // No need to update total_size_ } - - // Calculate and directly add to total_size - total_size_ += field_id_size + varint(value); + // Delegate to repeated version + add_uint32_repeated(field_id_size, value); } /** @@ -622,10 +621,8 @@ class ProtoSize { if (value == 0) { return; // No need to update total_size_ } - - // ZigZag encoding for sint32: (n << 1) ^ (n >> 31) - uint32_t zigzag = (static_cast(value) << 1) ^ (static_cast(value >> 31)); - total_size_ += field_id_size + varint(zigzag); + // Delegate to repeated version + add_sint32_repeated(field_id_size, value); } /** @@ -648,9 +645,8 @@ class ProtoSize { if (value == 0) { return; // No need to update total_size_ } - - // Calculate and directly add to total_size - total_size_ += field_id_size + varint(value); + // Delegate to repeated version + add_int64_repeated(field_id_size, value); } /** @@ -669,9 +665,8 @@ class ProtoSize { if (value == 0) { return; // No need to update total_size_ } - - // Calculate and directly add to total_size - total_size_ += field_id_size + varint(value); + // Delegate to repeated version + add_uint64_repeated(field_id_size, value); } /** @@ -693,9 +688,8 @@ class ProtoSize { if (len == 0) { return; // No need to update total_size_ } - - // Field ID + length varint + data bytes - total_size_ += field_id_size + varint(static_cast(len)) + static_cast(len); + // Delegate to repeated version + add_length_repeated(field_id_size, len); } /** From ae120976360577e1fdd39c1b7dffbd89898688b8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 14:53:14 -1000 Subject: [PATCH 08/24] dry --- esphome/components/api/proto.h | 35 ++++++++++------------------------ 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 03fa5e0452..b1dbcd1ac1 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -537,12 +537,9 @@ class ProtoSize { * @brief Calculates and adds the size of a uint32 field to the total message size */ inline void add_uint32(uint32_t field_id_size, uint32_t value) { - // Skip calculation if value is zero - if (value == 0) { - return; // No need to update total_size_ + if (value != 0) { + add_uint32_repeated(field_id_size, value); } - // Delegate to repeated version - add_uint32_repeated(field_id_size, value); } /** @@ -617,12 +614,9 @@ class ProtoSize { * Sint32 fields use ZigZag encoding, which is more efficient for negative values. */ inline void add_sint32(uint32_t field_id_size, int32_t value) { - // Skip calculation if value is zero - if (value == 0) { - return; // No need to update total_size_ + if (value != 0) { + add_sint32_repeated(field_id_size, value); } - // Delegate to repeated version - add_sint32_repeated(field_id_size, value); } /** @@ -641,12 +635,9 @@ class ProtoSize { * @brief Calculates and adds the size of an int64 field to the total message size */ inline void add_int64(uint32_t field_id_size, int64_t value) { - // Skip calculation if value is zero - if (value == 0) { - return; // No need to update total_size_ + if (value != 0) { + add_int64_repeated(field_id_size, value); } - // Delegate to repeated version - add_int64_repeated(field_id_size, value); } /** @@ -661,12 +652,9 @@ class ProtoSize { * @brief Calculates and adds the size of a uint64 field to the total message size */ inline void add_uint64(uint32_t field_id_size, uint64_t value) { - // Skip calculation if value is zero - if (value == 0) { - return; // No need to update total_size_ + if (value != 0) { + add_uint64_repeated(field_id_size, value); } - // Delegate to repeated version - add_uint64_repeated(field_id_size, value); } /** @@ -684,12 +672,9 @@ class ProtoSize { * @brief Calculates and adds the size of a length-delimited field (string/bytes) to the total message size */ inline void add_length(uint32_t field_id_size, size_t len) { - // Skip calculation if length is zero - if (len == 0) { - return; // No need to update total_size_ + if (len != 0) { + add_length_repeated(field_id_size, len); } - // Delegate to repeated version - add_length_repeated(field_id_size, len); } /** From 32edc3f062d489565fccfb51ad81570b155b4ef7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 14:54:52 -1000 Subject: [PATCH 09/24] dry --- esphome/components/api/api_pb2.cpp | 66 +++++++++++++-------------- esphome/components/api/proto.h | 70 ++++++++++++++--------------- script/api_protobuf/api_protobuf.py | 10 ++--- 3 files changed, 73 insertions(+), 73 deletions(-) diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index e3794ae56b..c91dc5ea04 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -167,10 +167,10 @@ void DeviceInfoResponse::calculate_size(ProtoSize &size) const { size.add_bool(2, this->api_encryption_supported); #endif #ifdef USE_DEVICES - size.add_repeated_message(2, this->devices); + size.add_force_message(2, this->devices); #endif #ifdef USE_AREAS - size.add_repeated_message(2, this->areas); + size.add_force_message(2, this->areas); #endif #ifdef USE_AREAS size.add_message_object(2, this->area); @@ -353,7 +353,7 @@ void ListEntitiesFanResponse::calculate_size(ProtoSize &size) const { size.add_uint32(1, static_cast(this->entity_category)); if (!this->supported_preset_modes.empty()) { for (const auto &it : this->supported_preset_modes) { - size.add_length_repeated(1, it.size()); + size.add_length_force(1, it.size()); } } #ifdef USE_DEVICES @@ -470,14 +470,14 @@ void ListEntitiesLightResponse::calculate_size(ProtoSize &size) const { size.add_length(1, this->name_ref_.size()); if (!this->supported_color_modes.empty()) { for (const auto &it : this->supported_color_modes) { - size.add_uint32_repeated(1, static_cast(it)); + size.add_uint32_force(1, static_cast(it)); } } size.add_float(1, this->min_mireds); size.add_float(1, this->max_mireds); if (!this->effects.empty()) { for (const auto &it : this->effects) { - size.add_length_repeated(1, it.size()); + size.add_length_force(1, it.size()); } } size.add_bool(1, this->disabled_by_default); @@ -862,9 +862,9 @@ void HomeassistantServiceResponse::encode(ProtoWriteBuffer buffer) const { } void HomeassistantServiceResponse::calculate_size(ProtoSize &size) const { size.add_length(1, this->service_ref_.size()); - size.add_repeated_message(1, this->data); - size.add_repeated_message(1, this->data_template); - size.add_repeated_message(1, this->variables); + size.add_force_message(1, this->data); + size.add_force_message(1, this->data_template); + size.add_force_message(1, this->variables); size.add_bool(1, this->is_event); } #ifdef USE_API_HOMEASSISTANT_STATES @@ -926,7 +926,7 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const { void ListEntitiesServicesResponse::calculate_size(ProtoSize &size) const { size.add_length(1, this->name_ref_.size()); size.add_fixed32(1, this->key); - size.add_repeated_message(1, this->args); + size.add_force_message(1, this->args); } bool ExecuteServiceArgument::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1106,7 +1106,7 @@ void ListEntitiesClimateResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->supports_two_point_target_temperature); if (!this->supported_modes.empty()) { for (const auto &it : this->supported_modes) { - size.add_uint32_repeated(1, static_cast(it)); + size.add_uint32_force(1, static_cast(it)); } } size.add_float(1, this->visual_min_temperature); @@ -1115,27 +1115,27 @@ void ListEntitiesClimateResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->supports_action); if (!this->supported_fan_modes.empty()) { for (const auto &it : this->supported_fan_modes) { - size.add_uint32_repeated(1, static_cast(it)); + size.add_uint32_force(1, static_cast(it)); } } if (!this->supported_swing_modes.empty()) { for (const auto &it : this->supported_swing_modes) { - size.add_uint32_repeated(1, static_cast(it)); + size.add_uint32_force(1, static_cast(it)); } } if (!this->supported_custom_fan_modes.empty()) { for (const auto &it : this->supported_custom_fan_modes) { - size.add_length_repeated(1, it.size()); + size.add_length_force(1, it.size()); } } if (!this->supported_presets.empty()) { for (const auto &it : this->supported_presets) { - size.add_uint32_repeated(2, static_cast(it)); + size.add_uint32_force(2, static_cast(it)); } } if (!this->supported_custom_presets.empty()) { for (const auto &it : this->supported_custom_presets) { - size.add_length_repeated(2, it.size()); + size.add_length_force(2, it.size()); } } size.add_bool(2, this->disabled_by_default); @@ -1387,7 +1387,7 @@ void ListEntitiesSelectResponse::calculate_size(ProtoSize &size) const { #endif if (!this->options.empty()) { for (const auto &it : this->options) { - size.add_length_repeated(1, it.size()); + size.add_length_force(1, it.size()); } } size.add_bool(1, this->disabled_by_default); @@ -1474,7 +1474,7 @@ void ListEntitiesSirenResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->disabled_by_default); if (!this->tones.empty()) { for (const auto &it : this->tones) { - size.add_length_repeated(1, it.size()); + size.add_length_force(1, it.size()); } } size.add_bool(1, this->supports_duration); @@ -1734,7 +1734,7 @@ void ListEntitiesMediaPlayerResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); size.add_bool(1, this->supports_pause); - size.add_repeated_message(1, this->supported_formats); + size.add_force_message(1, this->supported_formats); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -1840,7 +1840,7 @@ void BluetoothLERawAdvertisementsResponse::encode(ProtoWriteBuffer buffer) const } } void BluetoothLERawAdvertisementsResponse::calculate_size(ProtoSize &size) const { - size.add_repeated_message(1, this->advertisements); + size.add_force_message(1, this->advertisements); } bool BluetoothDeviceRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1889,8 +1889,8 @@ void BluetoothGATTDescriptor::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(2, this->handle); } void BluetoothGATTDescriptor::calculate_size(ProtoSize &size) const { - size.add_uint64_repeated(1, this->uuid[0]); - size.add_uint64_repeated(1, this->uuid[1]); + size.add_uint64_force(1, this->uuid[0]); + size.add_uint64_force(1, this->uuid[1]); size.add_uint32(1, this->handle); } void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer buffer) const { @@ -1903,11 +1903,11 @@ void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer buffer) const { } } void BluetoothGATTCharacteristic::calculate_size(ProtoSize &size) const { - size.add_uint64_repeated(1, this->uuid[0]); - size.add_uint64_repeated(1, this->uuid[1]); + size.add_uint64_force(1, this->uuid[0]); + size.add_uint64_force(1, this->uuid[1]); size.add_uint32(1, this->handle); size.add_uint32(1, this->properties); - size.add_repeated_message(1, this->descriptors); + size.add_force_message(1, this->descriptors); } void BluetoothGATTService::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->uuid[0], true); @@ -1918,10 +1918,10 @@ void BluetoothGATTService::encode(ProtoWriteBuffer buffer) const { } } void BluetoothGATTService::calculate_size(ProtoSize &size) const { - size.add_uint64_repeated(1, this->uuid[0]); - size.add_uint64_repeated(1, this->uuid[1]); + size.add_uint64_force(1, this->uuid[0]); + size.add_uint64_force(1, this->uuid[1]); size.add_uint32(1, this->handle); - size.add_repeated_message(1, this->characteristics); + size.add_force_message(1, this->characteristics); } void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); @@ -1929,7 +1929,7 @@ void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer buffer) const { } void BluetoothGATTGetServicesResponse::calculate_size(ProtoSize &size) const { size.add_uint64(1, this->address); - size.add_message_object_repeated(1, this->services[0]); + size.add_message_object_force(1, this->services[0]); } void BluetoothGATTGetServicesDoneResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); @@ -2058,7 +2058,7 @@ void BluetoothConnectionsFreeResponse::calculate_size(ProtoSize &size) const { size.add_uint32(1, this->limit); if (!this->allocated.empty()) { for (const auto &it : this->allocated) { - size.add_uint64_repeated(1, it); + size.add_uint64_force(1, it); } } } @@ -2322,7 +2322,7 @@ void VoiceAssistantWakeWord::calculate_size(ProtoSize &size) const { size.add_length(1, this->wake_word_ref_.size()); if (!this->trained_languages.empty()) { for (const auto &it : this->trained_languages) { - size.add_length_repeated(1, it.size()); + size.add_length_force(1, it.size()); } } } @@ -2336,10 +2336,10 @@ void VoiceAssistantConfigurationResponse::encode(ProtoWriteBuffer buffer) const buffer.encode_uint32(3, this->max_active_wake_words); } void VoiceAssistantConfigurationResponse::calculate_size(ProtoSize &size) const { - size.add_repeated_message(1, this->available_wake_words); + size.add_force_message(1, this->available_wake_words); if (!this->active_wake_words.empty()) { for (const auto &it : this->active_wake_words) { - size.add_length_repeated(1, it.size()); + size.add_length_force(1, it.size()); } } size.add_uint32(1, this->max_active_wake_words); @@ -2710,7 +2710,7 @@ void ListEntitiesEventResponse::calculate_size(ProtoSize &size) const { size.add_length(1, this->device_class_ref_.size()); if (!this->event_types.empty()) { for (const auto &it : this->event_types) { - size.add_length_repeated(1, it.size()); + size.add_length_force(1, it.size()); } } #ifdef USE_DEVICES diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index b1dbcd1ac1..da7d236ff1 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -520,10 +520,10 @@ class ProtoSize { } /** - * @brief Calculates and adds the size of an int32 field to the total message size (repeated field version) + * @brief Calculates and adds the size of an int32 field to the total message size (force version) */ - inline void add_int32_repeated(uint32_t field_id_size, int32_t value) { - // Always calculate size for repeated fields + inline void add_int32_force(uint32_t field_id_size, int32_t value) { + // Always calculate size when force is true if (value < 0) { // Negative values are encoded as 10-byte varints in protobuf total_size_ += field_id_size + 10; @@ -538,15 +538,15 @@ class ProtoSize { */ inline void add_uint32(uint32_t field_id_size, uint32_t value) { if (value != 0) { - add_uint32_repeated(field_id_size, value); + add_uint32_force(field_id_size, value); } } /** - * @brief Calculates and adds the size of a uint32 field to the total message size (repeated field version) + * @brief Calculates and adds the size of a uint32 field to the total message size (force version) */ - inline void add_uint32_repeated(uint32_t field_id_size, uint32_t value) { - // Always calculate size for repeated fields + inline void add_uint32_force(uint32_t field_id_size, uint32_t value) { + // Always calculate size when force is true total_size_ += field_id_size + varint(value); } @@ -564,10 +564,10 @@ class ProtoSize { } /** - * @brief Calculates and adds the size of a boolean field to the total message size (repeated field version) + * @brief Calculates and adds the size of a boolean field to the total message size (force version) */ - inline void add_bool_repeated(uint32_t field_id_size, bool value) { - // Always calculate size for repeated fields + inline void add_bool_force(uint32_t field_id_size, bool value) { + // Always calculate size when force is true // Boolean fields always use 1 byte total_size_ += field_id_size + 1; } @@ -615,17 +615,17 @@ class ProtoSize { */ inline void add_sint32(uint32_t field_id_size, int32_t value) { if (value != 0) { - add_sint32_repeated(field_id_size, value); + add_sint32_force(field_id_size, value); } } /** - * @brief Calculates and adds the size of a sint32 field to the total message size (repeated field version) + * @brief Calculates and adds the size of a sint32 field to the total message size (force version) * * Sint32 fields use ZigZag encoding, which is more efficient for negative values. */ - inline void add_sint32_repeated(uint32_t field_id_size, int32_t value) { - // Always calculate size for repeated fields + inline void add_sint32_force(uint32_t field_id_size, int32_t value) { + // Always calculate size when force is true // ZigZag encoding for sint32: (n << 1) ^ (n >> 31) uint32_t zigzag = (static_cast(value) << 1) ^ (static_cast(value >> 31)); total_size_ += field_id_size + varint(zigzag); @@ -636,15 +636,15 @@ class ProtoSize { */ inline void add_int64(uint32_t field_id_size, int64_t value) { if (value != 0) { - add_int64_repeated(field_id_size, value); + add_int64_force(field_id_size, value); } } /** - * @brief Calculates and adds the size of an int64 field to the total message size (repeated field version) + * @brief Calculates and adds the size of an int64 field to the total message size (force version) */ - inline void add_int64_repeated(uint32_t field_id_size, int64_t value) { - // Always calculate size for repeated fields + inline void add_int64_force(uint32_t field_id_size, int64_t value) { + // Always calculate size when force is true total_size_ += field_id_size + varint(value); } @@ -653,19 +653,19 @@ class ProtoSize { */ inline void add_uint64(uint32_t field_id_size, uint64_t value) { if (value != 0) { - add_uint64_repeated(field_id_size, value); + add_uint64_force(field_id_size, value); } } /** - * @brief Calculates and adds the size of a uint64 field to the total message size (repeated field version) + * @brief Calculates and adds the size of a uint64 field to the total message size (force version) */ - inline void add_uint64_repeated(uint32_t field_id_size, uint64_t value) { - // Always calculate size for repeated fields + inline void add_uint64_force(uint32_t field_id_size, uint64_t value) { + // Always calculate size when force is true total_size_ += field_id_size + varint(value); } - // NOTE: sint64 support functions (add_sint64_field, add_sint64_field_repeated) removed + // NOTE: sint64 support functions (add_sint64_field, add_sint64_field_force) removed // sint64 type is not supported by ESPHome API to reduce overhead on embedded systems /** @@ -673,7 +673,7 @@ class ProtoSize { */ inline void add_length(uint32_t field_id_size, size_t len) { if (len != 0) { - add_length_repeated(field_id_size, len); + add_length_force(field_id_size, len); } } @@ -681,8 +681,8 @@ class ProtoSize { * @brief Calculates and adds the size of a length-delimited field (string/bytes) to the total message size (repeated * field version) */ - inline void add_length_repeated(uint32_t field_id_size, size_t len) { - // Always calculate size for repeated fields + inline void add_length_force(uint32_t field_id_size, size_t len) { + // Always calculate size when force is true // Field ID + length varint + data bytes total_size_ += field_id_size + varint(static_cast(len)) + static_cast(len); } @@ -717,12 +717,12 @@ class ProtoSize { } /** - * @brief Calculates and adds the size of a nested message field to the total message size (repeated field version) + * @brief Calculates and adds the size of a nested message field to the total message size (force version) * * @param nested_size The pre-calculated size of the nested message */ - inline void add_message_field_repeated(uint32_t field_id_size, uint32_t nested_size) { - // Always calculate size for repeated fields + inline void add_message_field_force(uint32_t field_id_size, uint32_t nested_size) { + // Always calculate size when force is true // Field ID + length varint + nested message content total_size_ += field_id_size + varint(nested_size) + nested_size; } @@ -747,18 +747,18 @@ class ProtoSize { } /** - * @brief Calculates and adds the size of a nested message field to the total message size (repeated field version) + * @brief Calculates and adds the size of a nested message field to the total message size (force version) * * @param message The nested message object */ - inline void add_message_object_repeated(uint32_t field_id_size, const ProtoMessage &message) { + inline void add_message_object_force(uint32_t field_id_size, const ProtoMessage &message) { // Calculate nested message size by creating a temporary ProtoSize ProtoSize nested_calc; message.calculate_size(nested_calc); uint32_t nested_size = nested_calc.get_size(); // Use the base implementation with the calculated nested_size - add_message_field_repeated(field_id_size, nested_size); + add_message_field_force(field_id_size, nested_size); } /** @@ -771,15 +771,15 @@ class ProtoSize { * @param messages Vector of message objects */ template - inline void add_repeated_message(uint32_t field_id_size, const std::vector &messages) { + inline void add_force_message(uint32_t field_id_size, const std::vector &messages) { // Skip if the vector is empty if (messages.empty()) { return; } - // Use the repeated field version for all messages + // Use the force version for all messages for (const auto &message : messages) { - add_message_object_repeated(field_id_size, message); + add_message_object_force(field_id_size, message); } } }; diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index ce24b2a652..171468410f 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -279,7 +279,7 @@ class TypeInfo(ABC): value_expr: Optional value expression (defaults to name) """ field_id_size = self.calculate_field_id_size() - method = f"{base_method}_repeated" if force else base_method + method = f"{base_method}_force" if force else base_method value = value_expr if value_expr else name return f"size.{method}({field_id_size}, {value});" @@ -629,9 +629,9 @@ class StringType(TypeInfo): # Check if this is being called from a repeated field context # In that case, 'name' will be 'it' and we need to use the repeated version if name == "it": - # For repeated fields, we need to use add_length_repeated which includes field ID + # For repeated fields, we need to use add_length_force which includes field ID field_id_size = self.calculate_field_id_size() - return f"size.add_length_repeated({field_id_size}, it.size());" + return f"size.add_length_force({field_id_size}, it.size());" # For messages that need encoding, use the StringRef size field_id_size = self.calculate_field_id_size() @@ -846,7 +846,7 @@ class FixedArrayBytesType(TypeInfo): if force: # For repeated fields, always calculate size (no zero check) - return f"size.add_length_repeated({field_id_size}, {length_field});" + return f"size.add_length_force({field_id_size}, {length_field});" else: # For non-repeated fields, add_length already checks for zero return f"size.add_length({field_id_size}, {length_field});" @@ -1236,7 +1236,7 @@ class RepeatedTypeInfo(TypeInfo): if isinstance(self._ti, MessageType): # For repeated messages, use the dedicated helper that handles iteration internally field_id_size = self._ti.calculate_field_id_size() - o = f"size.add_repeated_message({field_id_size}, {name});" + o = f"size.add_force_message({field_id_size}, {name});" return o # For other repeated types, use the underlying type's size calculation with force=True From 0773fc320b3b4999150f961159d76ab31f93cb4f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 14:57:42 -1000 Subject: [PATCH 10/24] dry --- esphome/components/api/proto.h | 4 ++-- script/api_protobuf/api_protobuf.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index da7d236ff1..52733aaf7e 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -771,13 +771,13 @@ class ProtoSize { * @param messages Vector of message objects */ template - inline void add_force_message(uint32_t field_id_size, const std::vector &messages) { + inline void add_repeated_message(uint32_t field_id_size, const std::vector &messages) { // Skip if the vector is empty if (messages.empty()) { return; } - // Use the force version for all messages + // Use the force version for all messages in the repeated field for (const auto &message : messages) { add_message_object_force(field_id_size, message); } diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 171468410f..f0b636b775 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -1236,7 +1236,7 @@ class RepeatedTypeInfo(TypeInfo): if isinstance(self._ti, MessageType): # For repeated messages, use the dedicated helper that handles iteration internally field_id_size = self._ti.calculate_field_id_size() - o = f"size.add_force_message({field_id_size}, {name});" + o = f"size.add_repeated_message({field_id_size}, {name});" return o # For other repeated types, use the underlying type's size calculation with force=True From 193a85eb1cce5d3d9d985105c0bed871f2c0872a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 14:57:50 -1000 Subject: [PATCH 11/24] dry --- esphome/components/api/api_pb2.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index c91dc5ea04..96d134fa14 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -167,10 +167,10 @@ void DeviceInfoResponse::calculate_size(ProtoSize &size) const { size.add_bool(2, this->api_encryption_supported); #endif #ifdef USE_DEVICES - size.add_force_message(2, this->devices); + size.add_repeated_message(2, this->devices); #endif #ifdef USE_AREAS - size.add_force_message(2, this->areas); + size.add_repeated_message(2, this->areas); #endif #ifdef USE_AREAS size.add_message_object(2, this->area); @@ -862,9 +862,9 @@ void HomeassistantServiceResponse::encode(ProtoWriteBuffer buffer) const { } void HomeassistantServiceResponse::calculate_size(ProtoSize &size) const { size.add_length(1, this->service_ref_.size()); - size.add_force_message(1, this->data); - size.add_force_message(1, this->data_template); - size.add_force_message(1, this->variables); + size.add_repeated_message(1, this->data); + size.add_repeated_message(1, this->data_template); + size.add_repeated_message(1, this->variables); size.add_bool(1, this->is_event); } #ifdef USE_API_HOMEASSISTANT_STATES @@ -926,7 +926,7 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const { void ListEntitiesServicesResponse::calculate_size(ProtoSize &size) const { size.add_length(1, this->name_ref_.size()); size.add_fixed32(1, this->key); - size.add_force_message(1, this->args); + size.add_repeated_message(1, this->args); } bool ExecuteServiceArgument::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1734,7 +1734,7 @@ void ListEntitiesMediaPlayerResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->disabled_by_default); size.add_uint32(1, static_cast(this->entity_category)); size.add_bool(1, this->supports_pause); - size.add_force_message(1, this->supported_formats); + size.add_repeated_message(1, this->supported_formats); #ifdef USE_DEVICES size.add_uint32(1, this->device_id); #endif @@ -1840,7 +1840,7 @@ void BluetoothLERawAdvertisementsResponse::encode(ProtoWriteBuffer buffer) const } } void BluetoothLERawAdvertisementsResponse::calculate_size(ProtoSize &size) const { - size.add_force_message(1, this->advertisements); + size.add_repeated_message(1, this->advertisements); } bool BluetoothDeviceRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1907,7 +1907,7 @@ void BluetoothGATTCharacteristic::calculate_size(ProtoSize &size) const { size.add_uint64_force(1, this->uuid[1]); size.add_uint32(1, this->handle); size.add_uint32(1, this->properties); - size.add_force_message(1, this->descriptors); + size.add_repeated_message(1, this->descriptors); } void BluetoothGATTService::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->uuid[0], true); @@ -1921,7 +1921,7 @@ void BluetoothGATTService::calculate_size(ProtoSize &size) const { size.add_uint64_force(1, this->uuid[0]); size.add_uint64_force(1, this->uuid[1]); size.add_uint32(1, this->handle); - size.add_force_message(1, this->characteristics); + size.add_repeated_message(1, this->characteristics); } void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); @@ -2336,7 +2336,7 @@ void VoiceAssistantConfigurationResponse::encode(ProtoWriteBuffer buffer) const buffer.encode_uint32(3, this->max_active_wake_words); } void VoiceAssistantConfigurationResponse::calculate_size(ProtoSize &size) const { - size.add_force_message(1, this->available_wake_words); + size.add_repeated_message(1, this->available_wake_words); if (!this->active_wake_words.empty()) { for (const auto &it : this->active_wake_words) { size.add_length_force(1, it.size()); From a82b5fa87a9238670facef2ece72c9920ab9eb65 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 15:01:07 -1000 Subject: [PATCH 12/24] dry --- esphome/components/api/proto.h | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 52733aaf7e..63fdcce126 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -504,18 +504,8 @@ class ProtoSize { * @brief Calculates and adds the size of an int32 field to the total message size */ inline void add_int32(uint32_t field_id_size, int32_t value) { - // Skip calculation if value is zero - if (value == 0) { - return; // No need to update total_size_ - } - - // Calculate and directly add to total_size - if (value < 0) { - // Negative values are encoded as 10-byte varints in protobuf - total_size_ += field_id_size + 10; - } else { - // For non-negative values, use the standard varint size - total_size_ += field_id_size + varint(static_cast(value)); + if (value != 0) { + add_int32_force(field_id_size, value); } } @@ -554,13 +544,10 @@ class ProtoSize { * @brief Calculates and adds the size of a boolean field to the total message size */ inline void add_bool(uint32_t field_id_size, bool value) { - // Skip calculation if value is false - if (!value) { - return; // No need to update total_size_ + if (value) { + // Boolean fields always use 1 byte when true + total_size_ += field_id_size + 1; } - - // Boolean fields always use 1 byte when true - total_size_ += field_id_size + 1; } /** @@ -706,14 +693,10 @@ class ProtoSize { * @param nested_size The pre-calculated size of the nested message */ inline void add_message_field(uint32_t field_id_size, uint32_t nested_size) { - // Skip calculation if nested message is empty - if (nested_size == 0) { - return; // No need to update total_size_ + if (nested_size != 0) { + // Field ID + length varint + nested message content + total_size_ += field_id_size + varint(nested_size) + nested_size; } - - // Calculate and directly add to total_size - // Field ID + length varint + nested message content - total_size_ += field_id_size + varint(nested_size) + nested_size; } /** From 5ebce4a901f18ee93398308df4f04acfe5681974 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 15:01:45 -1000 Subject: [PATCH 13/24] dry --- esphome/components/api/proto.h | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 63fdcce126..bfca80960f 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -513,14 +513,9 @@ class ProtoSize { * @brief Calculates and adds the size of an int32 field to the total message size (force version) */ inline void add_int32_force(uint32_t field_id_size, int32_t value) { - // Always calculate size when force is true - if (value < 0) { - // Negative values are encoded as 10-byte varints in protobuf - total_size_ += field_id_size + 10; - } else { - // For non-negative values, use the standard varint size - total_size_ += field_id_size + varint(static_cast(value)); - } + // Always calculate size when forced + // Negative values are encoded as 10-byte varints in protobuf + total_size_ += field_id_size + (value < 0 ? 10 : varint(static_cast(value))); } /** From 6e345c5f23e7fa04ed74e9693e38c100fd611ce8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Jul 2025 15:03:36 -1000 Subject: [PATCH 14/24] dry --- esphome/components/api/proto.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index bfca80960f..3d66859bd3 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -689,8 +689,7 @@ class ProtoSize { */ inline void add_message_field(uint32_t field_id_size, uint32_t nested_size) { if (nested_size != 0) { - // Field ID + length varint + nested message content - total_size_ += field_id_size + varint(nested_size) + nested_size; + add_message_field_force(field_id_size, nested_size); } } From a2ed20954275ca40514a60bcfce583a1c545b9dc Mon Sep 17 00:00:00 2001 From: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com> Date: Sun, 27 Jul 2025 17:57:37 +1000 Subject: [PATCH 15/24] [wifi] Disallow psram config with arduino (#9922) --- esphome/components/wifi/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/esphome/components/wifi/__init__.py b/esphome/components/wifi/__init__.py index 4e1d2998f6..26195694e7 100644 --- a/esphome/components/wifi/__init__.py +++ b/esphome/components/wifi/__init__.py @@ -6,6 +6,7 @@ from esphome.components.esp32 import add_idf_sdkconfig_option, const, get_esp32_ from esphome.components.network import IPAddress from esphome.config_helpers import filter_source_files_from_platform import esphome.config_validation as cv +from esphome.config_validation import only_with_esp_idf from esphome.const import ( CONF_AP, CONF_BSSID, @@ -336,7 +337,7 @@ CONFIG_SCHEMA = cv.All( single=True ), cv.Optional(CONF_USE_PSRAM): cv.All( - cv.requires_component("psram"), cv.boolean + only_with_esp_idf, cv.requires_component("psram"), cv.boolean ), } ), From 20b61d4bdb83c695b068667bd7e17079ce2c7190 Mon Sep 17 00:00:00 2001 From: cryptk <421501+cryptk@users.noreply.github.com> Date: Sun, 27 Jul 2025 21:20:51 -0500 Subject: [PATCH 16/24] Add seed flag when running setup with uv present (#9932) --- script/setup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/setup b/script/setup index b17d3235a7..1bd7c44575 100755 --- a/script/setup +++ b/script/setup @@ -6,7 +6,7 @@ set -e cd "$(dirname "$0")/.." if [ ! -n "$VIRTUAL_ENV" ]; then if [ -x "$(command -v uv)" ]; then - uv venv venv + uv venv --seed venv else python3 -m venv venv fi From a3690422bf08550873d03e34b8ad75b5024bb910 Mon Sep 17 00:00:00 2001 From: Jimmy Hedman Date: Mon, 28 Jul 2025 05:19:17 +0200 Subject: [PATCH 17/24] Fail with old lerp (#9914) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/core/helpers.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index f67f13b71f..5204804e1e 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -68,7 +68,10 @@ To bit_cast(const From &src) { return dst; } #endif -using std::lerp; + +// clang-format off +inline float lerp(float completion, float start, float end) = delete; // Please use std::lerp. Notice that it has different order on arguments! +// clang-format on // std::byteswap from C++23 template constexpr T byteswap(T n) { From 6ed9214465dd6ddc573a619a46221e4319e6ad43 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 27 Jul 2025 17:20:30 -1000 Subject: [PATCH 18/24] [core] Use nullptr defaults in status_set_error/warning to reduce flash usage (#9931) --- esphome/core/component.cpp | 12 +++++++----- esphome/core/component.h | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/esphome/core/component.cpp b/esphome/core/component.cpp index 42b0a71d79..513b0a7ba2 100644 --- a/esphome/core/component.cpp +++ b/esphome/core/component.cpp @@ -16,6 +16,7 @@ namespace esphome { static const char *const TAG = "component"; +static const char *const UNSPECIFIED_MESSAGE = "unspecified"; // Global vectors for component data that doesn't belong in every instance. // Using vector instead of unordered_map for both because: @@ -132,7 +133,7 @@ void Component::call_dump_config() { this->dump_config(); if (this->is_failed()) { // Look up error message from global vector - const char *error_msg = "unspecified"; + const char *error_msg = nullptr; if (component_error_messages) { for (const auto &pair : *component_error_messages) { if (pair.first == this) { @@ -141,7 +142,8 @@ void Component::call_dump_config() { } } } - ESP_LOGE(TAG, " %s is marked FAILED: %s", this->get_component_source(), error_msg); + ESP_LOGE(TAG, " %s is marked FAILED: %s", this->get_component_source(), + error_msg ? error_msg : UNSPECIFIED_MESSAGE); } } @@ -284,15 +286,15 @@ void Component::status_set_warning(const char *message) { return; this->component_state_ |= STATUS_LED_WARNING; App.app_state_ |= STATUS_LED_WARNING; - ESP_LOGW(TAG, "%s set Warning flag: %s", this->get_component_source(), message); + ESP_LOGW(TAG, "%s set Warning flag: %s", this->get_component_source(), message ? message : UNSPECIFIED_MESSAGE); } void Component::status_set_error(const char *message) { if ((this->component_state_ & STATUS_LED_ERROR) != 0) return; this->component_state_ |= STATUS_LED_ERROR; App.app_state_ |= STATUS_LED_ERROR; - ESP_LOGE(TAG, "%s set Error flag: %s", this->get_component_source(), message); - if (strcmp(message, "unspecified") != 0) { + ESP_LOGE(TAG, "%s set Error flag: %s", this->get_component_source(), message ? message : UNSPECIFIED_MESSAGE); + if (message != nullptr) { // Lazy allocate the error messages vector if needed if (!component_error_messages) { component_error_messages = std::make_unique>>(); diff --git a/esphome/core/component.h b/esphome/core/component.h index 5f17c1c22a..096c6f9c69 100644 --- a/esphome/core/component.h +++ b/esphome/core/component.h @@ -202,9 +202,9 @@ class Component { bool status_has_error() const; - void status_set_warning(const char *message = "unspecified"); + void status_set_warning(const char *message = nullptr); - void status_set_error(const char *message = "unspecified"); + void status_set_error(const char *message = nullptr); void status_clear_warning(); From 84ed1bcf34b95d4774d7f4218eeb98e859f761ac Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 27 Jul 2025 17:22:56 -1000 Subject: [PATCH 19/24] [light] Reduce flash usage by 832 bytes through code optimization (#9924) --- esphome/components/light/light_call.cpp | 95 +++++++++++++------ esphome/components/light/light_color_values.h | 27 +++--- .../components/light/light_json_schema.cpp | 90 +++++++++--------- esphome/components/light/light_state.cpp | 17 ++-- 4 files changed, 134 insertions(+), 95 deletions(-) diff --git a/esphome/components/light/light_call.cpp b/esphome/components/light/light_call.cpp index 1b856ad580..60945531cf 100644 --- a/esphome/components/light/light_call.cpp +++ b/esphome/components/light/light_call.cpp @@ -9,11 +9,28 @@ namespace light { static const char *const TAG = "light"; -// Helper function to reduce code size for validation warnings +// Helper functions to reduce code size for logging +#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN static void log_validation_warning(const char *name, const char *param_name, float val, float min, float max) { ESP_LOGW(TAG, "'%s': %s value %.2f is out of range [%.1f - %.1f]", name, param_name, val, min, max); } +static void log_feature_not_supported(const char *name, const char *feature) { + ESP_LOGW(TAG, "'%s': %s not supported", name, feature); +} + +static void log_color_mode_not_supported(const char *name, const char *feature) { + ESP_LOGW(TAG, "'%s': color mode does not support setting %s", name, feature); +} + +static void log_invalid_parameter(const char *name, const char *message) { ESP_LOGW(TAG, "'%s': %s", name, message); } +#else +#define log_validation_warning(name, param_name, val, min, max) +#define log_feature_not_supported(name, feature) +#define log_color_mode_not_supported(name, feature) +#define log_invalid_parameter(name, message) +#endif + // Macro to reduce repetitive setter code #define IMPLEMENT_LIGHT_CALL_SETTER(name, type, flag) \ LightCall &LightCall::set_##name(optional(name)) { \ @@ -49,11 +66,21 @@ static const LogString *color_mode_to_human(ColorMode color_mode) { return LOG_STR(""); } +// Helper to log percentage values +#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG +static void log_percent(const char *name, const char *param, float value) { + ESP_LOGD(TAG, " %s: %.0f%%", param, value * 100.0f); +} +#else +#define log_percent(name, param, value) +#endif + void LightCall::perform() { const char *name = this->parent_->get_name().c_str(); LightColorValues v = this->validate_(); + const bool publish = this->get_publish_(); - if (this->get_publish_()) { + if (publish) { ESP_LOGD(TAG, "'%s' Setting:", name); // Only print color mode when it's being changed @@ -71,11 +98,11 @@ void LightCall::perform() { } if (this->has_brightness()) { - ESP_LOGD(TAG, " Brightness: %.0f%%", v.get_brightness() * 100.0f); + log_percent(name, "Brightness", v.get_brightness()); } if (this->has_color_brightness()) { - ESP_LOGD(TAG, " Color brightness: %.0f%%", v.get_color_brightness() * 100.0f); + log_percent(name, "Color brightness", v.get_color_brightness()); } if (this->has_red() || this->has_green() || this->has_blue()) { ESP_LOGD(TAG, " Red: %.0f%%, Green: %.0f%%, Blue: %.0f%%", v.get_red() * 100.0f, v.get_green() * 100.0f, @@ -83,7 +110,7 @@ void LightCall::perform() { } if (this->has_white()) { - ESP_LOGD(TAG, " White: %.0f%%", v.get_white() * 100.0f); + log_percent(name, "White", v.get_white()); } if (this->has_color_temperature()) { ESP_LOGD(TAG, " Color temperature: %.1f mireds", v.get_color_temperature()); @@ -97,26 +124,26 @@ void LightCall::perform() { if (this->has_flash_()) { // FLASH - if (this->get_publish_()) { + if (publish) { ESP_LOGD(TAG, " Flash length: %.1fs", this->flash_length_ / 1e3f); } - this->parent_->start_flash_(v, this->flash_length_, this->get_publish_()); + this->parent_->start_flash_(v, this->flash_length_, publish); } else if (this->has_transition_()) { // TRANSITION - if (this->get_publish_()) { + if (publish) { ESP_LOGD(TAG, " Transition length: %.1fs", this->transition_length_ / 1e3f); } // Special case: Transition and effect can be set when turning off if (this->has_effect_()) { - if (this->get_publish_()) { + if (publish) { ESP_LOGD(TAG, " Effect: 'None'"); } this->parent_->stop_effect_(); } - this->parent_->start_transition_(v, this->transition_length_, this->get_publish_()); + this->parent_->start_transition_(v, this->transition_length_, publish); } else if (this->has_effect_()) { // EFFECT @@ -127,7 +154,7 @@ void LightCall::perform() { effect_s = this->parent_->effects_[this->effect_ - 1]->get_name().c_str(); } - if (this->get_publish_()) { + if (publish) { ESP_LOGD(TAG, " Effect: '%s'", effect_s); } @@ -138,13 +165,13 @@ void LightCall::perform() { this->parent_->set_immediately_(v, true); } else { // INSTANT CHANGE - this->parent_->set_immediately_(v, this->get_publish_()); + this->parent_->set_immediately_(v, publish); } if (!this->has_transition_()) { this->parent_->target_state_reached_callback_.call(); } - if (this->get_publish_()) { + if (publish) { this->parent_->publish_state(); } if (this->get_save_()) { @@ -174,19 +201,19 @@ LightColorValues LightCall::validate_() { // Brightness exists check if (this->has_brightness() && this->brightness_ > 0.0f && !(color_mode & ColorCapability::BRIGHTNESS)) { - ESP_LOGW(TAG, "'%s': setting brightness not supported", name); + log_feature_not_supported(name, "brightness"); this->set_flag_(FLAG_HAS_BRIGHTNESS, false); } // Transition length possible check if (this->has_transition_() && this->transition_length_ != 0 && !(color_mode & ColorCapability::BRIGHTNESS)) { - ESP_LOGW(TAG, "'%s': transitions not supported", name); + log_feature_not_supported(name, "transitions"); this->set_flag_(FLAG_HAS_TRANSITION, false); } // Color brightness exists check if (this->has_color_brightness() && this->color_brightness_ > 0.0f && !(color_mode & ColorCapability::RGB)) { - ESP_LOGW(TAG, "'%s': color mode does not support setting RGB brightness", name); + log_color_mode_not_supported(name, "RGB brightness"); this->set_flag_(FLAG_HAS_COLOR_BRIGHTNESS, false); } @@ -194,7 +221,7 @@ LightColorValues LightCall::validate_() { if ((this->has_red() && this->red_ > 0.0f) || (this->has_green() && this->green_ > 0.0f) || (this->has_blue() && this->blue_ > 0.0f)) { if (!(color_mode & ColorCapability::RGB)) { - ESP_LOGW(TAG, "'%s': color mode does not support setting RGB color", name); + log_color_mode_not_supported(name, "RGB color"); this->set_flag_(FLAG_HAS_RED, false); this->set_flag_(FLAG_HAS_GREEN, false); this->set_flag_(FLAG_HAS_BLUE, false); @@ -204,21 +231,21 @@ LightColorValues LightCall::validate_() { // White value exists check if (this->has_white() && this->white_ > 0.0f && !(color_mode & ColorCapability::WHITE || color_mode & ColorCapability::COLD_WARM_WHITE)) { - ESP_LOGW(TAG, "'%s': color mode does not support setting white value", name); + log_color_mode_not_supported(name, "white value"); this->set_flag_(FLAG_HAS_WHITE, false); } // Color temperature exists check if (this->has_color_temperature() && !(color_mode & ColorCapability::COLOR_TEMPERATURE || color_mode & ColorCapability::COLD_WARM_WHITE)) { - ESP_LOGW(TAG, "'%s': color mode does not support setting color temperature", name); + log_color_mode_not_supported(name, "color temperature"); this->set_flag_(FLAG_HAS_COLOR_TEMPERATURE, false); } // Cold/warm white value exists check if ((this->has_cold_white() && this->cold_white_ > 0.0f) || (this->has_warm_white() && this->warm_white_ > 0.0f)) { if (!(color_mode & ColorCapability::COLD_WARM_WHITE)) { - ESP_LOGW(TAG, "'%s': color mode does not support setting cold/warm white value", name); + log_color_mode_not_supported(name, "cold/warm white value"); this->set_flag_(FLAG_HAS_COLD_WHITE, false); this->set_flag_(FLAG_HAS_WARM_WHITE, false); } @@ -292,7 +319,7 @@ LightColorValues LightCall::validate_() { // Flash length check if (this->has_flash_() && this->flash_length_ == 0) { - ESP_LOGW(TAG, "'%s': flash length must be greater than zero", name); + log_invalid_parameter(name, "flash length must be greater than zero"); this->set_flag_(FLAG_HAS_FLASH, false); } @@ -311,13 +338,13 @@ LightColorValues LightCall::validate_() { } if (this->has_effect_() && (this->has_transition_() || this->has_flash_())) { - ESP_LOGW(TAG, "'%s': effect cannot be used with transition/flash", name); + log_invalid_parameter(name, "effect cannot be used with transition/flash"); this->set_flag_(FLAG_HAS_TRANSITION, false); this->set_flag_(FLAG_HAS_FLASH, false); } if (this->has_flash_() && this->has_transition_()) { - ESP_LOGW(TAG, "'%s': flash cannot be used with transition", name); + log_invalid_parameter(name, "flash cannot be used with transition"); this->set_flag_(FLAG_HAS_TRANSITION, false); } @@ -334,7 +361,7 @@ LightColorValues LightCall::validate_() { } if (this->has_transition_() && !supports_transition) { - ESP_LOGW(TAG, "'%s': transitions not supported", name); + log_feature_not_supported(name, "transitions"); this->set_flag_(FLAG_HAS_TRANSITION, false); } @@ -344,7 +371,7 @@ LightColorValues LightCall::validate_() { bool target_state = this->has_state() ? this->state_ : v.is_on(); if (!this->has_flash_() && !target_state) { if (this->has_effect_()) { - ESP_LOGW(TAG, "'%s': cannot start effect when turning off", name); + log_invalid_parameter(name, "cannot start effect when turning off"); this->set_flag_(FLAG_HAS_EFFECT, false); } else if (this->parent_->active_effect_index_ != 0 && explicit_turn_off_request) { // Auto turn off effect @@ -368,21 +395,27 @@ void LightCall::transform_parameters_() { // - RGBWW lights with color_interlock=true, which also sets "brightness" and // "color_temperature" (without color_interlock, CW/WW are set directly) // - Legacy Home Assistant (pre-colormode), which sets "white" and "color_temperature" + + // Cache min/max mireds to avoid repeated calls + const float min_mireds = traits.get_min_mireds(); + const float max_mireds = traits.get_max_mireds(); + if (((this->has_white() && this->white_ > 0.0f) || this->has_color_temperature()) && // (this->color_mode_ & ColorCapability::COLD_WARM_WHITE) && // !(this->color_mode_ & ColorCapability::WHITE) && // !(this->color_mode_ & ColorCapability::COLOR_TEMPERATURE) && // - traits.get_min_mireds() > 0.0f && traits.get_max_mireds() > 0.0f) { + min_mireds > 0.0f && max_mireds > 0.0f) { ESP_LOGD(TAG, "'%s': setting cold/warm white channels using white/color temperature values", this->parent_->get_name().c_str()); if (this->has_color_temperature()) { - const float color_temp = clamp(this->color_temperature_, traits.get_min_mireds(), traits.get_max_mireds()); - const float ww_fraction = - (color_temp - traits.get_min_mireds()) / (traits.get_max_mireds() - traits.get_min_mireds()); + const float color_temp = clamp(this->color_temperature_, min_mireds, max_mireds); + const float range = max_mireds - min_mireds; + const float ww_fraction = (color_temp - min_mireds) / range; const float cw_fraction = 1.0f - ww_fraction; const float max_cw_ww = std::max(ww_fraction, cw_fraction); - this->cold_white_ = gamma_uncorrect(cw_fraction / max_cw_ww, this->parent_->get_gamma_correct()); - this->warm_white_ = gamma_uncorrect(ww_fraction / max_cw_ww, this->parent_->get_gamma_correct()); + const float gamma = this->parent_->get_gamma_correct(); + this->cold_white_ = gamma_uncorrect(cw_fraction / max_cw_ww, gamma); + this->warm_white_ = gamma_uncorrect(ww_fraction / max_cw_ww, gamma); this->set_flag_(FLAG_HAS_COLD_WHITE, true); this->set_flag_(FLAG_HAS_WARM_WHITE, true); } diff --git a/esphome/components/light/light_color_values.h b/esphome/components/light/light_color_values.h index 5653a8d2a5..04d7d1e7d8 100644 --- a/esphome/components/light/light_color_values.h +++ b/esphome/components/light/light_color_values.h @@ -84,18 +84,23 @@ class LightColorValues { * @return The linearly interpolated LightColorValues. */ static LightColorValues lerp(const LightColorValues &start, const LightColorValues &end, float completion) { + // Directly interpolate the raw values to avoid getter/setter overhead. + // This is safe because: + // - All LightColorValues have their values clamped when set via the setters + // - std::lerp guarantees output is in the same range as inputs + // - Therefore the output doesn't need clamping, so we can skip the setters LightColorValues v; - v.set_color_mode(end.color_mode_); - v.set_state(std::lerp(start.get_state(), end.get_state(), completion)); - v.set_brightness(std::lerp(start.get_brightness(), end.get_brightness(), completion)); - v.set_color_brightness(std::lerp(start.get_color_brightness(), end.get_color_brightness(), completion)); - v.set_red(std::lerp(start.get_red(), end.get_red(), completion)); - v.set_green(std::lerp(start.get_green(), end.get_green(), completion)); - v.set_blue(std::lerp(start.get_blue(), end.get_blue(), completion)); - v.set_white(std::lerp(start.get_white(), end.get_white(), completion)); - v.set_color_temperature(std::lerp(start.get_color_temperature(), end.get_color_temperature(), completion)); - v.set_cold_white(std::lerp(start.get_cold_white(), end.get_cold_white(), completion)); - v.set_warm_white(std::lerp(start.get_warm_white(), end.get_warm_white(), completion)); + v.color_mode_ = end.color_mode_; + v.state_ = std::lerp(start.state_, end.state_, completion); + v.brightness_ = std::lerp(start.brightness_, end.brightness_, completion); + v.color_brightness_ = std::lerp(start.color_brightness_, end.color_brightness_, completion); + v.red_ = std::lerp(start.red_, end.red_, completion); + v.green_ = std::lerp(start.green_, end.green_, completion); + v.blue_ = std::lerp(start.blue_, end.blue_, completion); + v.white_ = std::lerp(start.white_, end.white_, completion); + v.color_temperature_ = std::lerp(start.color_temperature_, end.color_temperature_, completion); + v.cold_white_ = std::lerp(start.cold_white_, end.cold_white_, completion); + v.warm_white_ = std::lerp(start.warm_white_, end.warm_white_, completion); return v; } diff --git a/esphome/components/light/light_json_schema.cpp b/esphome/components/light/light_json_schema.cpp index 26615bae5c..896b821705 100644 --- a/esphome/components/light/light_json_schema.cpp +++ b/esphome/components/light/light_json_schema.cpp @@ -8,6 +8,32 @@ namespace light { // See https://www.home-assistant.io/integrations/light.mqtt/#json-schema for documentation on the schema +// Lookup table for color mode strings +static constexpr const char *get_color_mode_json_str(ColorMode mode) { + switch (mode) { + case ColorMode::ON_OFF: + return "onoff"; + case ColorMode::BRIGHTNESS: + return "brightness"; + case ColorMode::WHITE: + return "white"; // not supported by HA in MQTT + case ColorMode::COLOR_TEMPERATURE: + return "color_temp"; + case ColorMode::COLD_WARM_WHITE: + return "cwww"; // not supported by HA + case ColorMode::RGB: + return "rgb"; + case ColorMode::RGB_WHITE: + return "rgbw"; + case ColorMode::RGB_COLOR_TEMPERATURE: + return "rgbct"; // not supported by HA + case ColorMode::RGB_COLD_WARM_WHITE: + return "rgbww"; + default: + return nullptr; + } +} + void LightJSONSchema::dump_json(LightState &state, JsonObject root) { // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson if (state.supports_effects()) @@ -16,60 +42,36 @@ void LightJSONSchema::dump_json(LightState &state, JsonObject root) { auto values = state.remote_values; auto traits = state.get_output()->get_traits(); - switch (values.get_color_mode()) { - case ColorMode::UNKNOWN: // don't need to set color mode if we don't know it - break; - case ColorMode::ON_OFF: - root["color_mode"] = "onoff"; - break; - case ColorMode::BRIGHTNESS: - root["color_mode"] = "brightness"; - break; - case ColorMode::WHITE: // not supported by HA in MQTT - root["color_mode"] = "white"; - break; - case ColorMode::COLOR_TEMPERATURE: - root["color_mode"] = "color_temp"; - break; - case ColorMode::COLD_WARM_WHITE: // not supported by HA - root["color_mode"] = "cwww"; - break; - case ColorMode::RGB: - root["color_mode"] = "rgb"; - break; - case ColorMode::RGB_WHITE: - root["color_mode"] = "rgbw"; - break; - case ColorMode::RGB_COLOR_TEMPERATURE: // not supported by HA - root["color_mode"] = "rgbct"; - break; - case ColorMode::RGB_COLD_WARM_WHITE: - root["color_mode"] = "rgbww"; - break; + const auto color_mode = values.get_color_mode(); + const char *mode_str = get_color_mode_json_str(color_mode); + if (mode_str != nullptr) { + root["color_mode"] = mode_str; } - if (values.get_color_mode() & ColorCapability::ON_OFF) + if (color_mode & ColorCapability::ON_OFF) root["state"] = (values.get_state() != 0.0f) ? "ON" : "OFF"; - if (values.get_color_mode() & ColorCapability::BRIGHTNESS) - root["brightness"] = uint8_t(values.get_brightness() * 255); + if (color_mode & ColorCapability::BRIGHTNESS) + root["brightness"] = to_uint8_scale(values.get_brightness()); JsonObject color = root["color"].to(); - if (values.get_color_mode() & ColorCapability::RGB) { - color["r"] = uint8_t(values.get_color_brightness() * values.get_red() * 255); - color["g"] = uint8_t(values.get_color_brightness() * values.get_green() * 255); - color["b"] = uint8_t(values.get_color_brightness() * values.get_blue() * 255); + if (color_mode & ColorCapability::RGB) { + float color_brightness = values.get_color_brightness(); + color["r"] = to_uint8_scale(color_brightness * values.get_red()); + color["g"] = to_uint8_scale(color_brightness * values.get_green()); + color["b"] = to_uint8_scale(color_brightness * values.get_blue()); } - if (values.get_color_mode() & ColorCapability::WHITE) { - color["w"] = uint8_t(values.get_white() * 255); - root["white_value"] = uint8_t(values.get_white() * 255); // legacy API + if (color_mode & ColorCapability::WHITE) { + uint8_t white_val = to_uint8_scale(values.get_white()); + color["w"] = white_val; + root["white_value"] = white_val; // legacy API } - if (values.get_color_mode() & ColorCapability::COLOR_TEMPERATURE) { + if (color_mode & ColorCapability::COLOR_TEMPERATURE) { // this one isn't under the color subkey for some reason root["color_temp"] = uint32_t(values.get_color_temperature()); } - if (values.get_color_mode() & ColorCapability::COLD_WARM_WHITE) { - color["c"] = uint8_t(values.get_cold_white() * 255); - color["w"] = uint8_t(values.get_warm_white() * 255); + if (color_mode & ColorCapability::COLD_WARM_WHITE) { + color["c"] = to_uint8_scale(values.get_cold_white()); + color["w"] = to_uint8_scale(values.get_warm_white()); } } diff --git a/esphome/components/light/light_state.cpp b/esphome/components/light/light_state.cpp index fd0aafe4c6..5b57707d6b 100644 --- a/esphome/components/light/light_state.cpp +++ b/esphome/components/light/light_state.cpp @@ -24,7 +24,8 @@ void LightState::setup() { } // When supported color temperature range is known, initialize color temperature setting within bounds. - float min_mireds = this->get_traits().get_min_mireds(); + auto traits = this->get_traits(); + float min_mireds = traits.get_min_mireds(); if (min_mireds > 0) { this->remote_values.set_color_temperature(min_mireds); this->current_values.set_color_temperature(min_mireds); @@ -43,11 +44,8 @@ void LightState::setup() { this->rtc_ = global_preferences->make_preference(this->get_object_id_hash()); // Attempt to load from preferences, else fall back to default values if (!this->rtc_.load(&recovered)) { - recovered.state = false; - if (this->restore_mode_ == LIGHT_RESTORE_DEFAULT_ON || - this->restore_mode_ == LIGHT_RESTORE_INVERTED_DEFAULT_ON) { - recovered.state = true; - } + recovered.state = (this->restore_mode_ == LIGHT_RESTORE_DEFAULT_ON || + this->restore_mode_ == LIGHT_RESTORE_INVERTED_DEFAULT_ON); } else if (this->restore_mode_ == LIGHT_RESTORE_INVERTED_DEFAULT_OFF || this->restore_mode_ == LIGHT_RESTORE_INVERTED_DEFAULT_ON) { // Inverted restore state @@ -88,17 +86,18 @@ void LightState::setup() { } void LightState::dump_config() { ESP_LOGCONFIG(TAG, "Light '%s'", this->get_name().c_str()); - if (this->get_traits().supports_color_capability(ColorCapability::BRIGHTNESS)) { + auto traits = this->get_traits(); + if (traits.supports_color_capability(ColorCapability::BRIGHTNESS)) { ESP_LOGCONFIG(TAG, " Default Transition Length: %.1fs\n" " Gamma Correct: %.2f", this->default_transition_length_ / 1e3f, this->gamma_correct_); } - if (this->get_traits().supports_color_capability(ColorCapability::COLOR_TEMPERATURE)) { + if (traits.supports_color_capability(ColorCapability::COLOR_TEMPERATURE)) { ESP_LOGCONFIG(TAG, " Min Mireds: %.1f\n" " Max Mireds: %.1f", - this->get_traits().get_min_mireds(), this->get_traits().get_max_mireds()); + traits.get_min_mireds(), traits.get_max_mireds()); } } void LightState::loop() { From 7f0c66f8350b1ee32c933fbb524163359afb94b0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 27 Jul 2025 17:24:15 -1000 Subject: [PATCH 20/24] [api] Reduce code duplication in send_noise_encryption_set_key_response (#9918) --- esphome/components/api/api_connection.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 0d3b99cd41..e0d4ec0cc8 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1538,19 +1538,18 @@ void APIConnection::execute_service(const ExecuteServiceRequest &msg) { #endif #ifdef USE_API_NOISE bool APIConnection::send_noise_encryption_set_key_response(const NoiseEncryptionSetKeyRequest &msg) { - psk_t psk{}; NoiseEncryptionSetKeyResponse resp; + resp.success = false; + + psk_t psk{}; if (base64_decode(msg.key, psk.data(), msg.key.size()) != psk.size()) { ESP_LOGW(TAG, "Invalid encryption key length"); - resp.success = false; - return this->send_message(resp, NoiseEncryptionSetKeyResponse::MESSAGE_TYPE); - } - if (!this->parent_->save_noise_psk(psk, true)) { + } else if (!this->parent_->save_noise_psk(psk, true)) { ESP_LOGW(TAG, "Failed to save encryption key"); - resp.success = false; - return this->send_message(resp, NoiseEncryptionSetKeyResponse::MESSAGE_TYPE); + } else { + resp.success = true; } - resp.success = true; + return this->send_message(resp, NoiseEncryptionSetKeyResponse::MESSAGE_TYPE); } #endif From 087970bca8ed71b994b795acf3e473562cb3e3c1 Mon Sep 17 00:00:00 2001 From: Cornelius Mosch <16503500+cmaxl@users.noreply.github.com> Date: Mon, 28 Jul 2025 05:25:32 +0200 Subject: [PATCH 21/24] replace os.getlogin() with getpass.getuser() (#9928) --- esphome/__main__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/esphome/__main__.py b/esphome/__main__.py index c9f5037890..341c1fa893 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -2,6 +2,7 @@ import argparse from datetime import datetime import functools +import getpass import importlib import logging import os @@ -335,7 +336,7 @@ def check_permissions(port): raise EsphomeError( "You do not have read or write permission on the selected serial port. " "To resolve this issue, you can add your user to the dialout group " - f"by running the following command: sudo usermod -a -G dialout {os.getlogin()}. " + f"by running the following command: sudo usermod -a -G dialout {getpass.getuser()}. " "You will need to log out & back in or reboot to activate the new group access." ) From 5029e248ebb68eaf175cef6f66ec03cd06b938ea Mon Sep 17 00:00:00 2001 From: tomaszduda23 Date: Mon, 28 Jul 2025 05:28:27 +0200 Subject: [PATCH 22/24] [packages] add example from documentation to component tests (#9891) --- tests/components/packages/garage-door.yaml | 5 +++++ .../packages/test-vars.esp32-idf.yaml | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/components/packages/garage-door.yaml create mode 100644 tests/components/packages/test-vars.esp32-idf.yaml diff --git a/tests/components/packages/garage-door.yaml b/tests/components/packages/garage-door.yaml new file mode 100644 index 0000000000..e16265d1e1 --- /dev/null +++ b/tests/components/packages/garage-door.yaml @@ -0,0 +1,5 @@ +switch: + - name: ${door_name} Garage Door Switch + platform: gpio + pin: ${door_pin} + id: ${door_id} diff --git a/tests/components/packages/test-vars.esp32-idf.yaml b/tests/components/packages/test-vars.esp32-idf.yaml new file mode 100644 index 0000000000..f12467d9f9 --- /dev/null +++ b/tests/components/packages/test-vars.esp32-idf.yaml @@ -0,0 +1,19 @@ +packages: + left_garage_door: !include + file: garage-door.yaml + vars: + door_name: Left + door_pin: 1 + door_id: left_garage_door + middle_garage_door: !include + file: garage-door.yaml + vars: + door_name: Middle + door_pin: 2 + door_id: middle_garage_door + right_garage_door: !include + file: garage-door.yaml + vars: + door_name: Right + door_pin: 3 + door_id: right_garage_door From 05f6d01cbe32ebf505b61d5d14607a8309aabe0c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 27 Jul 2025 18:35:35 -1000 Subject: [PATCH 23/24] [api] Add conditional compilation for Home Assistant service subscriptions (#9900) --- esphome/components/api/__init__.py | 7 +++++++ esphome/components/api/api.proto | 2 ++ esphome/components/api/api_connection.h | 4 ++++ esphome/components/api/api_pb2.cpp | 2 ++ esphome/components/api/api_pb2.h | 2 ++ esphome/components/api/api_pb2_dump.cpp | 2 ++ esphome/components/api/api_pb2_service.cpp | 4 ++++ esphome/components/api/api_pb2_service.h | 6 ++++++ esphome/components/api/api_server.cpp | 2 ++ esphome/components/api/api_server.h | 2 ++ esphome/components/api/custom_api_device.h | 2 ++ esphome/components/api/homeassistant_service.h | 2 ++ esphome/components/homeassistant/number/__init__.py | 1 + esphome/components/homeassistant/switch/__init__.py | 1 + esphome/core/defines.h | 1 + 15 files changed, 40 insertions(+) diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index f48a7d93be..5d398a4e23 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -53,6 +53,7 @@ SERVICE_ARG_NATIVE_TYPES = { CONF_ENCRYPTION = "encryption" CONF_BATCH_DELAY = "batch_delay" CONF_CUSTOM_SERVICES = "custom_services" +CONF_HOMEASSISTANT_SERVICES = "homeassistant_services" CONF_HOMEASSISTANT_STATES = "homeassistant_states" @@ -119,6 +120,7 @@ CONFIG_SCHEMA = cv.All( cv.Range(max=cv.TimePeriod(milliseconds=65535)), ), cv.Optional(CONF_CUSTOM_SERVICES, default=False): cv.boolean, + cv.Optional(CONF_HOMEASSISTANT_SERVICES, default=False): cv.boolean, cv.Optional(CONF_HOMEASSISTANT_STATES, default=False): cv.boolean, cv.Optional(CONF_ON_CLIENT_CONNECTED): automation.validate_automation( single=True @@ -148,6 +150,9 @@ async def to_code(config): if config.get(CONF_ACTIONS) or config[CONF_CUSTOM_SERVICES]: cg.add_define("USE_API_SERVICES") + if config[CONF_HOMEASSISTANT_SERVICES]: + cg.add_define("USE_API_HOMEASSISTANT_SERVICES") + if config[CONF_HOMEASSISTANT_STATES]: cg.add_define("USE_API_HOMEASSISTANT_STATES") @@ -240,6 +245,7 @@ HOMEASSISTANT_ACTION_ACTION_SCHEMA = cv.All( HOMEASSISTANT_ACTION_ACTION_SCHEMA, ) async def homeassistant_service_to_code(config, action_id, template_arg, args): + cg.add_define("USE_API_HOMEASSISTANT_SERVICES") serv = await cg.get_variable(config[CONF_ID]) var = cg.new_Pvariable(action_id, template_arg, serv, False) templ = await cg.templatable(config[CONF_ACTION], args, None) @@ -283,6 +289,7 @@ HOMEASSISTANT_EVENT_ACTION_SCHEMA = cv.Schema( HOMEASSISTANT_EVENT_ACTION_SCHEMA, ) async def homeassistant_event_to_code(config, action_id, template_arg, args): + cg.add_define("USE_API_HOMEASSISTANT_SERVICES") serv = await cg.get_variable(config[CONF_ID]) var = cg.new_Pvariable(action_id, template_arg, serv, True) templ = await cg.templatable(config[CONF_EVENT], args, None) diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index 5956c8b0b9..eb6ccbfb91 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -755,6 +755,7 @@ message NoiseEncryptionSetKeyResponse { message SubscribeHomeassistantServicesRequest { option (id) = 34; option (source) = SOURCE_CLIENT; + option (ifdef) = "USE_API_HOMEASSISTANT_SERVICES"; } message HomeassistantServiceMap { @@ -766,6 +767,7 @@ message HomeassistantServiceResponse { option (id) = 35; option (source) = SOURCE_SERVER; option (no_delay) = true; + option (ifdef) = "USE_API_HOMEASSISTANT_SERVICES"; string service = 1; repeated HomeassistantServiceMap data = 2; diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 4f4b44b0ad..3c446c431b 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -131,11 +131,13 @@ class APIConnection : public APIServerConnection { void media_player_command(const MediaPlayerCommandRequest &msg) override; #endif bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len); +#ifdef USE_API_HOMEASSISTANT_SERVICES void send_homeassistant_service_call(const HomeassistantServiceResponse &call) { if (!this->flags_.service_call_subscription) return; this->send_message(call, HomeassistantServiceResponse::MESSAGE_TYPE); } +#endif #ifdef USE_BLUETOOTH_PROXY void subscribe_bluetooth_le_advertisements(const SubscribeBluetoothLEAdvertisementsRequest &msg) override; void unsubscribe_bluetooth_le_advertisements(const UnsubscribeBluetoothLEAdvertisementsRequest &msg) override; @@ -209,9 +211,11 @@ class APIConnection : public APIServerConnection { if (msg.dump_config) App.schedule_dump_config(); } +#ifdef USE_API_HOMEASSISTANT_SERVICES void subscribe_homeassistant_services(const SubscribeHomeassistantServicesRequest &msg) override { this->flags_.service_call_subscription = true; } +#endif #ifdef USE_API_HOMEASSISTANT_STATES void subscribe_home_assistant_states(const SubscribeHomeAssistantStatesRequest &msg) override; #endif diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index e2d1cfebd4..89177b233a 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -843,6 +843,7 @@ void NoiseEncryptionSetKeyResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_bool_field(total_size, 1, this->success); } #endif +#ifdef USE_API_HOMEASSISTANT_SERVICES void HomeassistantServiceMap::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->key_ref_); buffer.encode_string(2, this->value_ref_); @@ -871,6 +872,7 @@ void HomeassistantServiceResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_repeated_message(total_size, 1, this->variables); ProtoSize::add_bool_field(total_size, 1, this->is_event); } +#endif #ifdef USE_API_HOMEASSISTANT_STATES void SubscribeHomeAssistantStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->entity_id_ref_); diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index b7d8945e8e..65623144d1 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -1044,6 +1044,7 @@ class NoiseEncryptionSetKeyResponse : public ProtoMessage { protected: }; #endif +#ifdef USE_API_HOMEASSISTANT_SERVICES class SubscribeHomeassistantServicesRequest : public ProtoDecodableMessage { public: static constexpr uint8_t MESSAGE_TYPE = 34; @@ -1092,6 +1093,7 @@ class HomeassistantServiceResponse : public ProtoMessage { protected: }; +#endif #ifdef USE_API_HOMEASSISTANT_STATES class SubscribeHomeAssistantStatesRequest : public ProtoDecodableMessage { public: diff --git a/esphome/components/api/api_pb2_dump.cpp b/esphome/components/api/api_pb2_dump.cpp index d7f9f63f5f..c18425f369 100644 --- a/esphome/components/api/api_pb2_dump.cpp +++ b/esphome/components/api/api_pb2_dump.cpp @@ -1038,6 +1038,7 @@ void NoiseEncryptionSetKeyRequest::dump_to(std::string &out) const { } void NoiseEncryptionSetKeyResponse::dump_to(std::string &out) const { dump_field(out, "success", this->success); } #endif +#ifdef USE_API_HOMEASSISTANT_SERVICES void SubscribeHomeassistantServicesRequest::dump_to(std::string &out) const { out.append("SubscribeHomeassistantServicesRequest {}"); } @@ -1066,6 +1067,7 @@ void HomeassistantServiceResponse::dump_to(std::string &out) const { } dump_field(out, "is_event", this->is_event); } +#endif #ifdef USE_API_HOMEASSISTANT_STATES void SubscribeHomeAssistantStatesRequest::dump_to(std::string &out) const { out.append("SubscribeHomeAssistantStatesRequest {}"); diff --git a/esphome/components/api/api_pb2_service.cpp b/esphome/components/api/api_pb2_service.cpp index 4674d04f66..26d5b12d00 100644 --- a/esphome/components/api/api_pb2_service.cpp +++ b/esphome/components/api/api_pb2_service.cpp @@ -149,6 +149,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type, break; } #endif +#ifdef USE_API_HOMEASSISTANT_SERVICES case SubscribeHomeassistantServicesRequest::MESSAGE_TYPE: { SubscribeHomeassistantServicesRequest msg; msg.decode(msg_data, msg_size); @@ -158,6 +159,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type, this->on_subscribe_homeassistant_services_request(msg); break; } +#endif case GetTimeRequest::MESSAGE_TYPE: { GetTimeRequest msg; msg.decode(msg_data, msg_size); @@ -639,12 +641,14 @@ void APIServerConnection::on_subscribe_logs_request(const SubscribeLogsRequest & this->subscribe_logs(msg); } } +#ifdef USE_API_HOMEASSISTANT_SERVICES void APIServerConnection::on_subscribe_homeassistant_services_request( const SubscribeHomeassistantServicesRequest &msg) { if (this->check_authenticated_()) { this->subscribe_homeassistant_services(msg); } } +#endif #ifdef USE_API_HOMEASSISTANT_STATES void APIServerConnection::on_subscribe_home_assistant_states_request(const SubscribeHomeAssistantStatesRequest &msg) { if (this->check_authenticated_()) { diff --git a/esphome/components/api/api_pb2_service.h b/esphome/components/api/api_pb2_service.h index 19ed85aa0b..6172e33bf6 100644 --- a/esphome/components/api/api_pb2_service.h +++ b/esphome/components/api/api_pb2_service.h @@ -60,7 +60,9 @@ class APIServerConnectionBase : public ProtoService { virtual void on_noise_encryption_set_key_request(const NoiseEncryptionSetKeyRequest &value){}; #endif +#ifdef USE_API_HOMEASSISTANT_SERVICES virtual void on_subscribe_homeassistant_services_request(const SubscribeHomeassistantServicesRequest &value){}; +#endif #ifdef USE_API_HOMEASSISTANT_STATES virtual void on_subscribe_home_assistant_states_request(const SubscribeHomeAssistantStatesRequest &value){}; @@ -218,7 +220,9 @@ class APIServerConnection : public APIServerConnectionBase { virtual void list_entities(const ListEntitiesRequest &msg) = 0; virtual void subscribe_states(const SubscribeStatesRequest &msg) = 0; virtual void subscribe_logs(const SubscribeLogsRequest &msg) = 0; +#ifdef USE_API_HOMEASSISTANT_SERVICES virtual void subscribe_homeassistant_services(const SubscribeHomeassistantServicesRequest &msg) = 0; +#endif #ifdef USE_API_HOMEASSISTANT_STATES virtual void subscribe_home_assistant_states(const SubscribeHomeAssistantStatesRequest &msg) = 0; #endif @@ -338,7 +342,9 @@ class APIServerConnection : public APIServerConnectionBase { void on_list_entities_request(const ListEntitiesRequest &msg) override; void on_subscribe_states_request(const SubscribeStatesRequest &msg) override; void on_subscribe_logs_request(const SubscribeLogsRequest &msg) override; +#ifdef USE_API_HOMEASSISTANT_SERVICES void on_subscribe_homeassistant_services_request(const SubscribeHomeassistantServicesRequest &msg) override; +#endif #ifdef USE_API_HOMEASSISTANT_STATES void on_subscribe_home_assistant_states_request(const SubscribeHomeAssistantStatesRequest &msg) override; #endif diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index 0454315760..1f38f4a31a 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -369,11 +369,13 @@ void APIServer::set_password(const std::string &password) { this->password_ = pa void APIServer::set_batch_delay(uint16_t batch_delay) { this->batch_delay_ = batch_delay; } +#ifdef USE_API_HOMEASSISTANT_SERVICES void APIServer::send_homeassistant_service_call(const HomeassistantServiceResponse &call) { for (auto &client : this->clients_) { client->send_homeassistant_service_call(call); } } +#endif #ifdef USE_API_HOMEASSISTANT_STATES void APIServer::subscribe_home_assistant_state(std::string entity_id, optional attribute, diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index 22e9573d7e..8b5e624df2 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -106,7 +106,9 @@ class APIServer : public Component, public Controller { #ifdef USE_MEDIA_PLAYER void on_media_player_update(media_player::MediaPlayer *obj) override; #endif +#ifdef USE_API_HOMEASSISTANT_SERVICES void send_homeassistant_service_call(const HomeassistantServiceResponse &call); +#endif #ifdef USE_API_SERVICES void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); } #endif diff --git a/esphome/components/api/custom_api_device.h b/esphome/components/api/custom_api_device.h index e9e39a0772..2f85a40614 100644 --- a/esphome/components/api/custom_api_device.h +++ b/esphome/components/api/custom_api_device.h @@ -137,6 +137,7 @@ class CustomAPIDevice { } #endif +#ifdef USE_API_HOMEASSISTANT_SERVICES /** Call a Home Assistant service from ESPHome. * * Usage: @@ -221,6 +222,7 @@ class CustomAPIDevice { } global_api_server->send_homeassistant_service_call(resp); } +#endif }; } // namespace esphome::api diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index 212b3b22d6..ec17c0c7a4 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -2,6 +2,7 @@ #include "api_server.h" #ifdef USE_API +#ifdef USE_API_HOMEASSISTANT_SERVICES #include "api_pb2.h" #include "esphome/core/automation.h" #include "esphome/core/helpers.h" @@ -100,3 +101,4 @@ template class HomeAssistantServiceCallAction : public Action Date: Sun, 27 Jul 2025 18:39:25 -1000 Subject: [PATCH 24/24] [api] Fix string lifetime issue in Home Assistant service calls with templated values (#9909) --- esphome/components/api/api.proto | 2 +- esphome/components/api/api_options.proto | 1 + esphome/components/api/api_pb2.cpp | 4 +- esphome/components/api/api_pb2.h | 3 +- esphome/components/api/api_pb2_dump.cpp | 2 +- esphome/components/api/custom_api_device.h | 4 +- .../components/api/homeassistant_service.h | 9 +- .../number/homeassistant_number.cpp | 6 +- .../switch/homeassistant_switch.cpp | 2 +- script/api_protobuf/api_protobuf.py | 46 ++- .../fixtures/api_homeassistant.yaml | 311 ++++++++++++++++++ tests/integration/test_api_homeassistant.py | 305 +++++++++++++++++ 12 files changed, 670 insertions(+), 25 deletions(-) create mode 100644 tests/integration/fixtures/api_homeassistant.yaml create mode 100644 tests/integration/test_api_homeassistant.py diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index eb6ccbfb91..e0e1602fcb 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -760,7 +760,7 @@ message SubscribeHomeassistantServicesRequest { message HomeassistantServiceMap { string key = 1; - string value = 2; + string value = 2 [(no_zero_copy) = true]; } message HomeassistantServiceResponse { diff --git a/esphome/components/api/api_options.proto b/esphome/components/api/api_options.proto index bb3947e8a3..4f0f52fc6f 100644 --- a/esphome/components/api/api_options.proto +++ b/esphome/components/api/api_options.proto @@ -27,4 +27,5 @@ extend google.protobuf.MessageOptions { extend google.protobuf.FieldOptions { optional string field_ifdef = 1042; optional uint32 fixed_array_size = 50007; + optional bool no_zero_copy = 50008 [default=false]; } diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 89177b233a..b587ee5f03 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -846,11 +846,11 @@ void NoiseEncryptionSetKeyResponse::calculate_size(uint32_t &total_size) const { #ifdef USE_API_HOMEASSISTANT_SERVICES void HomeassistantServiceMap::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->key_ref_); - buffer.encode_string(2, this->value_ref_); + buffer.encode_string(2, this->value); } void HomeassistantServiceMap::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->key_ref_.size()); - ProtoSize::add_string_field(total_size, 1, this->value_ref_.size()); + ProtoSize::add_string_field(total_size, 1, this->value.size()); } void HomeassistantServiceResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->service_ref_); diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 65623144d1..fb8174f988 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -1062,8 +1062,7 @@ class HomeassistantServiceMap : public ProtoMessage { public: StringRef key_ref_{}; void set_key(const StringRef &ref) { this->key_ref_ = ref; } - StringRef value_ref_{}; - void set_value(const StringRef &ref) { this->value_ref_ = ref; } + std::string value{}; void encode(ProtoWriteBuffer buffer) const override; void calculate_size(uint32_t &total_size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP diff --git a/esphome/components/api/api_pb2_dump.cpp b/esphome/components/api/api_pb2_dump.cpp index c18425f369..aca60464a3 100644 --- a/esphome/components/api/api_pb2_dump.cpp +++ b/esphome/components/api/api_pb2_dump.cpp @@ -1045,7 +1045,7 @@ void SubscribeHomeassistantServicesRequest::dump_to(std::string &out) const { void HomeassistantServiceMap::dump_to(std::string &out) const { MessageDumpHelper helper(out, "HomeassistantServiceMap"); dump_field(out, "key", this->key_ref_); - dump_field(out, "value", this->value_ref_); + dump_field(out, "value", this->value); } void HomeassistantServiceResponse::dump_to(std::string &out) const { MessageDumpHelper helper(out, "HomeassistantServiceResponse"); diff --git a/esphome/components/api/custom_api_device.h b/esphome/components/api/custom_api_device.h index 2f85a40614..a39947e725 100644 --- a/esphome/components/api/custom_api_device.h +++ b/esphome/components/api/custom_api_device.h @@ -175,7 +175,7 @@ class CustomAPIDevice { resp.data.emplace_back(); auto &kv = resp.data.back(); kv.set_key(StringRef(it.first)); - kv.set_value(StringRef(it.second)); + kv.value = it.second; } global_api_server->send_homeassistant_service_call(resp); } @@ -218,7 +218,7 @@ class CustomAPIDevice { resp.data.emplace_back(); auto &kv = resp.data.back(); kv.set_key(StringRef(it.first)); - kv.set_value(StringRef(it.second)); + kv.value = it.second; } global_api_server->send_homeassistant_service_call(resp); } diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index ec17c0c7a4..5df9c7c792 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -70,22 +70,19 @@ template class HomeAssistantServiceCallAction : public Actiondata_template_) { resp.data_template.emplace_back(); auto &kv = resp.data_template.back(); kv.set_key(StringRef(it.key)); - std::string value = it.value.value(x...); - kv.set_value(StringRef(value)); + kv.value = it.value.value(x...); } for (auto &it : this->variables_) { resp.variables.emplace_back(); auto &kv = resp.variables.back(); kv.set_key(StringRef(it.key)); - std::string value = it.value.value(x...); - kv.set_value(StringRef(value)); + kv.value = it.value.value(x...); } this->parent_->send_homeassistant_service_call(resp); } diff --git a/esphome/components/homeassistant/number/homeassistant_number.cpp b/esphome/components/homeassistant/number/homeassistant_number.cpp index ffb352c969..87bf6727f2 100644 --- a/esphome/components/homeassistant/number/homeassistant_number.cpp +++ b/esphome/components/homeassistant/number/homeassistant_number.cpp @@ -93,14 +93,12 @@ void HomeassistantNumber::control(float value) { resp.data.emplace_back(); auto &entity_id = resp.data.back(); entity_id.set_key(ENTITY_ID_KEY); - entity_id.set_value(StringRef(this->entity_id_)); + entity_id.value = this->entity_id_; resp.data.emplace_back(); auto &entity_value = resp.data.back(); entity_value.set_key(VALUE_KEY); - // to_string() returns a temporary - must store it to avoid dangling reference - std::string value_str = to_string(value); - entity_value.set_value(StringRef(value_str)); + entity_value.value = to_string(value); api::global_api_server->send_homeassistant_service_call(resp); } diff --git a/esphome/components/homeassistant/switch/homeassistant_switch.cpp b/esphome/components/homeassistant/switch/homeassistant_switch.cpp index 0fe609bf43..b3300335b9 100644 --- a/esphome/components/homeassistant/switch/homeassistant_switch.cpp +++ b/esphome/components/homeassistant/switch/homeassistant_switch.cpp @@ -54,7 +54,7 @@ void HomeassistantSwitch::write_state(bool state) { resp.data.emplace_back(); auto &entity_id_kv = resp.data.back(); entity_id_kv.set_key(ENTITY_ID_KEY); - entity_id_kv.set_value(StringRef(this->entity_id_)); + entity_id_kv.value = this->entity_id_; api::global_api_server->send_homeassistant_service_call(resp); } diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 92c85d2366..4b9a61383d 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -562,11 +562,16 @@ class StringType(TypeInfo): @property def public_content(self) -> list[str]: content: list[str] = [] - # Add std::string storage if message needs decoding - if self._needs_decode: + + # Check if no_zero_copy option is set + no_zero_copy = get_field_opt(self._field, pb.no_zero_copy, False) + + # Add std::string storage if message needs decoding OR if no_zero_copy is set + if self._needs_decode or no_zero_copy: content.append(f"std::string {self.field_name}{{}};") - if self._needs_encode: + # Only add StringRef if encoding is needed AND no_zero_copy is not set + if self._needs_encode and not no_zero_copy: content.extend( [ # Add StringRef field if message needs encoding @@ -581,13 +586,28 @@ class StringType(TypeInfo): @property def encode_content(self) -> str: - return f"buffer.encode_string({self.number}, this->{self.field_name}_ref_);" + # Check if no_zero_copy option is set + no_zero_copy = get_field_opt(self._field, pb.no_zero_copy, False) + + if no_zero_copy: + # Use the std::string directly + return f"buffer.encode_string({self.number}, this->{self.field_name});" + else: + # Use the StringRef + return f"buffer.encode_string({self.number}, this->{self.field_name}_ref_);" def dump(self, name): + # Check if no_zero_copy option is set + no_zero_copy = get_field_opt(self._field, pb.no_zero_copy, False) + # If name is 'it', this is a repeated field element - always use string if name == "it": return "append_quoted_string(out, StringRef(it));" + # If no_zero_copy is set, always use std::string + if no_zero_copy: + return f'out.append("\'").append(this->{self.field_name}).append("\'");' + # For SOURCE_CLIENT only, always use std::string if not self._needs_encode: return f'out.append("\'").append(this->{self.field_name}).append("\'");' @@ -607,6 +627,13 @@ class StringType(TypeInfo): @property def dump_content(self) -> str: + # Check if no_zero_copy option is set + no_zero_copy = get_field_opt(self._field, pb.no_zero_copy, False) + + # If no_zero_copy is set, always use std::string + if no_zero_copy: + return f'dump_field(out, "{self.name}", this->{self.field_name});' + # For SOURCE_CLIENT only, use std::string if not self._needs_encode: return f'dump_field(out, "{self.name}", this->{self.field_name});' @@ -622,8 +649,15 @@ class StringType(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - # For SOURCE_CLIENT only messages, use the string field directly - if not self._needs_encode: + # Check if no_zero_copy option is set + no_zero_copy = get_field_opt(self._field, pb.no_zero_copy, False) + + # For SOURCE_CLIENT only messages or no_zero_copy, use the string field directly + if not self._needs_encode or no_zero_copy: + # For no_zero_copy, we need to use .size() on the string + if no_zero_copy and name != "it": + field_id_size = self.calculate_field_id_size() + return f"ProtoSize::add_string_field(total_size, {field_id_size}, this->{self.field_name}.size());" return self._get_simple_size_calculation(name, force, "add_string_field") # Check if this is being called from a repeated field context diff --git a/tests/integration/fixtures/api_homeassistant.yaml b/tests/integration/fixtures/api_homeassistant.yaml new file mode 100644 index 0000000000..ce8628977a --- /dev/null +++ b/tests/integration/fixtures/api_homeassistant.yaml @@ -0,0 +1,311 @@ +esphome: + name: test-ha-api + friendly_name: Home Assistant API Test + +host: + +api: + services: + - service: trigger_all_tests + then: + - logger.log: "=== Starting Home Assistant API Tests ===" + - button.press: test_basic_service + - button.press: test_templated_service + - button.press: test_empty_string_service + - button.press: test_multiple_fields_service + - button.press: test_complex_lambda_service + - button.press: test_all_empty_service + - button.press: test_rapid_service_calls + - button.press: test_read_ha_states + - number.set: + id: ha_number + value: 42.5 + - switch.turn_on: ha_switch + - switch.turn_off: ha_switch + - logger.log: "=== All tests completed ===" + +logger: + level: DEBUG + +# Time component for templated values +time: + - platform: homeassistant + id: homeassistant_time + +# Global variables for testing +globals: + - id: test_brightness + type: int + initial_value: '75' + - id: test_string + type: std::string + initial_value: '"test_value"' + +# Sensors for testing state reading +sensor: + - platform: template + name: "Test Sensor" + id: test_sensor + lambda: return 42.0; + update_interval: 0.1s + + # Home Assistant sensor that reads external state + - platform: homeassistant + name: "HA Temperature" + entity_id: sensor.external_temperature + id: ha_temperature + on_value: + then: + - logger.log: + format: "HA Temperature state updated: %.1f" + args: ['x'] + + # Test multiple HA state sensors + - platform: homeassistant + name: "HA Humidity" + entity_id: sensor.external_humidity + id: ha_humidity + on_value: + then: + - logger.log: + format: "HA Humidity state updated: %.1f" + args: ['x'] + +# Binary sensor from Home Assistant +binary_sensor: + - platform: homeassistant + name: "HA Motion" + entity_id: binary_sensor.external_motion + id: ha_motion + on_state: + then: + - logger.log: + format: "HA Motion state changed: %s" + args: ['x ? "ON" : "OFF"'] + +# Text sensor from Home Assistant +text_sensor: + - platform: homeassistant + name: "HA Weather" + entity_id: weather.home + attribute: condition + id: ha_weather + on_value: + then: + - logger.log: + format: "HA Weather condition updated: %s" + args: ['x.c_str()'] + + # Test empty state handling + - platform: homeassistant + name: "HA Empty State" + entity_id: sensor.nonexistent_sensor + id: ha_empty_state + on_value: + then: + - logger.log: + format: "HA Empty state updated: %s" + args: ['x.c_str()'] + +# Number component for testing HA number control +number: + - platform: template + name: "HA Controlled Number" + id: ha_number + min_value: 0 + max_value: 100 + step: 1 + optimistic: true + set_action: + - logger.log: + format: "Setting HA number to: %.1f" + args: ['x'] + - homeassistant.action: + action: input_number.set_value + data: + entity_id: input_number.test_number + value: !lambda 'return to_string(x);' + +# Switch component for testing HA switch control +switch: + - platform: template + name: "HA Controlled Switch" + id: ha_switch + optimistic: true + turn_on_action: + - logger.log: "Toggling HA switch: switch.test_switch ON" + - homeassistant.action: + action: switch.turn_on + data: + entity_id: switch.test_switch + turn_off_action: + - logger.log: "Toggling HA switch: switch.test_switch OFF" + - homeassistant.action: + action: switch.turn_off + data: + entity_id: switch.test_switch + +# Buttons for testing various service call scenarios +button: + # Test 1: Basic service call with static values + - platform: template + name: "Test Basic Service" + id: test_basic_service + on_press: + - logger.log: "Sending HomeAssistant service call: light.turn_off" + - homeassistant.action: + action: light.turn_off + data: + entity_id: light.test_light + - logger.log: "Service data: entity_id=light.test_light" + + # Test 2: Service call with templated/lambda values (main bug fix test) + - platform: template + name: "Test Templated Service" + id: test_templated_service + on_press: + - logger.log: "Testing templated service call" + - lambda: |- + int brightness_percent = id(test_brightness); + std::string computed = to_string(brightness_percent * 255 / 100); + ESP_LOGI("test", "Lambda computed value: %s", computed.c_str()); + - homeassistant.action: + action: light.turn_on + data: + entity_id: light.test_light + # This creates a temporary string - the main test case + brightness: !lambda 'return to_string(id(test_brightness) * 255 / 100);' + data_template: + color_name: !lambda 'return id(test_string);' + variables: + transition: !lambda 'return "2.5";' + + # Test 3: Service call with empty string values + - platform: template + name: "Test Empty String Service" + id: test_empty_string_service + on_press: + - logger.log: "Testing empty string values" + - homeassistant.action: + action: notify.test + data: + message: "Test message" + title: "" + data_template: + target: !lambda 'return "";' + variables: + sound: !lambda 'return "";' + + - logger.log: "Empty value for key: title" + - logger.log: "Empty value for key: target" + - logger.log: "Empty value for key: sound" + + # Test 4: Service call with multiple data fields + - platform: template + name: "Test Multiple Fields Service" + id: test_multiple_fields_service + on_press: + - logger.log: "Testing multiple data fields" + - homeassistant.action: + action: climate.set_temperature + data: + entity_id: climate.test_climate + temperature: "22" + hvac_mode: "heat" + data_template: + target_temp_high: !lambda 'return "24";' + target_temp_low: !lambda 'return "20";' + variables: + preset_mode: !lambda 'return "comfort";' + + # Test 5: Complex lambda with string operations + - platform: template + name: "Test Complex Lambda Service" + id: test_complex_lambda_service + on_press: + - logger.log: "Testing complex lambda expressions" + - homeassistant.action: + action: script.test_script + data: + entity_id: !lambda |- + std::string base = "light."; + std::string room = "living_room"; + return base + room; + brightness_pct: !lambda |- + float sensor_val = id(test_sensor).state; + int pct = (int)(sensor_val * 2.38); // 42 * 2.38 ≈ 100 + return to_string(pct); + data_template: + message: !lambda |- + char buffer[50]; + snprintf(buffer, sizeof(buffer), "Sensor: %.1f, Time: %02d:%02d", + id(test_sensor).state, + id(homeassistant_time).now().hour, + id(homeassistant_time).now().minute); + return std::string(buffer); + + # Test 6: Service with only empty strings to verify size calculation + - platform: template + name: "Test All Empty Service" + id: test_all_empty_service + on_press: + - logger.log: "Testing all empty string values" + - homeassistant.action: + action: test.empty + data: + field1: "" + field2: "" + data_template: + field3: !lambda 'return "";' + variables: + field4: !lambda 'return "";' + - logger.log: "All empty service call completed" + + # Test 7: Rapid successive service calls + - platform: template + name: "Test Rapid Service Calls" + id: test_rapid_service_calls + on_press: + - logger.log: "Testing rapid service calls" + - repeat: + count: 5 + then: + - homeassistant.action: + action: counter.increment + data: + entity_id: counter.test_counter + - delay: 10ms + - logger.log: "Rapid service calls completed" + + # Test 8: Log current HA states + - platform: template + name: "Test Read HA States" + id: test_read_ha_states + on_press: + - logger.log: "Reading current HA states" + - lambda: |- + if (id(ha_temperature).has_state()) { + ESP_LOGI("test", "Current HA Temperature: %.1f", id(ha_temperature).state); + } else { + ESP_LOGI("test", "HA Temperature has no state"); + } + + if (id(ha_humidity).has_state()) { + ESP_LOGI("test", "Current HA Humidity: %.1f", id(ha_humidity).state); + } else { + ESP_LOGI("test", "HA Humidity has no state"); + } + + ESP_LOGI("test", "Current HA Motion: %s", id(ha_motion).state ? "ON" : "OFF"); + + if (id(ha_weather).has_state()) { + ESP_LOGI("test", "Current HA Weather: %s", id(ha_weather).state.c_str()); + } else { + ESP_LOGI("test", "HA Weather has no state"); + } + + if (id(ha_empty_state).has_state()) { + ESP_LOGI("test", "HA Empty State value: %s", id(ha_empty_state).state.c_str()); + } else { + ESP_LOGI("test", "HA Empty State has no value (expected)"); + } diff --git a/tests/integration/test_api_homeassistant.py b/tests/integration/test_api_homeassistant.py new file mode 100644 index 0000000000..f69838396d --- /dev/null +++ b/tests/integration/test_api_homeassistant.py @@ -0,0 +1,305 @@ +"""Integration test for Home Assistant API functionality. + +Tests: +- Home Assistant service calls with templated values (main bug fix) +- Service calls with empty string values +- Home Assistant state reading (sensors, binary sensors, text sensors) +- Home Assistant number and switch component control +- Complex lambda expressions and string handling +""" + +from __future__ import annotations + +import asyncio +import re + +from aioesphomeapi import HomeassistantServiceCall +import pytest + +from .types import APIClientConnectedFactory, RunCompiledFunction + + +@pytest.mark.asyncio +async def test_api_homeassistant( + yaml_config: str, + run_compiled: RunCompiledFunction, + api_client_connected: APIClientConnectedFactory, +) -> None: + """Comprehensive test for Home Assistant API functionality.""" + loop = asyncio.get_running_loop() + + # Create futures for patterns that capture values + lambda_computed_future = loop.create_future() + ha_temp_state_future = loop.create_future() + ha_humidity_state_future = loop.create_future() + ha_motion_state_future = loop.create_future() + ha_weather_state_future = loop.create_future() + + # State update futures + temp_update_future = loop.create_future() + humidity_update_future = loop.create_future() + motion_update_future = loop.create_future() + weather_update_future = loop.create_future() + + # Number future + ha_number_future = loop.create_future() + + tests_complete_future = loop.create_future() + + # Patterns to match in logs - only keeping patterns that capture values + lambda_computed_pattern = re.compile(r"Lambda computed value: (\d+)") + ha_temp_state_pattern = re.compile(r"Current HA Temperature: ([\d.]+)") + ha_humidity_state_pattern = re.compile(r"Current HA Humidity: ([\d.]+)") + ha_motion_state_pattern = re.compile(r"Current HA Motion: (ON|OFF)") + ha_weather_state_pattern = re.compile(r"Current HA Weather: (\w+)") + + # State update patterns + temp_update_pattern = re.compile(r"HA Temperature state updated: ([\d.]+)") + humidity_update_pattern = re.compile(r"HA Humidity state updated: ([\d.]+)") + motion_update_pattern = re.compile(r"HA Motion state changed: (ON|OFF)") + weather_update_pattern = re.compile(r"HA Weather condition updated: (\w+)") + + # Number pattern + ha_number_pattern = re.compile(r"Setting HA number to: ([\d.]+)") + + tests_complete_pattern = re.compile(r"=== All tests completed ===") + + # Track all log lines for debugging + log_lines: list[str] = [] + + # Track HomeAssistant service calls + ha_service_calls: list[HomeassistantServiceCall] = [] + + # Service call futures organized by service name + service_call_futures = { + "light.turn_off": loop.create_future(), # basic_service_call + "light.turn_on": loop.create_future(), # templated_service_call + "notify.test": loop.create_future(), # empty_string_service_call + "climate.set_temperature": loop.create_future(), # multiple_fields_service_call + "script.test_script": loop.create_future(), # complex_lambda_service_call + "test.empty": loop.create_future(), # all_empty_service_call + "input_number.set_value": loop.create_future(), # ha_number_service_call + "switch.turn_on": loop.create_future(), # ha_switch_on_service_call + "switch.turn_off": loop.create_future(), # ha_switch_off_service_call + } + + def on_service_call(service_call: HomeassistantServiceCall) -> None: + """Capture HomeAssistant service calls.""" + ha_service_calls.append(service_call) + + # Check if this service call is one we're waiting for + if service_call.service in service_call_futures: + future = service_call_futures[service_call.service] + if not future.done(): + future.set_result(service_call) + + def check_output(line: str) -> None: + """Check log output for expected messages.""" + log_lines.append(line) + + # Check for patterns that capture values + if not lambda_computed_future.done(): + match = lambda_computed_pattern.search(line) + if match: + lambda_computed_future.set_result(match.group(1)) + elif not ha_temp_state_future.done() and ha_temp_state_pattern.search(line): + ha_temp_state_future.set_result(line) + elif not ha_humidity_state_future.done() and ha_humidity_state_pattern.search( + line + ): + ha_humidity_state_future.set_result(line) + elif not ha_motion_state_future.done() and ha_motion_state_pattern.search(line): + ha_motion_state_future.set_result(line) + elif not ha_weather_state_future.done() and ha_weather_state_pattern.search( + line + ): + ha_weather_state_future.set_result(line) + + # Check state update patterns + elif not temp_update_future.done() and temp_update_pattern.search(line): + temp_update_future.set_result(line) + elif not humidity_update_future.done() and humidity_update_pattern.search(line): + humidity_update_future.set_result(line) + elif not motion_update_future.done() and motion_update_pattern.search(line): + motion_update_future.set_result(line) + elif not weather_update_future.done() and weather_update_pattern.search(line): + weather_update_future.set_result(line) + + # Check number pattern + elif not ha_number_future.done() and ha_number_pattern.search(line): + match = ha_number_pattern.search(line) + if match: + ha_number_future.set_result(match.group(1)) + + elif not tests_complete_future.done() and tests_complete_pattern.search(line): + tests_complete_future.set_result(True) + + # Run with log monitoring + async with ( + run_compiled(yaml_config, line_callback=check_output), + api_client_connected() as client, + ): + # Verify device info + device_info = await client.device_info() + assert device_info is not None + assert device_info.name == "test-ha-api" + + # Subscribe to HomeAssistant service calls + client.subscribe_service_calls(on_service_call) + + # Send some Home Assistant states for our sensors to read + client.send_home_assistant_state("sensor.external_temperature", "", "22.5") + client.send_home_assistant_state("sensor.external_humidity", "", "65.0") + client.send_home_assistant_state("binary_sensor.external_motion", "", "ON") + client.send_home_assistant_state("weather.home", "condition", "sunny") + + # List entities and services + _, services = await client.list_entities_services() + + # Find the trigger service + trigger_service = next( + (s for s in services if s.name == "trigger_all_tests"), None + ) + assert trigger_service is not None, "trigger_all_tests service not found" + + # Execute all tests + client.execute_service(trigger_service, {}) + + # Wait for all tests to complete with appropriate timeouts + try: + # Templated service test - the main bug fix + computed_value = await asyncio.wait_for(lambda_computed_future, timeout=5.0) + # Verify the computed value is reasonable (75 * 255 / 100 = 191.25 -> 191) + assert computed_value in ["191", "192"], ( + f"Unexpected computed value: {computed_value}" + ) + + # Check state reads - verify we received the mocked values + temp_line = await asyncio.wait_for(ha_temp_state_future, timeout=5.0) + assert "Current HA Temperature: 22.5" in temp_line + + humidity_line = await asyncio.wait_for( + ha_humidity_state_future, timeout=5.0 + ) + assert "Current HA Humidity: 65.0" in humidity_line + + motion_line = await asyncio.wait_for(ha_motion_state_future, timeout=5.0) + assert "Current HA Motion: ON" in motion_line + + weather_line = await asyncio.wait_for(ha_weather_state_future, timeout=5.0) + assert "Current HA Weather: sunny" in weather_line + + # Number test + number_value = await asyncio.wait_for(ha_number_future, timeout=5.0) + assert number_value == "42.5", f"Unexpected number value: {number_value}" + + # Wait for completion + await asyncio.wait_for(tests_complete_future, timeout=5.0) + + # Now verify the protobuf messages + # 1. Basic service call + basic_call = await asyncio.wait_for( + service_call_futures["light.turn_off"], timeout=2.0 + ) + assert basic_call.service == "light.turn_off" + assert "entity_id" in basic_call.data, ( + f"entity_id not found in data: {basic_call.data}" + ) + assert basic_call.data["entity_id"] == "light.test_light", ( + f"Wrong entity_id: {basic_call.data['entity_id']}" + ) + + # 2. Templated service call - verify the temporary string issue is fixed + templated_call = await asyncio.wait_for( + service_call_futures["light.turn_on"], timeout=2.0 + ) + assert templated_call.service == "light.turn_on" + # Check the computed brightness value + assert "brightness" in templated_call.data + assert templated_call.data["brightness"] in ["191", "192"] # 75 * 255 / 100 + # Check data_template + assert "color_name" in templated_call.data_template + assert templated_call.data_template["color_name"] == "test_value" + # Check variables + assert "transition" in templated_call.variables + assert templated_call.variables["transition"] == "2.5" + + # 3. Empty string service call + empty_call = await asyncio.wait_for( + service_call_futures["notify.test"], timeout=2.0 + ) + assert empty_call.service == "notify.test" + # Verify empty strings are properly handled + assert "title" in empty_call.data and empty_call.data["title"] == "" + assert ( + "target" in empty_call.data_template + and empty_call.data_template["target"] == "" + ) + assert ( + "sound" in empty_call.variables and empty_call.variables["sound"] == "" + ) + + # 4. Multiple fields service call + multi_call = await asyncio.wait_for( + service_call_futures["climate.set_temperature"], timeout=2.0 + ) + assert multi_call.service == "climate.set_temperature" + assert multi_call.data["temperature"] == "22" + assert multi_call.data["hvac_mode"] == "heat" + assert multi_call.data_template["target_temp_high"] == "24" + assert multi_call.variables["preset_mode"] == "comfort" + + # 5. Complex lambda service call + complex_call = await asyncio.wait_for( + service_call_futures["script.test_script"], timeout=2.0 + ) + assert complex_call.service == "script.test_script" + assert complex_call.data["entity_id"] == "light.living_room" + assert complex_call.data["brightness_pct"] == "99" # 42 * 2.38 ≈ 99 + # Check message includes sensor value + assert "message" in complex_call.data_template + assert "Sensor: 42.0" in complex_call.data_template["message"] + + # 6. All empty service call + all_empty_call = await asyncio.wait_for( + service_call_futures["test.empty"], timeout=2.0 + ) + assert all_empty_call.service == "test.empty" + # All fields should be empty strings + assert all(v == "" for v in all_empty_call.data.values()) + assert all(v == "" for v in all_empty_call.data_template.values()) + assert all(v == "" for v in all_empty_call.variables.values()) + + # 7. HA Number service call + number_call = await asyncio.wait_for( + service_call_futures["input_number.set_value"], timeout=2.0 + ) + assert number_call.service == "input_number.set_value" + assert number_call.data["entity_id"] == "input_number.test_number" + # The value might be formatted with trailing zeros + assert float(number_call.data["value"]) == 42.5 + + # 8. HA Switch service calls + switch_on_call = await asyncio.wait_for( + service_call_futures["switch.turn_on"], timeout=2.0 + ) + assert switch_on_call.service == "switch.turn_on" + assert switch_on_call.data["entity_id"] == "switch.test_switch" + + switch_off_call = await asyncio.wait_for( + service_call_futures["switch.turn_off"], timeout=2.0 + ) + assert switch_off_call.service == "switch.turn_off" + assert switch_off_call.data["entity_id"] == "switch.test_switch" + + except TimeoutError as e: + # Show recent log lines for debugging + recent_logs = "\n".join(log_lines[-20:]) + service_calls_summary = "\n".join( + f"- {call.service}" for call in ha_service_calls + ) + pytest.fail( + f"Test timed out waiting for expected log pattern or service call. Error: {e}\n\n" + f"Recent log lines:\n{recent_logs}\n\n" + f"Received service calls:\n{service_calls_summary}" + )