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