diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 6db5dcf97e..038c1c7800 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -2353,12 +2353,11 @@ uint32_t BluetoothLERawAdvertisementsResponse::calculate_size() const { uint32_t size = 0; for (uint16_t i = 0; i < this->advertisements_len; i++) { auto &sub_msg = this->advertisements[i]; - uint32_t sub_size = 0; - sub_size += ProtoSize::calc_uint64_force(1, sub_msg.address); - sub_size += ProtoSize::calc_sint32_force(1, sub_msg.rssi); - sub_size += sub_msg.address_type ? 2 : 0; - sub_size += 2 + sub_msg.data_len; - size += 2 + sub_size; + size += 2; + size += ProtoSize::calc_uint64_force(1, sub_msg.address); + size += ProtoSize::calc_sint32_force(1, sub_msg.rssi); + size += sub_msg.address_type ? 2 : 0; + size += 2 + sub_msg.data_len; } return size; } diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 6f92e4cb6d..746402f8c6 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -1628,7 +1628,8 @@ def _generate_inline_size_block( lines = [] lines.append(f"auto &sub_msg = {element};") - lines.append("uint32_t sub_size = 0;") + # 1 byte tag + 1 byte length (guaranteed < 128 by validation) + lines.append("size += 2;") for field in sub_desc.field: if field.options.deprecated: @@ -1636,14 +1637,10 @@ def _generate_inline_size_block( ti = create_field_type_info(field, needs_decode=False, needs_encode=True) force = get_field_opt(field, pb.force, False) size_line = ti.get_size_calculation(f"sub_msg.{ti.field_name}", force) - # Replace "size +=" with "sub_size +=" for the local accumulator - size_line = size_line.replace("size +=", "sub_size +=") # Replace hardcoded this-> references (e.g., FixedArrayBytesType uses this->field_len) size_line = size_line.replace("this->", "sub_msg.") lines.extend(size_line.split("\n")) - # 1 byte tag + 1 byte length (guaranteed < 128) + body - lines.append("size += 2 + sub_size;") return "\n".join(lines)