From c691f01c7f7ec29c978d0937b811232170b6239e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 15 Jul 2025 15:50:32 -1000 Subject: [PATCH] Reduce flash usage by replacing ProtoSize template with specialized methods (#9487) --- esphome/components/api/api_pb2.cpp | 166 ++++++++++++++-------------- esphome/components/api/proto.h | 38 ++++++- script/api_protobuf/api_protobuf.py | 38 ++----- 3 files changed, 132 insertions(+), 110 deletions(-) diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 797a33bfbd..b6212d915e 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -184,7 +184,7 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesBinarySensorResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->device_class); @@ -201,7 +201,7 @@ void BinarySensorStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); } void BinarySensorStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + 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); ProtoSize::add_uint32_field(total_size, 1, this->device_id); @@ -225,7 +225,7 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesCoverResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_bool_field(total_size, 1, this->assumed_state); @@ -247,10 +247,10 @@ void CoverStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(6, this->device_id); } void CoverStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_enum_field(total_size, 1, static_cast(this->legacy_state)); - ProtoSize::add_fixed_field<4>(total_size, 1, this->position != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->tilt != 0.0f); + 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)); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } @@ -316,7 +316,7 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesFanResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_bool_field(total_size, 1, this->supports_oscillation); @@ -344,7 +344,7 @@ void FanStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(8, this->device_id); } void FanStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + 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->speed)); @@ -442,7 +442,7 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesLightResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); if (!this->supported_color_modes.empty()) { @@ -454,8 +454,8 @@ void ListEntitiesLightResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_bool_field(total_size, 1, this->legacy_supports_rgb); ProtoSize::add_bool_field(total_size, 1, this->legacy_supports_white_value); ProtoSize::add_bool_field(total_size, 1, this->legacy_supports_color_temperature); - ProtoSize::add_fixed_field<4>(total_size, 1, this->min_mireds != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->max_mireds != 0.0f); + ProtoSize::add_float_field(total_size, 1, this->min_mireds); + ProtoSize::add_float_field(total_size, 1, this->max_mireds); if (!this->effects.empty()) { for (const auto &it : this->effects) { ProtoSize::add_string_field_repeated(total_size, 1, it); @@ -483,18 +483,18 @@ void LightStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(14, this->device_id); } void LightStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_bool_field(total_size, 1, this->state); - ProtoSize::add_fixed_field<4>(total_size, 1, this->brightness != 0.0f); + ProtoSize::add_float_field(total_size, 1, this->brightness); ProtoSize::add_enum_field(total_size, 1, static_cast(this->color_mode)); - ProtoSize::add_fixed_field<4>(total_size, 1, this->color_brightness != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->red != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->green != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->blue != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->white != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->color_temperature != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->cold_white != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->warm_white != 0.0f); + 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); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } @@ -623,7 +623,7 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesSensorResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -644,8 +644,8 @@ void SensorStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); } void SensorStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); - ProtoSize::add_fixed_field<4>(total_size, 1, this->state != 0.0f); + 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); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } @@ -665,7 +665,7 @@ void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesSwitchResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -681,7 +681,7 @@ void SwitchStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->device_id); } void SwitchStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_bool_field(total_size, 1, this->state); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } @@ -723,7 +723,7 @@ void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesTextSensorResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -739,7 +739,7 @@ void TextSensorStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); } void TextSensorStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->state); ProtoSize::add_bool_field(total_size, 1, this->missing_state); ProtoSize::add_uint32_field(total_size, 1, this->device_id); @@ -863,7 +863,7 @@ bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { } void GetTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->epoch_seconds); } void GetTimeResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->epoch_seconds != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->epoch_seconds); } #ifdef USE_API_SERVICES bool ListEntitiesServicesArgument::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -903,7 +903,7 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesServicesResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->name); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_repeated_message(total_size, 1, this->args); } bool ExecuteServiceArgument::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -976,7 +976,7 @@ void ExecuteServiceArgument::encode(ProtoWriteBuffer buffer) const { void ExecuteServiceArgument::calculate_size(uint32_t &total_size) const { ProtoSize::add_bool_field(total_size, 1, this->bool_); ProtoSize::add_int32_field(total_size, 1, this->legacy_int); - ProtoSize::add_fixed_field<4>(total_size, 1, this->float_ != 0.0f); + ProtoSize::add_float_field(total_size, 1, this->float_); ProtoSize::add_string_field(total_size, 1, this->string_); ProtoSize::add_sint32_field(total_size, 1, this->int_); if (!this->bool_array.empty()) { @@ -1033,7 +1033,7 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesCameraResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_bool_field(total_size, 1, this->disabled_by_default); @@ -1048,7 +1048,7 @@ void CameraImageResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); } void CameraImageResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->data); ProtoSize::add_bool_field(total_size, 1, this->done); ProtoSize::add_uint32_field(total_size, 1, this->device_id); @@ -1110,7 +1110,7 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesClimateResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_bool_field(total_size, 1, this->supports_current_temperature); @@ -1120,9 +1120,9 @@ void ListEntitiesClimateResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_enum_field_repeated(total_size, 1, static_cast(it)); } } - ProtoSize::add_fixed_field<4>(total_size, 1, this->visual_min_temperature != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->visual_max_temperature != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->visual_target_temperature_step != 0.0f); + 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->legacy_supports_away); ProtoSize::add_bool_field(total_size, 1, this->supports_action); if (!this->supported_fan_modes.empty()) { @@ -1153,11 +1153,11 @@ void ListEntitiesClimateResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_bool_field(total_size, 2, this->disabled_by_default); ProtoSize::add_string_field(total_size, 2, this->icon); ProtoSize::add_enum_field(total_size, 2, static_cast(this->entity_category)); - ProtoSize::add_fixed_field<4>(total_size, 2, this->visual_current_temperature_step != 0.0f); + 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_fixed_field<4>(total_size, 2, this->visual_min_humidity != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 2, this->visual_max_humidity != 0.0f); + ProtoSize::add_float_field(total_size, 2, this->visual_min_humidity); + ProtoSize::add_float_field(total_size, 2, this->visual_max_humidity); ProtoSize::add_uint32_field(total_size, 2, this->device_id); } void ClimateStateResponse::encode(ProtoWriteBuffer buffer) const { @@ -1179,12 +1179,12 @@ void ClimateStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(16, this->device_id); } void ClimateStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_enum_field(total_size, 1, static_cast(this->mode)); - ProtoSize::add_fixed_field<4>(total_size, 1, this->current_temperature != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->target_temperature != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->target_temperature_low != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->target_temperature_high != 0.0f); + 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_bool_field(total_size, 1, this->unused_legacy_away); ProtoSize::add_enum_field(total_size, 1, static_cast(this->action)); ProtoSize::add_enum_field(total_size, 1, static_cast(this->fan_mode)); @@ -1192,8 +1192,8 @@ void ClimateStateResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->custom_fan_mode); ProtoSize::add_enum_field(total_size, 1, static_cast(this->preset)); ProtoSize::add_string_field(total_size, 1, this->custom_preset); - ProtoSize::add_fixed_field<4>(total_size, 1, this->current_humidity != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->target_humidity != 0.0f); + ProtoSize::add_float_field(total_size, 1, this->current_humidity); + ProtoSize::add_float_field(total_size, 1, this->target_humidity); ProtoSize::add_uint32_field(total_size, 2, this->device_id); } bool ClimateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -1309,13 +1309,13 @@ void ListEntitiesNumberResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesNumberResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); - ProtoSize::add_fixed_field<4>(total_size, 1, this->min_value != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->max_value != 0.0f); - ProtoSize::add_fixed_field<4>(total_size, 1, this->step != 0.0f); + 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); @@ -1330,8 +1330,8 @@ void NumberStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); } void NumberStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); - ProtoSize::add_fixed_field<4>(total_size, 1, this->state != 0.0f); + 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); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } @@ -1375,7 +1375,7 @@ void ListEntitiesSelectResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesSelectResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -1395,7 +1395,7 @@ void SelectStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); } void SelectStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->state); ProtoSize::add_bool_field(total_size, 1, this->missing_state); ProtoSize::add_uint32_field(total_size, 1, this->device_id); @@ -1449,7 +1449,7 @@ void ListEntitiesSirenResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesSirenResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -1470,7 +1470,7 @@ void SirenStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->device_id); } void SirenStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_bool_field(total_size, 1, this->state); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } @@ -1543,7 +1543,7 @@ void ListEntitiesLockResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesLockResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -1561,7 +1561,7 @@ void LockStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->device_id); } void LockStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_enum_field(total_size, 1, static_cast(this->state)); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } @@ -1616,7 +1616,7 @@ void ListEntitiesButtonResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesButtonResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -1706,7 +1706,7 @@ void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesMediaPlayerResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -1724,9 +1724,9 @@ void MediaPlayerStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(5, this->device_id); } void MediaPlayerStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_enum_field(total_size, 1, static_cast(this->state)); - ProtoSize::add_fixed_field<4>(total_size, 1, this->volume != 0.0f); + ProtoSize::add_float_field(total_size, 1, this->volume); ProtoSize::add_bool_field(total_size, 1, this->muted); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } @@ -2326,7 +2326,7 @@ void VoiceAssistantAudioSettings::encode(ProtoWriteBuffer buffer) const { 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_fixed_field<4>(total_size, 1, this->volume_multiplier != 0.0f); + ProtoSize::add_float_field(total_size, 1, this->volume_multiplier); } void VoiceAssistantRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->start); @@ -2564,7 +2564,7 @@ void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer buffer) cons } void ListEntitiesAlarmControlPanelResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -2581,7 +2581,7 @@ void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->device_id); } void AlarmControlPanelStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_enum_field(total_size, 1, static_cast(this->state)); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } @@ -2636,7 +2636,7 @@ void ListEntitiesTextResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesTextResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -2655,7 +2655,7 @@ void TextStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); } void TextStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->state); ProtoSize::add_bool_field(total_size, 1, this->missing_state); ProtoSize::add_uint32_field(total_size, 1, this->device_id); @@ -2704,7 +2704,7 @@ void ListEntitiesDateResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesDateResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -2721,7 +2721,7 @@ void DateStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(6, this->device_id); } void DateStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + 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); @@ -2771,7 +2771,7 @@ void ListEntitiesTimeResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesTimeResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -2788,7 +2788,7 @@ void TimeStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(6, this->device_id); } void TimeStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + 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); @@ -2842,7 +2842,7 @@ void ListEntitiesEventResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesEventResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -2862,7 +2862,7 @@ void EventResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(3, this->device_id); } void EventResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->event_type); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } @@ -2884,7 +2884,7 @@ void ListEntitiesValveResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesValveResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -2903,8 +2903,8 @@ void ValveStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); } void ValveStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); - ProtoSize::add_fixed_field<4>(total_size, 1, this->position != 0.0f); + 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)); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } @@ -2951,7 +2951,7 @@ void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesDateTimeResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -2966,9 +2966,9 @@ void DateTimeStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(4, this->device_id); } void DateTimeStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_bool_field(total_size, 1, this->missing_state); - ProtoSize::add_fixed_field<4>(total_size, 1, this->epoch_seconds != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->epoch_seconds); ProtoSize::add_uint32_field(total_size, 1, this->device_id); } bool DateTimeCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -3009,7 +3009,7 @@ void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer buffer) const { } void ListEntitiesUpdateResponse::calculate_size(uint32_t &total_size) const { ProtoSize::add_string_field(total_size, 1, this->object_id); - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + ProtoSize::add_fixed32_field(total_size, 1, this->key); ProtoSize::add_string_field(total_size, 1, this->name); ProtoSize::add_string_field(total_size, 1, this->unique_id); ProtoSize::add_string_field(total_size, 1, this->icon); @@ -3032,11 +3032,11 @@ void UpdateStateResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(11, this->device_id); } void UpdateStateResponse::calculate_size(uint32_t &total_size) const { - ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0); + 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_fixed_field<4>(total_size, 1, this->progress != 0.0f); + ProtoSize::add_float_field(total_size, 1, this->progress); ProtoSize::add_string_field(total_size, 1, this->current_version); ProtoSize::add_string_field(total_size, 1, this->latest_version); ProtoSize::add_string_field(total_size, 1, this->title); diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index f8539f4be1..83a03ba628 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -189,9 +189,9 @@ class ProtoWriteBuffer { * @param field_id Field number (tag) in the protobuf message * @param type Wire type value: * - 0: Varint (int32, int64, uint32, uint64, sint32, sint64, bool, enum) - * - 1: 64-bit (fixed64, sfixed64, double) * - 2: Length-delimited (string, bytes, embedded messages, packed repeated fields) * - 5: 32-bit (fixed32, sfixed32, float) + * - Note: Wire type 1 (64-bit fixed) is not supported * * Following https://protobuf.dev/programming-guides/encoding/#structure */ @@ -540,6 +540,42 @@ class ProtoSize { total_size += field_id_size + NumBytes; } + /** + * @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) { + if (value != 0.0f) { + total_size += field_id_size + 4; + } + } + + // NOTE: add_double_field removed - wire type 1 (64-bit: double) not supported + // to reduce overhead on embedded systems + + /** + * @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) { + if (value != 0) { + total_size += field_id_size + 4; + } + } + + // NOTE: add_fixed64_field removed - wire type 1 (64-bit: fixed64) not supported + // to reduce overhead on embedded systems + + /** + * @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) { + if (value != 0) { + total_size += field_id_size + 4; + } + } + + // 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 * diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index f6e18d529d..fddddc7399 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -240,26 +240,6 @@ class TypeInfo(ABC): value = value_expr if value_expr else name return f"ProtoSize::{method}(total_size, {field_id_size}, {value});" - def _get_fixed_size_calculation( - self, name: str, force: bool, num_bytes: int, zero_check: str - ) -> str: - """Helper for fixed-size field calculations. - - Args: - name: Field name - force: Whether this is for a repeated field - num_bytes: Number of bytes (4 or 8) - zero_check: Expression to check for zero value (e.g., "!= 0.0f") - """ - field_id_size = self.calculate_field_id_size() - # Fixed-size repeated fields are handled differently in RepeatedTypeInfo - # so we should never get force=True here - assert not force, ( - "Fixed-size repeated fields should be handled by RepeatedTypeInfo" - ) - method = f"add_fixed_field<{num_bytes}>" - return f"ProtoSize::{method}(total_size, {field_id_size}, {name} {zero_check});" - @abstractmethod def get_size_calculation(self, name: str, force: bool = False) -> str: """Calculate the size needed for encoding this field. @@ -345,7 +325,8 @@ class DoubleType(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_fixed_size_calculation(name, force, 8, "!= 0.0") + field_id_size = self.calculate_field_id_size() + return f"ProtoSize::add_double_field(total_size, {field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8 @@ -368,7 +349,8 @@ class FloatType(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_fixed_size_calculation(name, force, 4, "!= 0.0f") + field_id_size = self.calculate_field_id_size() + return f"ProtoSize::add_float_field(total_size, {field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -451,7 +433,8 @@ class Fixed64Type(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_fixed_size_calculation(name, force, 8, "!= 0") + field_id_size = self.calculate_field_id_size() + return f"ProtoSize::add_fixed64_field(total_size, {field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8 @@ -474,7 +457,8 @@ class Fixed32Type(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_fixed_size_calculation(name, force, 4, "!= 0") + field_id_size = self.calculate_field_id_size() + return f"ProtoSize::add_fixed32_field(total_size, {field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -663,7 +647,8 @@ class SFixed32Type(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_fixed_size_calculation(name, force, 4, "!= 0") + field_id_size = self.calculate_field_id_size() + return f"ProtoSize::add_sfixed32_field(total_size, {field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -686,7 +671,8 @@ class SFixed64Type(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_fixed_size_calculation(name, force, 8, "!= 0") + field_id_size = self.calculate_field_id_size() + return f"ProtoSize::add_sfixed64_field(total_size, {field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8