diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index ed8c3e7bde..a138168ca1 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -38,12 +38,12 @@ void HelloResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(4, this->name); } uint32_t HelloResponse::calculate_size() const { - ProtoSize size; - size.add_uint32(1, this->api_version_major); - size.add_uint32(1, this->api_version_minor); - size.add_length(1, this->server_info.size()); - size.add_length(1, this->name.size()); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint32(1, this->api_version_major); + size += ProtoSize::uint32(1, this->api_version_minor); + size += ProtoSize::length(1, this->server_info.size()); + size += ProtoSize::length(1, this->name.size()); + return size; } #ifdef USE_AREAS void AreaInfo::encode(ProtoWriteBuffer &buffer) const { @@ -51,10 +51,10 @@ void AreaInfo::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(2, this->name); } uint32_t AreaInfo::calculate_size() const { - ProtoSize size; - size.add_uint32(1, this->area_id); - size.add_length(1, this->name.size()); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint32(1, this->area_id); + size += ProtoSize::length(1, this->name.size()); + return size; } #endif #ifdef USE_DEVICES @@ -64,11 +64,11 @@ void DeviceInfo::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(3, this->area_id); } uint32_t DeviceInfo::calculate_size() const { - ProtoSize size; - size.add_uint32(1, this->device_id); - size.add_length(1, this->name.size()); - size.add_uint32(1, this->area_id); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint32(1, this->device_id); + size += ProtoSize::length(1, this->name.size()); + size += ProtoSize::uint32(1, this->area_id); + return size; } #endif void DeviceInfoResponse::encode(ProtoWriteBuffer &buffer) const { @@ -127,61 +127,61 @@ void DeviceInfoResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t DeviceInfoResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->name.size()); - size.add_length(1, this->mac_address.size()); - size.add_length(1, this->esphome_version.size()); - size.add_length(1, this->compilation_time.size()); - size.add_length(1, this->model.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->name.size()); + size += ProtoSize::length(1, this->mac_address.size()); + size += ProtoSize::length(1, this->esphome_version.size()); + size += ProtoSize::length(1, this->compilation_time.size()); + size += ProtoSize::length(1, this->model.size()); #ifdef USE_DEEP_SLEEP - size.add_bool(1, this->has_deep_sleep); + size += ProtoSize::bool_(1, this->has_deep_sleep); #endif #ifdef ESPHOME_PROJECT_NAME - size.add_length(1, this->project_name.size()); + size += ProtoSize::length(1, this->project_name.size()); #endif #ifdef ESPHOME_PROJECT_NAME - size.add_length(1, this->project_version.size()); + size += ProtoSize::length(1, this->project_version.size()); #endif #ifdef USE_WEBSERVER - size.add_uint32(1, this->webserver_port); + size += ProtoSize::uint32(1, this->webserver_port); #endif #ifdef USE_BLUETOOTH_PROXY - size.add_uint32(1, this->bluetooth_proxy_feature_flags); + size += ProtoSize::uint32(1, this->bluetooth_proxy_feature_flags); #endif - size.add_length(1, this->manufacturer.size()); - size.add_length(1, this->friendly_name.size()); + size += ProtoSize::length(1, this->manufacturer.size()); + size += ProtoSize::length(1, this->friendly_name.size()); #ifdef USE_VOICE_ASSISTANT - size.add_uint32(2, this->voice_assistant_feature_flags); + size += ProtoSize::uint32(2, this->voice_assistant_feature_flags); #endif #ifdef USE_AREAS - size.add_length(2, this->suggested_area.size()); + size += ProtoSize::length(2, this->suggested_area.size()); #endif #ifdef USE_BLUETOOTH_PROXY - size.add_length(2, this->bluetooth_mac_address.size()); + size += ProtoSize::length(2, this->bluetooth_mac_address.size()); #endif #ifdef USE_API_NOISE - size.add_bool(2, this->api_encryption_supported); + size += ProtoSize::bool_(2, this->api_encryption_supported); #endif #ifdef USE_DEVICES for (const auto &it : this->devices) { - size.add_message_object_force(2, it); + size += ProtoSize::message_force(2, it.calculate_size()); } #endif #ifdef USE_AREAS for (const auto &it : this->areas) { - size.add_message_object_force(2, it); + size += ProtoSize::message_force(2, it.calculate_size()); } #endif #ifdef USE_AREAS - size.add_message_object(2, this->area); + size += ProtoSize::message(2, this->area.calculate_size()); #endif #ifdef USE_ZWAVE_PROXY - size.add_uint32(2, this->zwave_proxy_feature_flags); + size += ProtoSize::uint32(2, this->zwave_proxy_feature_flags); #endif #ifdef USE_ZWAVE_PROXY - size.add_uint32(2, this->zwave_home_id); + size += ProtoSize::uint32(2, this->zwave_home_id); #endif - return size.get_size(); + return size; } #ifdef USE_BINARY_SENSOR void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer &buffer) const { @@ -200,21 +200,21 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesBinarySensorResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); - size.add_length(1, this->device_class.size()); - size.add_bool(1, this->is_status_binary_sensor); - size.add_bool(1, this->disabled_by_default); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); + size += ProtoSize::length(1, this->device_class.size()); + size += ProtoSize::bool_(1, this->is_status_binary_sensor); + size += ProtoSize::bool_(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_uint32(1, static_cast(this->entity_category)); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void BinarySensorStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -225,14 +225,14 @@ void BinarySensorStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t BinarySensorStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_bool(1, this->state); - size.add_bool(1, this->missing_state); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::bool_(1, this->state); + size += ProtoSize::bool_(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } #endif #ifdef USE_COVER @@ -255,24 +255,24 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesCoverResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); - size.add_bool(1, this->assumed_state); - size.add_bool(1, this->supports_position); - size.add_bool(1, this->supports_tilt); - size.add_length(1, this->device_class.size()); - size.add_bool(1, this->disabled_by_default); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); + size += ProtoSize::bool_(1, this->assumed_state); + size += ProtoSize::bool_(1, this->supports_position); + size += ProtoSize::bool_(1, this->supports_tilt); + size += ProtoSize::length(1, this->device_class.size()); + size += ProtoSize::bool_(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_uint32(1, static_cast(this->entity_category)); - size.add_bool(1, this->supports_stop); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::bool_(1, this->supports_stop); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void CoverStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -284,15 +284,15 @@ void CoverStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t CoverStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_float(1, this->position); - size.add_float(1, this->tilt); - size.add_uint32(1, static_cast(this->current_operation)); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::float_(1, this->position); + size += ProtoSize::float_(1, this->tilt); + size += ProtoSize::uint32(1, static_cast(this->current_operation)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool CoverCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -354,28 +354,28 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesFanResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.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); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); + size += ProtoSize::bool_(1, this->supports_oscillation); + size += ProtoSize::bool_(1, this->supports_speed); + size += ProtoSize::bool_(1, this->supports_direction); + size += ProtoSize::int32(1, this->supported_speed_count); + size += ProtoSize::bool_(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_uint32(1, static_cast(this->entity_category)); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); if (!this->supported_preset_modes->empty()) { for (const char *it : *this->supported_preset_modes) { - size.add_length_force(1, strlen(it)); + size += ProtoSize::length_force(1, strlen(it)); } } #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void FanStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -389,17 +389,17 @@ void FanStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t FanStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_bool(1, this->state); - size.add_bool(1, this->oscillating); - size.add_uint32(1, static_cast(this->direction)); - size.add_int32(1, this->speed_level); - size.add_length(1, this->preset_mode.size()); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::bool_(1, this->state); + size += ProtoSize::bool_(1, this->oscillating); + size += ProtoSize::uint32(1, static_cast(this->direction)); + size += ProtoSize::int32(1, this->speed_level); + size += ProtoSize::length(1, this->preset_mode.size()); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool FanCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -485,31 +485,31 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesLightResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); if (!this->supported_color_modes->empty()) { for (const auto &it : *this->supported_color_modes) { - size.add_uint32_force(1, static_cast(it)); + size += ProtoSize::uint32_force(1, static_cast(it)); } } - size.add_float(1, this->min_mireds); - size.add_float(1, this->max_mireds); + size += ProtoSize::float_(1, this->min_mireds); + size += ProtoSize::float_(1, this->max_mireds); if (!this->effects->empty()) { for (const char *it : *this->effects) { - size.add_length_force(1, strlen(it)); + size += ProtoSize::length_force(1, strlen(it)); } } - size.add_bool(1, this->disabled_by_default); + size += ProtoSize::bool_(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_uint32(1, static_cast(this->entity_category)); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32(2, this->device_id); + size += ProtoSize::uint32(2, this->device_id); #endif - return size.get_size(); + return size; } void LightStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -530,24 +530,24 @@ void LightStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t LightStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_bool(1, this->state); - size.add_float(1, this->brightness); - 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); - 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_length(1, this->effect.size()); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::bool_(1, this->state); + size += ProtoSize::float_(1, this->brightness); + size += ProtoSize::uint32(1, static_cast(this->color_mode)); + size += ProtoSize::float_(1, this->color_brightness); + size += ProtoSize::float_(1, this->red); + size += ProtoSize::float_(1, this->green); + size += ProtoSize::float_(1, this->blue); + size += ProtoSize::float_(1, this->white); + size += ProtoSize::float_(1, this->color_temperature); + size += ProtoSize::float_(1, this->cold_white); + size += ProtoSize::float_(1, this->warm_white); + size += ProtoSize::length(1, this->effect.size()); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool LightCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -678,24 +678,24 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesSensorResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_length(1, this->unit_of_measurement.size()); - size.add_int32(1, this->accuracy_decimals); - size.add_bool(1, this->force_update); - size.add_length(1, this->device_class.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)); + size += ProtoSize::length(1, this->unit_of_measurement.size()); + size += ProtoSize::int32(1, this->accuracy_decimals); + size += ProtoSize::bool_(1, this->force_update); + size += ProtoSize::length(1, this->device_class.size()); + size += ProtoSize::uint32(1, static_cast(this->state_class)); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void SensorStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -706,14 +706,14 @@ void SensorStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t SensorStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_float(1, this->state); - size.add_bool(1, this->missing_state); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::float_(1, this->state); + size += ProtoSize::bool_(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } #endif #ifdef USE_SWITCH @@ -733,21 +733,21 @@ void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesSwitchResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.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_length(1, this->device_class.size()); + size += ProtoSize::bool_(1, this->assumed_state); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::length(1, this->device_class.size()); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void SwitchStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -757,13 +757,13 @@ void SwitchStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t SwitchStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_bool(1, this->state); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::bool_(1, this->state); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool SwitchCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -807,20 +807,20 @@ void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesTextSensorResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); - size.add_uint32(1, static_cast(this->entity_category)); - size.add_length(1, this->device_class.size()); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::length(1, this->device_class.size()); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void TextSensorStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -831,14 +831,14 @@ void TextSensorStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t TextSensorStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_length(1, this->state.size()); - size.add_bool(1, this->missing_state); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->state.size()); + size += ProtoSize::bool_(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } #endif bool SubscribeLogsRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { @@ -859,10 +859,10 @@ void SubscribeLogsResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_bytes(3, this->message_ptr_, this->message_len_); } uint32_t SubscribeLogsResponse::calculate_size() const { - ProtoSize size; - size.add_uint32(1, static_cast(this->level)); - size.add_length(1, this->message_len_); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint32(1, static_cast(this->level)); + size += ProtoSize::length(1, this->message_len_); + return size; } #ifdef USE_API_NOISE bool NoiseEncryptionSetKeyRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) { @@ -879,9 +879,9 @@ bool NoiseEncryptionSetKeyRequest::decode_length(uint32_t field_id, ProtoLengthD } void NoiseEncryptionSetKeyResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_bool(1, this->success); } uint32_t NoiseEncryptionSetKeyResponse::calculate_size() const { - ProtoSize size; - size.add_bool(1, this->success); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::bool_(1, this->success); + return size; } #endif #ifdef USE_API_HOMEASSISTANT_SERVICES @@ -890,10 +890,10 @@ void HomeassistantServiceMap::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(2, this->value); } uint32_t HomeassistantServiceMap::calculate_size() const { - ProtoSize size; - size.add_length(1, this->key.size()); - size.add_length(1, this->value.size()); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::length(1, this->key.size()); + size += ProtoSize::length(1, this->value.size()); + return size; } void HomeassistantActionRequest::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(1, this->service); @@ -918,22 +918,34 @@ void HomeassistantActionRequest::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t HomeassistantActionRequest::calculate_size() const { - ProtoSize size; - size.add_length(1, this->service.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(1, this->is_event); + uint32_t size = 0; + size += ProtoSize::length(1, this->service.size()); + if (!this->data.empty()) { + for (const auto &it : this->data) { + size += ProtoSize::message_force(1, it.calculate_size()); + } + } + if (!this->data_template.empty()) { + for (const auto &it : this->data_template) { + size += ProtoSize::message_force(1, it.calculate_size()); + } + } + if (!this->variables.empty()) { + for (const auto &it : this->variables) { + size += ProtoSize::message_force(1, it.calculate_size()); + } + } + size += ProtoSize::bool_(1, this->is_event); #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES - size.add_uint32(1, this->call_id); + size += ProtoSize::uint32(1, this->call_id); #endif #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON - size.add_bool(1, this->wants_response); + size += ProtoSize::bool_(1, this->wants_response); #endif #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON - size.add_length(1, this->response_template.size()); + size += ProtoSize::length(1, this->response_template.size()); #endif - return size.get_size(); + return size; } #endif #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES @@ -976,11 +988,11 @@ void SubscribeHomeAssistantStateResponse::encode(ProtoWriteBuffer &buffer) const buffer.encode_bool(3, this->once); } uint32_t SubscribeHomeAssistantStateResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->entity_id.size()); - size.add_length(1, this->attribute.size()); - size.add_bool(1, this->once); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::length(1, this->entity_id.size()); + size += ProtoSize::length(1, this->attribute.size()); + size += ProtoSize::bool_(1, this->once); + return size; } bool HomeAssistantStateResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { @@ -1083,10 +1095,10 @@ void ListEntitiesServicesArgument::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(2, static_cast(this->type)); } uint32_t ListEntitiesServicesArgument::calculate_size() const { - ProtoSize size; - size.add_length(1, this->name.size()); - size.add_uint32(1, static_cast(this->type)); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::length(1, this->name.size()); + size += ProtoSize::uint32(1, static_cast(this->type)); + return size; } void ListEntitiesServicesResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(1, this->name); @@ -1097,12 +1109,16 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(4, static_cast(this->supports_response)); } uint32_t ListEntitiesServicesResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->name.size()); - size.add_fixed32(1, this->key); - size.add_repeated_message(1, this->args); - size.add_uint32(1, static_cast(this->supports_response)); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::length(1, this->name.size()); + size += ProtoSize::fixed32(1, this->key); + if (!this->args.empty()) { + for (const auto &it : this->args) { + size += ProtoSize::message_force(1, it.calculate_size()); + } + } + size += ProtoSize::uint32(1, static_cast(this->supports_response)); + return size; } bool ExecuteServiceArgument::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1218,14 +1234,14 @@ void ExecuteServiceResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ExecuteServiceResponse::calculate_size() const { - ProtoSize size; - size.add_uint32(1, this->call_id); - size.add_bool(1, this->success); - size.add_length(1, this->error_message.size()); + uint32_t size = 0; + size += ProtoSize::uint32(1, this->call_id); + size += ProtoSize::bool_(1, this->success); + size += ProtoSize::length(1, this->error_message.size()); #ifdef USE_API_USER_DEFINED_ACTION_RESPONSES_JSON - size.add_length(1, this->response_data_len); + size += ProtoSize::length(1, this->response_data_len); #endif - return size.get_size(); + return size; } #endif #ifdef USE_CAMERA @@ -1243,19 +1259,19 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesCameraResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); - size.add_bool(1, this->disabled_by_default); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); + size += ProtoSize::bool_(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_uint32(1, static_cast(this->entity_category)); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void CameraImageResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -1266,14 +1282,14 @@ void CameraImageResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t CameraImageResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_length(1, this->data_len_); - size.add_bool(1, this->done); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->data_len_); + size += ProtoSize::bool_(1, this->done); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool CameraImageRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1334,61 +1350,61 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(27, this->feature_flags); } uint32_t ListEntitiesClimateResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); - size.add_bool(1, this->supports_current_temperature); - size.add_bool(1, this->supports_two_point_target_temperature); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); + size += ProtoSize::bool_(1, this->supports_current_temperature); + size += ProtoSize::bool_(1, this->supports_two_point_target_temperature); if (!this->supported_modes->empty()) { for (const auto &it : *this->supported_modes) { - size.add_uint32_force(1, static_cast(it)); + size += ProtoSize::uint32_force(1, static_cast(it)); } } - 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); + size += ProtoSize::float_(1, this->visual_min_temperature); + size += ProtoSize::float_(1, this->visual_max_temperature); + size += ProtoSize::float_(1, this->visual_target_temperature_step); + size += ProtoSize::bool_(1, this->supports_action); if (!this->supported_fan_modes->empty()) { for (const auto &it : *this->supported_fan_modes) { - size.add_uint32_force(1, static_cast(it)); + size += ProtoSize::uint32_force(1, static_cast(it)); } } if (!this->supported_swing_modes->empty()) { for (const auto &it : *this->supported_swing_modes) { - size.add_uint32_force(1, static_cast(it)); + size += ProtoSize::uint32_force(1, static_cast(it)); } } if (!this->supported_custom_fan_modes->empty()) { for (const char *it : *this->supported_custom_fan_modes) { - size.add_length_force(1, strlen(it)); + size += ProtoSize::length_force(1, strlen(it)); } } if (!this->supported_presets->empty()) { for (const auto &it : *this->supported_presets) { - size.add_uint32_force(2, static_cast(it)); + size += ProtoSize::uint32_force(2, static_cast(it)); } } if (!this->supported_custom_presets->empty()) { for (const char *it : *this->supported_custom_presets) { - size.add_length_force(2, strlen(it)); + size += ProtoSize::length_force(2, strlen(it)); } } - size.add_bool(2, this->disabled_by_default); + size += ProtoSize::bool_(2, this->disabled_by_default); #ifdef USE_ENTITY_ICON - size.add_length(2, this->icon.size()); + size += ProtoSize::length(2, this->icon.size()); #endif - 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); - size.add_float(2, this->visual_min_humidity); - size.add_float(2, this->visual_max_humidity); + size += ProtoSize::uint32(2, static_cast(this->entity_category)); + size += ProtoSize::float_(2, this->visual_current_temperature_step); + size += ProtoSize::bool_(2, this->supports_current_humidity); + size += ProtoSize::bool_(2, this->supports_target_humidity); + size += ProtoSize::float_(2, this->visual_min_humidity); + size += ProtoSize::float_(2, this->visual_max_humidity); #ifdef USE_DEVICES - size.add_uint32(2, this->device_id); + size += ProtoSize::uint32(2, this->device_id); #endif - size.add_uint32(2, this->feature_flags); - return size.get_size(); + size += ProtoSize::uint32(2, this->feature_flags); + return size; } void ClimateStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -1410,25 +1426,25 @@ void ClimateStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ClimateStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - 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_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_length(1, this->custom_fan_mode.size()); - size.add_uint32(1, static_cast(this->preset)); - size.add_length(1, this->custom_preset.size()); - size.add_float(1, this->current_humidity); - size.add_float(1, this->target_humidity); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::uint32(1, static_cast(this->mode)); + size += ProtoSize::float_(1, this->current_temperature); + size += ProtoSize::float_(1, this->target_temperature); + size += ProtoSize::float_(1, this->target_temperature_low); + size += ProtoSize::float_(1, this->target_temperature_high); + size += ProtoSize::uint32(1, static_cast(this->action)); + size += ProtoSize::uint32(1, static_cast(this->fan_mode)); + size += ProtoSize::uint32(1, static_cast(this->swing_mode)); + size += ProtoSize::length(1, this->custom_fan_mode.size()); + size += ProtoSize::uint32(1, static_cast(this->preset)); + size += ProtoSize::length(1, this->custom_preset.size()); + size += ProtoSize::float_(1, this->current_humidity); + size += ProtoSize::float_(1, this->target_humidity); #ifdef USE_DEVICES - size.add_uint32(2, this->device_id); + size += ProtoSize::uint32(2, this->device_id); #endif - return size.get_size(); + return size; } bool ClimateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1544,28 +1560,28 @@ void ListEntitiesWaterHeaterResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(12, this->supported_features); } uint32_t ListEntitiesWaterHeaterResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); - size.add_uint32(1, static_cast(this->entity_category)); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - size.add_float(1, this->min_temperature); - size.add_float(1, this->max_temperature); - size.add_float(1, this->target_temperature_step); + size += ProtoSize::float_(1, this->min_temperature); + size += ProtoSize::float_(1, this->max_temperature); + size += ProtoSize::float_(1, this->target_temperature_step); if (!this->supported_modes->empty()) { for (const auto &it : *this->supported_modes) { - size.add_uint32_force(1, static_cast(it)); + size += ProtoSize::uint32_force(1, static_cast(it)); } } - size.add_uint32(1, this->supported_features); - return size.get_size(); + size += ProtoSize::uint32(1, this->supported_features); + return size; } void WaterHeaterStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -1580,18 +1596,18 @@ void WaterHeaterStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_float(8, this->target_temperature_high); } uint32_t WaterHeaterStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_float(1, this->current_temperature); - size.add_float(1, this->target_temperature); - size.add_uint32(1, static_cast(this->mode)); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::float_(1, this->current_temperature); + size += ProtoSize::float_(1, this->target_temperature); + size += ProtoSize::uint32(1, static_cast(this->mode)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - size.add_uint32(1, this->state); - size.add_float(1, this->target_temperature_low); - size.add_float(1, this->target_temperature_high); - return size.get_size(); + size += ProtoSize::uint32(1, this->state); + size += ProtoSize::float_(1, this->target_temperature_low); + size += ProtoSize::float_(1, this->target_temperature_high); + return size; } bool WaterHeaterCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1655,25 +1671,25 @@ void ListEntitiesNumberResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesNumberResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.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_length(1, this->unit_of_measurement.size()); - size.add_uint32(1, static_cast(this->mode)); - size.add_length(1, this->device_class.size()); + size += ProtoSize::float_(1, this->min_value); + size += ProtoSize::float_(1, this->max_value); + size += ProtoSize::float_(1, this->step); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::length(1, this->unit_of_measurement.size()); + size += ProtoSize::uint32(1, static_cast(this->mode)); + size += ProtoSize::length(1, this->device_class.size()); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void NumberStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -1684,14 +1700,14 @@ void NumberStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t NumberStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_float(1, this->state); - size.add_bool(1, this->missing_state); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::float_(1, this->state); + size += ProtoSize::bool_(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool NumberCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1737,24 +1753,24 @@ void ListEntitiesSelectResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesSelectResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif if (!this->options->empty()) { for (const char *it : *this->options) { - size.add_length_force(1, strlen(it)); + size += ProtoSize::length_force(1, strlen(it)); } } - size.add_bool(1, this->disabled_by_default); - size.add_uint32(1, static_cast(this->entity_category)); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void SelectStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -1765,14 +1781,14 @@ void SelectStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t SelectStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_length(1, this->state.size()); - size.add_bool(1, this->missing_state); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->state.size()); + size += ProtoSize::bool_(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool SelectCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1828,26 +1844,26 @@ void ListEntitiesSirenResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesSirenResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); + size += ProtoSize::bool_(1, this->disabled_by_default); if (!this->tones->empty()) { for (const char *it : *this->tones) { - size.add_length_force(1, strlen(it)); + size += ProtoSize::length_force(1, strlen(it)); } } - size.add_bool(1, this->supports_duration); - size.add_bool(1, this->supports_volume); - size.add_uint32(1, static_cast(this->entity_category)); + size += ProtoSize::bool_(1, this->supports_duration); + size += ProtoSize::bool_(1, this->supports_volume); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void SirenStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -1857,13 +1873,13 @@ void SirenStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t SirenStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_bool(1, this->state); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::bool_(1, this->state); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool SirenCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -1939,23 +1955,23 @@ void ListEntitiesLockResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesLockResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.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_length(1, this->code_format.size()); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::bool_(1, this->assumed_state); + size += ProtoSize::bool_(1, this->supports_open); + size += ProtoSize::bool_(1, this->requires_code); + size += ProtoSize::length(1, this->code_format.size()); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void LockStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -1965,13 +1981,13 @@ void LockStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t LockStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_uint32(1, static_cast(this->state)); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::uint32(1, static_cast(this->state)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool LockCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2029,20 +2045,20 @@ void ListEntitiesButtonResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesButtonResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); - size.add_uint32(1, static_cast(this->entity_category)); - size.add_length(1, this->device_class.size()); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::length(1, this->device_class.size()); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool ButtonCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2076,13 +2092,13 @@ void MediaPlayerSupportedFormat::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(5, this->sample_bytes); } uint32_t MediaPlayerSupportedFormat::calculate_size() const { - ProtoSize size; - size.add_length(1, this->format.size()); - size.add_uint32(1, this->sample_rate); - size.add_uint32(1, this->num_channels); - size.add_uint32(1, static_cast(this->purpose)); - size.add_uint32(1, this->sample_bytes); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::length(1, this->format.size()); + size += ProtoSize::uint32(1, this->sample_rate); + size += ProtoSize::uint32(1, this->num_channels); + size += ProtoSize::uint32(1, static_cast(this->purpose)); + size += ProtoSize::uint32(1, this->sample_bytes); + return size; } void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(1, this->object_id); @@ -2103,22 +2119,26 @@ void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(11, this->feature_flags); } uint32_t ListEntitiesMediaPlayerResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - 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 += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::bool_(1, this->supports_pause); + if (!this->supported_formats.empty()) { + for (const auto &it : this->supported_formats) { + size += ProtoSize::message_force(1, it.calculate_size()); + } + } #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - size.add_uint32(1, this->feature_flags); - return size.get_size(); + size += ProtoSize::uint32(1, this->feature_flags); + return size; } void MediaPlayerStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -2130,15 +2150,15 @@ void MediaPlayerStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t MediaPlayerStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_uint32(1, static_cast(this->state)); - size.add_float(1, this->volume); - size.add_bool(1, this->muted); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::uint32(1, static_cast(this->state)); + size += ProtoSize::float_(1, this->volume); + size += ProtoSize::bool_(1, this->muted); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool MediaPlayerCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2213,12 +2233,12 @@ void BluetoothLERawAdvertisement::encode(ProtoWriteBuffer &buffer) const { buffer.encode_bytes(4, this->data, this->data_len); } uint32_t BluetoothLERawAdvertisement::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - size.add_sint32(1, this->rssi); - size.add_uint32(1, this->address_type); - size.add_length(1, this->data_len); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + size += ProtoSize::sint32(1, this->rssi); + size += ProtoSize::uint32(1, this->address_type); + size += ProtoSize::length(1, this->data_len); + return size; } void BluetoothLERawAdvertisementsResponse::encode(ProtoWriteBuffer &buffer) const { for (uint16_t i = 0; i < this->advertisements_len; i++) { @@ -2226,11 +2246,11 @@ void BluetoothLERawAdvertisementsResponse::encode(ProtoWriteBuffer &buffer) cons } } uint32_t BluetoothLERawAdvertisementsResponse::calculate_size() const { - ProtoSize size; + uint32_t size = 0; for (uint16_t i = 0; i < this->advertisements_len; i++) { - size.add_message_object_force(1, this->advertisements[i]); + size += ProtoSize::message_force(1, this->advertisements[i].calculate_size()); } - return size.get_size(); + return size; } bool BluetoothDeviceRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2258,12 +2278,12 @@ void BluetoothDeviceConnectionResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_int32(4, this->error); } uint32_t BluetoothDeviceConnectionResponse::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - size.add_bool(1, this->connected); - size.add_uint32(1, this->mtu); - size.add_int32(1, this->error); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + size += ProtoSize::bool_(1, this->connected); + size += ProtoSize::uint32(1, this->mtu); + size += ProtoSize::int32(1, this->error); + return size; } bool BluetoothGATTGetServicesRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2284,14 +2304,14 @@ void BluetoothGATTDescriptor::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(3, this->short_uuid); } uint32_t BluetoothGATTDescriptor::calculate_size() const { - ProtoSize size; + uint32_t size = 0; if (this->uuid[0] != 0 || this->uuid[1] != 0) { - size.add_uint64_force(1, this->uuid[0]); - size.add_uint64_force(1, this->uuid[1]); + size += ProtoSize::uint64_force(1, this->uuid[0]); + size += ProtoSize::uint64_force(1, this->uuid[1]); } - size.add_uint32(1, this->handle); - size.add_uint32(1, this->short_uuid); - return size.get_size(); + size += ProtoSize::uint32(1, this->handle); + size += ProtoSize::uint32(1, this->short_uuid); + return size; } void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer &buffer) const { if (this->uuid[0] != 0 || this->uuid[1] != 0) { @@ -2306,16 +2326,20 @@ void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(5, this->short_uuid); } uint32_t BluetoothGATTCharacteristic::calculate_size() const { - ProtoSize size; + uint32_t size = 0; if (this->uuid[0] != 0 || this->uuid[1] != 0) { - size.add_uint64_force(1, this->uuid[0]); - size.add_uint64_force(1, this->uuid[1]); + size += ProtoSize::uint64_force(1, this->uuid[0]); + size += ProtoSize::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_uint32(1, this->short_uuid); - return size.get_size(); + size += ProtoSize::uint32(1, this->handle); + size += ProtoSize::uint32(1, this->properties); + if (!this->descriptors.empty()) { + for (const auto &it : this->descriptors) { + size += ProtoSize::message_force(1, it.calculate_size()); + } + } + size += ProtoSize::uint32(1, this->short_uuid); + return size; } void BluetoothGATTService::encode(ProtoWriteBuffer &buffer) const { if (this->uuid[0] != 0 || this->uuid[1] != 0) { @@ -2329,15 +2353,19 @@ void BluetoothGATTService::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(4, this->short_uuid); } uint32_t BluetoothGATTService::calculate_size() const { - ProtoSize size; + uint32_t size = 0; if (this->uuid[0] != 0 || this->uuid[1] != 0) { - size.add_uint64_force(1, this->uuid[0]); - size.add_uint64_force(1, this->uuid[1]); + size += ProtoSize::uint64_force(1, this->uuid[0]); + size += ProtoSize::uint64_force(1, this->uuid[1]); } - size.add_uint32(1, this->handle); - size.add_repeated_message(1, this->characteristics); - size.add_uint32(1, this->short_uuid); - return size.get_size(); + size += ProtoSize::uint32(1, this->handle); + if (!this->characteristics.empty()) { + for (const auto &it : this->characteristics) { + size += ProtoSize::message_force(1, it.calculate_size()); + } + } + size += ProtoSize::uint32(1, this->short_uuid); + return size; } void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint64(1, this->address); @@ -2346,18 +2374,22 @@ void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer &buffer) const { } } uint32_t BluetoothGATTGetServicesResponse::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - size.add_repeated_message(1, this->services); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + if (!this->services.empty()) { + for (const auto &it : this->services) { + size += ProtoSize::message_force(1, it.calculate_size()); + } + } + return size; } void BluetoothGATTGetServicesDoneResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint64(1, this->address); } uint32_t BluetoothGATTGetServicesDoneResponse::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + return size; } bool BluetoothGATTReadRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2378,11 +2410,11 @@ void BluetoothGATTReadResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_bytes(3, this->data_ptr_, this->data_len_); } uint32_t BluetoothGATTReadResponse::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - size.add_uint32(1, this->handle); - size.add_length(1, this->data_len_); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + size += ProtoSize::uint32(1, this->handle); + size += ProtoSize::length(1, this->data_len_); + return size; } bool BluetoothGATTWriteRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2472,11 +2504,11 @@ void BluetoothGATTNotifyDataResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_bytes(3, this->data_ptr_, this->data_len_); } uint32_t BluetoothGATTNotifyDataResponse::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - size.add_uint32(1, this->handle); - size.add_length(1, this->data_len_); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + size += ProtoSize::uint32(1, this->handle); + size += ProtoSize::length(1, this->data_len_); + return size; } void BluetoothConnectionsFreeResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(1, this->free); @@ -2488,15 +2520,15 @@ void BluetoothConnectionsFreeResponse::encode(ProtoWriteBuffer &buffer) const { } } uint32_t BluetoothConnectionsFreeResponse::calculate_size() const { - ProtoSize size; - size.add_uint32(1, this->free); - size.add_uint32(1, this->limit); + uint32_t size = 0; + size += ProtoSize::uint32(1, this->free); + size += ProtoSize::uint32(1, this->limit); for (const auto &it : this->allocated) { if (it != 0) { - size.add_uint64_force(1, it); + size += ProtoSize::uint64_force(1, it); } } - return size.get_size(); + return size; } void BluetoothGATTErrorResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint64(1, this->address); @@ -2504,31 +2536,31 @@ void BluetoothGATTErrorResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_int32(3, this->error); } uint32_t BluetoothGATTErrorResponse::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - size.add_uint32(1, this->handle); - size.add_int32(1, this->error); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + size += ProtoSize::uint32(1, this->handle); + size += ProtoSize::int32(1, this->error); + return size; } void BluetoothGATTWriteResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint64(1, this->address); buffer.encode_uint32(2, this->handle); } uint32_t BluetoothGATTWriteResponse::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - size.add_uint32(1, this->handle); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + size += ProtoSize::uint32(1, this->handle); + return size; } void BluetoothGATTNotifyResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint64(1, this->address); buffer.encode_uint32(2, this->handle); } uint32_t BluetoothGATTNotifyResponse::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - size.add_uint32(1, this->handle); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + size += ProtoSize::uint32(1, this->handle); + return size; } void BluetoothDevicePairingResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint64(1, this->address); @@ -2536,11 +2568,11 @@ void BluetoothDevicePairingResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_int32(3, this->error); } uint32_t BluetoothDevicePairingResponse::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - size.add_bool(1, this->paired); - size.add_int32(1, this->error); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + size += ProtoSize::bool_(1, this->paired); + size += ProtoSize::int32(1, this->error); + return size; } void BluetoothDeviceUnpairingResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint64(1, this->address); @@ -2548,11 +2580,11 @@ void BluetoothDeviceUnpairingResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_int32(3, this->error); } uint32_t BluetoothDeviceUnpairingResponse::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - size.add_bool(1, this->success); - size.add_int32(1, this->error); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + size += ProtoSize::bool_(1, this->success); + size += ProtoSize::int32(1, this->error); + return size; } void BluetoothDeviceClearCacheResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint64(1, this->address); @@ -2560,11 +2592,11 @@ void BluetoothDeviceClearCacheResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_int32(3, this->error); } uint32_t BluetoothDeviceClearCacheResponse::calculate_size() const { - ProtoSize size; - size.add_uint64(1, this->address); - size.add_bool(1, this->success); - size.add_int32(1, this->error); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint64(1, this->address); + size += ProtoSize::bool_(1, this->success); + size += ProtoSize::int32(1, this->error); + return size; } void BluetoothScannerStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(1, static_cast(this->state)); @@ -2572,11 +2604,11 @@ void BluetoothScannerStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(3, static_cast(this->configured_mode)); } uint32_t BluetoothScannerStateResponse::calculate_size() const { - ProtoSize size; - size.add_uint32(1, static_cast(this->state)); - size.add_uint32(1, static_cast(this->mode)); - size.add_uint32(1, static_cast(this->configured_mode)); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint32(1, static_cast(this->state)); + size += ProtoSize::uint32(1, static_cast(this->mode)); + size += ProtoSize::uint32(1, static_cast(this->configured_mode)); + return size; } bool BluetoothScannerSetModeRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2609,11 +2641,11 @@ void VoiceAssistantAudioSettings::encode(ProtoWriteBuffer &buffer) const { buffer.encode_float(3, this->volume_multiplier); } uint32_t VoiceAssistantAudioSettings::calculate_size() const { - ProtoSize size; - size.add_uint32(1, this->noise_suppression_level); - size.add_uint32(1, this->auto_gain); - size.add_float(1, this->volume_multiplier); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint32(1, this->noise_suppression_level); + size += ProtoSize::uint32(1, this->auto_gain); + size += ProtoSize::float_(1, this->volume_multiplier); + return size; } void VoiceAssistantRequest::encode(ProtoWriteBuffer &buffer) const { buffer.encode_bool(1, this->start); @@ -2623,13 +2655,13 @@ void VoiceAssistantRequest::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->wake_word_phrase); } uint32_t VoiceAssistantRequest::calculate_size() const { - ProtoSize size; - size.add_bool(1, this->start); - size.add_length(1, this->conversation_id.size()); - size.add_uint32(1, this->flags); - size.add_message_object(1, this->audio_settings); - size.add_length(1, this->wake_word_phrase.size()); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::bool_(1, this->start); + size += ProtoSize::length(1, this->conversation_id.size()); + size += ProtoSize::uint32(1, this->flags); + size += ProtoSize::message(1, this->audio_settings.calculate_size()); + size += ProtoSize::length(1, this->wake_word_phrase.size()); + return size; } bool VoiceAssistantResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2707,10 +2739,10 @@ void VoiceAssistantAudio::encode(ProtoWriteBuffer &buffer) const { buffer.encode_bool(2, this->end); } uint32_t VoiceAssistantAudio::calculate_size() const { - ProtoSize size; - size.add_length(1, this->data_len); - size.add_bool(1, this->end); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::length(1, this->data_len); + size += ProtoSize::bool_(1, this->end); + return size; } bool VoiceAssistantTimerEventResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2777,9 +2809,9 @@ bool VoiceAssistantAnnounceRequest::decode_length(uint32_t field_id, ProtoLength } void VoiceAssistantAnnounceFinished::encode(ProtoWriteBuffer &buffer) const { buffer.encode_bool(1, this->success); } uint32_t VoiceAssistantAnnounceFinished::calculate_size() const { - ProtoSize size; - size.add_bool(1, this->success); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::bool_(1, this->success); + return size; } void VoiceAssistantWakeWord::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(1, this->id); @@ -2789,15 +2821,15 @@ void VoiceAssistantWakeWord::encode(ProtoWriteBuffer &buffer) const { } } uint32_t VoiceAssistantWakeWord::calculate_size() const { - ProtoSize size; - size.add_length(1, this->id.size()); - size.add_length(1, this->wake_word.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->id.size()); + size += ProtoSize::length(1, this->wake_word.size()); if (!this->trained_languages.empty()) { for (const auto &it : this->trained_languages) { - size.add_length_force(1, it.size()); + size += ProtoSize::length_force(1, it.size()); } } - return size.get_size(); + return size; } bool VoiceAssistantExternalWakeWord::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2860,15 +2892,19 @@ void VoiceAssistantConfigurationResponse::encode(ProtoWriteBuffer &buffer) const buffer.encode_uint32(3, this->max_active_wake_words); } uint32_t VoiceAssistantConfigurationResponse::calculate_size() const { - ProtoSize size; - 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()); + uint32_t size = 0; + if (!this->available_wake_words.empty()) { + for (const auto &it : this->available_wake_words) { + size += ProtoSize::message_force(1, it.calculate_size()); } } - size.add_uint32(1, this->max_active_wake_words); - return size.get_size(); + if (!this->active_wake_words->empty()) { + for (const auto &it : *this->active_wake_words) { + size += ProtoSize::length_force(1, it.size()); + } + } + size += ProtoSize::uint32(1, this->max_active_wake_words); + return size; } bool VoiceAssistantSetConfiguration::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { @@ -2899,22 +2935,22 @@ void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer &buffer) con #endif } uint32_t ListEntitiesAlarmControlPanelResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); - 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); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::uint32(1, this->supported_features); + size += ProtoSize::bool_(1, this->requires_code); + size += ProtoSize::bool_(1, this->requires_code_to_arm); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -2924,13 +2960,13 @@ void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t AlarmControlPanelStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_uint32(1, static_cast(this->state)); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::uint32(1, static_cast(this->state)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool AlarmControlPanelCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -2988,23 +3024,23 @@ void ListEntitiesTextResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesTextResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.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_length(1, this->pattern.size()); - size.add_uint32(1, static_cast(this->mode)); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::uint32(1, this->min_length); + size += ProtoSize::uint32(1, this->max_length); + size += ProtoSize::length(1, this->pattern.size()); + size += ProtoSize::uint32(1, static_cast(this->mode)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void TextStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -3015,14 +3051,14 @@ void TextStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t TextStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_length(1, this->state.size()); - size.add_bool(1, this->missing_state); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->state.size()); + size += ProtoSize::bool_(1, this->missing_state); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool TextCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -3073,19 +3109,19 @@ void ListEntitiesDateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesDateResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); - size.add_uint32(1, static_cast(this->entity_category)); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void DateStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -3098,16 +3134,16 @@ void DateStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t DateStateResponse::calculate_size() const { - ProtoSize size; - 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); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::bool_(1, this->missing_state); + size += ProtoSize::uint32(1, this->year); + size += ProtoSize::uint32(1, this->month); + size += ProtoSize::uint32(1, this->day); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool DateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -3156,19 +3192,19 @@ void ListEntitiesTimeResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesTimeResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); - size.add_uint32(1, static_cast(this->entity_category)); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void TimeStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -3181,16 +3217,16 @@ void TimeStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t TimeStateResponse::calculate_size() const { - ProtoSize size; - 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); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::bool_(1, this->missing_state); + size += ProtoSize::uint32(1, this->hour); + size += ProtoSize::uint32(1, this->minute); + size += ProtoSize::uint32(1, this->second); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool TimeCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -3243,25 +3279,25 @@ void ListEntitiesEventResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesEventResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); - size.add_uint32(1, static_cast(this->entity_category)); - size.add_length(1, this->device_class.size()); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::length(1, this->device_class.size()); if (!this->event_types->empty()) { for (const char *it : *this->event_types) { - size.add_length_force(1, strlen(it)); + size += ProtoSize::length_force(1, strlen(it)); } } #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void EventResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -3271,13 +3307,13 @@ void EventResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t EventResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_length(1, this->event_type.size()); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->event_type.size()); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } #endif #ifdef USE_VALVE @@ -3299,23 +3335,23 @@ void ListEntitiesValveResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesValveResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); - size.add_uint32(1, static_cast(this->entity_category)); - size.add_length(1, this->device_class.size()); - size.add_bool(1, this->assumed_state); - size.add_bool(1, this->supports_position); - size.add_bool(1, this->supports_stop); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::length(1, this->device_class.size()); + size += ProtoSize::bool_(1, this->assumed_state); + size += ProtoSize::bool_(1, this->supports_position); + size += ProtoSize::bool_(1, this->supports_stop); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void ValveStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -3326,14 +3362,14 @@ void ValveStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ValveStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_float(1, this->position); - size.add_uint32(1, static_cast(this->current_operation)); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::float_(1, this->position); + size += ProtoSize::uint32(1, static_cast(this->current_operation)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool ValveCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -3382,19 +3418,19 @@ void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesDateTimeResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); - size.add_uint32(1, static_cast(this->entity_category)); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void DateTimeStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -3405,14 +3441,14 @@ void DateTimeStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t DateTimeStateResponse::calculate_size() const { - ProtoSize size; - size.add_fixed32(1, this->key); - size.add_bool(1, this->missing_state); - size.add_fixed32(1, this->epoch_seconds); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::bool_(1, this->missing_state); + size += ProtoSize::fixed32(1, this->epoch_seconds); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool DateTimeCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -3456,20 +3492,20 @@ void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t ListEntitiesUpdateResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); - size.add_uint32(1, static_cast(this->entity_category)); - size.add_length(1, this->device_class.size()); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); + size += ProtoSize::length(1, this->device_class.size()); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } void UpdateStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_fixed32(1, this->key); @@ -3487,21 +3523,21 @@ void UpdateStateResponse::encode(ProtoWriteBuffer &buffer) const { #endif } uint32_t UpdateStateResponse::calculate_size() const { - ProtoSize 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_length(1, this->current_version.size()); - size.add_length(1, this->latest_version.size()); - size.add_length(1, this->title.size()); - size.add_length(1, this->release_summary.size()); - size.add_length(1, this->release_url.size()); + uint32_t size = 0; + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::bool_(1, this->missing_state); + size += ProtoSize::bool_(1, this->in_progress); + size += ProtoSize::bool_(1, this->has_progress); + size += ProtoSize::float_(1, this->progress); + size += ProtoSize::length(1, this->current_version.size()); + size += ProtoSize::length(1, this->latest_version.size()); + size += ProtoSize::length(1, this->title.size()); + size += ProtoSize::length(1, this->release_summary.size()); + size += ProtoSize::length(1, this->release_url.size()); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - return size.get_size(); + return size; } bool UpdateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -3544,9 +3580,9 @@ bool ZWaveProxyFrame::decode_length(uint32_t field_id, ProtoLengthDelimited valu } void ZWaveProxyFrame::encode(ProtoWriteBuffer &buffer) const { buffer.encode_bytes(1, this->data, this->data_len); } uint32_t ZWaveProxyFrame::calculate_size() const { - ProtoSize size; - size.add_length(1, this->data_len); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::length(1, this->data_len); + return size; } bool ZWaveProxyRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { switch (field_id) { @@ -3575,10 +3611,10 @@ void ZWaveProxyRequest::encode(ProtoWriteBuffer &buffer) const { buffer.encode_bytes(2, this->data, this->data_len); } uint32_t ZWaveProxyRequest::calculate_size() const { - ProtoSize size; - size.add_uint32(1, static_cast(this->type)); - size.add_length(1, this->data_len); - return size.get_size(); + uint32_t size = 0; + size += ProtoSize::uint32(1, static_cast(this->type)); + size += ProtoSize::length(1, this->data_len); + return size; } #endif #ifdef USE_INFRARED @@ -3597,20 +3633,20 @@ void ListEntitiesInfraredResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(8, this->capabilities); } uint32_t ListEntitiesInfraredResponse::calculate_size() const { - ProtoSize size; - size.add_length(1, this->object_id.size()); - size.add_fixed32(1, this->key); - size.add_length(1, this->name.size()); + uint32_t size = 0; + size += ProtoSize::length(1, this->object_id.size()); + size += ProtoSize::fixed32(1, this->key); + size += ProtoSize::length(1, this->name.size()); #ifdef USE_ENTITY_ICON - size.add_length(1, this->icon.size()); + size += ProtoSize::length(1, this->icon.size()); #endif - size.add_bool(1, this->disabled_by_default); - size.add_uint32(1, static_cast(this->entity_category)); + size += ProtoSize::bool_(1, this->disabled_by_default); + size += ProtoSize::uint32(1, static_cast(this->entity_category)); #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - size.add_uint32(1, this->capabilities); - return size.get_size(); + size += ProtoSize::uint32(1, this->capabilities); + return size; } #endif #ifdef USE_IR_RF @@ -3665,17 +3701,17 @@ void InfraredRFReceiveEvent::encode(ProtoWriteBuffer &buffer) const { } } uint32_t InfraredRFReceiveEvent::calculate_size() const { - ProtoSize size; + uint32_t size = 0; #ifdef USE_DEVICES - size.add_uint32(1, this->device_id); + size += ProtoSize::uint32(1, this->device_id); #endif - size.add_fixed32(1, this->key); + size += ProtoSize::fixed32(1, this->key); if (!this->timings->empty()) { for (const auto &it : *this->timings) { - size.add_sint32_force(1, it); + size += ProtoSize::sint32_force(1, it); } } - return size.get_size(); + return size; } #endif diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 665d7dcce3..410e604b99 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -497,32 +497,7 @@ 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 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 - * - Static varint methods for external callers - * - Specialized handling for different field types according to protobuf spec - */ - - 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 * @@ -619,292 +594,62 @@ class ProtoSize { return varint(tag); } - /** - * @brief Common parameters for all add_*_field methods - * - * All add_*_field methods follow these common patterns: - * * @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 - * - * Each method follows this implementation pattern: - * 1. Skip calculation if value is default (0, false, empty) and not forced - * 2. Calculate the size based on the field's encoding rules - * 3. Add the field_id_size + calculated value size to total_size - */ - - /** - * @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) { - if (value != 0) { - add_int32_force(field_id_size, value); - } + // Static methods that RETURN size contribution (no ProtoSize object needed). + // Used by generated calculate_size() methods to accumulate into a plain uint32_t register. + static constexpr uint32_t int32(uint32_t field_id_size, int32_t value) { + return value ? field_id_size + (value < 0 ? 10 : varint(static_cast(value))) : 0; } - - /** - * @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 forced - // Negative values are encoded as 10-byte varints in protobuf - total_size_ += field_id_size + (value < 0 ? 10 : varint(static_cast(value))); + static constexpr uint32_t int32_force(uint32_t field_id_size, int32_t value) { + return field_id_size + (value < 0 ? 10 : varint(static_cast(value))); } - - /** - * @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) { - if (value != 0) { - add_uint32_force(field_id_size, value); - } + static constexpr uint32_t uint32(uint32_t field_id_size, uint32_t value) { + return value ? field_id_size + varint(value) : 0; } - - /** - * @brief Calculates and adds the size of a uint32 field to the total message size (force version) - */ - 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); + static constexpr uint32_t uint32_force(uint32_t field_id_size, uint32_t value) { + return field_id_size + varint(value); } - - /** - * @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) { - if (value) { - // Boolean fields always use 1 byte when true - total_size_ += field_id_size + 1; - } + static constexpr uint32_t bool_(uint32_t field_id_size, bool value) { return value ? field_id_size + 1 : 0; } + static constexpr uint32_t bool_force(uint32_t field_id_size) { return field_id_size + 1; } + static constexpr uint32_t float_(uint32_t field_id_size, float value) { + return value != 0.0f ? field_id_size + 4 : 0; } - - /** - * @brief Calculates and adds the size of a boolean field to the total message size (force version) - */ - 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; + static constexpr uint32_t fixed32(uint32_t field_id_size, uint32_t value) { return value ? field_id_size + 4 : 0; } + static constexpr uint32_t sfixed32(uint32_t field_id_size, int32_t value) { return value ? field_id_size + 4 : 0; } + static constexpr uint32_t sint32(uint32_t field_id_size, int32_t value) { + return value ? field_id_size + varint(encode_zigzag32(value)) : 0; } - - /** - * @brief Calculates and adds the size of a float field to the total message size - */ - inline void add_float(uint32_t field_id_size, float value) { - if (value != 0.0f) { - total_size_ += field_id_size + 4; - } + static constexpr uint32_t sint32_force(uint32_t field_id_size, int32_t value) { + return field_id_size + varint(encode_zigzag32(value)); } - - // 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 - */ - inline void add_fixed32(uint32_t field_id_size, uint32_t value) { - if (value != 0) { - total_size_ += field_id_size + 4; - } + static constexpr uint32_t int64(uint32_t field_id_size, int64_t value) { + return value ? field_id_size + varint(value) : 0; } - - // 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 - */ - inline void add_sfixed32(uint32_t field_id_size, int32_t value) { - if (value != 0) { - total_size_ += field_id_size + 4; - } + static constexpr uint32_t int64_force(uint32_t field_id_size, int64_t value) { return field_id_size + varint(value); } + static constexpr uint32_t uint64(uint32_t field_id_size, uint64_t value) { + return value ? field_id_size + varint(value) : 0; } - - // 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 a sint32 field to the total message size - * - * Sint32 fields use ZigZag encoding, which is more efficient for negative values. - */ - inline void add_sint32(uint32_t field_id_size, int32_t value) { - if (value != 0) { - add_sint32_force(field_id_size, value); - } + static constexpr uint32_t uint64_force(uint32_t field_id_size, uint64_t value) { + return field_id_size + varint(value); } - - /** - * @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_force(uint32_t field_id_size, int32_t value) { - // Always calculate size when force is true - // ZigZag encoding for sint32 - total_size_ += field_id_size + varint(encode_zigzag32(value)); + static constexpr uint32_t length(uint32_t field_id_size, size_t len) { + return len ? field_id_size + varint(static_cast(len)) + static_cast(len) : 0; } - - /** - * @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) { - if (value != 0) { - add_int64_force(field_id_size, value); - } + static constexpr uint32_t length_force(uint32_t field_id_size, size_t len) { + return field_id_size + varint(static_cast(len)) + static_cast(len); } - - /** - * @brief Calculates and adds the size of an int64 field to the total message size (force version) - */ - 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); + static constexpr uint32_t sint64(uint32_t field_id_size, int64_t value) { + return value ? field_id_size + varint(encode_zigzag64(value)) : 0; } - - /** - * @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) { - if (value != 0) { - add_uint64_force(field_id_size, value); - } + static constexpr uint32_t sint64_force(uint32_t field_id_size, int64_t value) { + return field_id_size + varint(encode_zigzag64(value)); } - - /** - * @brief Calculates and adds the size of a uint64 field to the total message size (force version) - */ - 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); + static constexpr uint32_t fixed64(uint32_t field_id_size, uint64_t value) { return value ? field_id_size + 8 : 0; } + static constexpr uint32_t sfixed64(uint32_t field_id_size, int64_t value) { return value ? field_id_size + 8 : 0; } + static constexpr uint32_t message(uint32_t field_id_size, uint32_t nested_size) { + return nested_size ? field_id_size + varint(nested_size) + nested_size : 0; } - - // 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 - - /** - * @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) { - if (len != 0) { - add_length_force(field_id_size, len); - } - } - - /** - * @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_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); - } - - /** - * @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 - * - * This helper function directly updates the total_size reference if the nested size - * is greater than zero. - * - * @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) { - if (nested_size != 0) { - add_message_field_force(field_id_size, nested_size); - } - } - - /** - * @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_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; - } - - /** - * @brief Calculates and adds the size of a nested message field to the total message size - * - * This version takes a ProtoMessage object, calculates its size internally, - * and updates the total_size reference. This eliminates the need for a temporary variable - * at the call site. - * - * @param message The nested message object - */ - template inline void add_message_object(uint32_t field_id_size, const T &message) { - add_message_field(field_id_size, message.calculate_size()); - } - - template inline void add_message_object_force(uint32_t field_id_size, const T &message) { - add_message_field_force(field_id_size, message.calculate_size()); - } - - /** - * @brief Calculates and adds the sizes of all messages in a repeated field to the total message size - * - * This helper processes a vector of message objects, calculating the size for each message - * and adding it to the total size. - * - * @tparam MessageType The type of the nested messages in the vector - * @param messages Vector of message objects - */ - template - inline void add_repeated_message(uint32_t field_id_size, const std::vector &messages) { - // Skip if the vector is empty - if (!messages.empty()) { - // Use the force version for all messages in the repeated field - for (const auto &message : messages) { - add_message_object_force(field_id_size, message); - } - } - } - - /** - * @brief Calculates and adds the sizes of all messages in a repeated field to the total message size (FixedVector - * version) - * - * @tparam MessageType The type of the nested messages in the FixedVector - * @param messages FixedVector of message objects - */ - template - inline void add_repeated_message(uint32_t field_id_size, const FixedVector &messages) { - // Skip if the fixed vector is empty - if (!messages.empty()) { - // Use the force version for all messages in the repeated field - for (const auto &message : messages) { - add_message_object_force(field_id_size, message); - } - } - } - - /** - * @brief Calculate size of a packed repeated sint32 field - */ - inline void add_packed_sint32(uint32_t field_id_size, const std::vector &values) { - if (values.empty()) - return; - - size_t packed_size = 0; - for (int value : values) { - packed_size += varint(encode_zigzag32(value)); - } - - // field_id + length varint + packed data - total_size_ += field_id_size + varint(static_cast(packed_size)) + static_cast(packed_size); + static constexpr uint32_t message_force(uint32_t field_id_size, uint32_t nested_size) { + return field_id_size + varint(nested_size) + nested_size; } }; diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 9906982c7f..e5106c8500 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -270,18 +270,18 @@ class TypeInfo(ABC): def _get_simple_size_calculation( self, name: str, force: bool, base_method: str, value_expr: str = None ) -> str: - """Helper for simple size calculations. + """Helper for simple size calculations using static ProtoSize methods. Args: name: Field name force: Whether this is for a repeated field - base_method: Base method name (e.g., "add_int32") + base_method: Base method name (e.g., "int32") value_expr: Optional value expression (defaults to name) """ field_id_size = self.calculate_field_id_size() method = f"{base_method}_force" if force else base_method value = value_expr or name - return f"size.{method}({field_id_size}, {value});" + return f"size += ProtoSize::{method}({field_id_size}, {value});" @abstractmethod def get_size_calculation(self, name: str, force: bool = False) -> str: @@ -410,7 +410,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_id_size}, {name});" + return f"size += ProtoSize::fixed64({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8 @@ -434,7 +434,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_id_size}, {name});" + return f"size += ProtoSize::float_({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -457,7 +457,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") + return self._get_simple_size_calculation(name, force, "int64") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint @@ -477,7 +477,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") + return self._get_simple_size_calculation(name, force, "uint64") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint @@ -497,7 +497,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") + return self._get_simple_size_calculation(name, force, "int32") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint @@ -518,7 +518,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_id_size}, {name});" + return f"size += ProtoSize::fixed64({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8 @@ -542,7 +542,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_id_size}, {name});" + return f"size += ProtoSize::fixed32({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -563,7 +563,7 @@ class BoolType(TypeInfo): return f"out.append(YESNO({name}));" def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_simple_size_calculation(name, force, "add_bool") + return self._get_simple_size_calculation(name, force, "bool_") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 1 # field ID + 1 byte @@ -647,18 +647,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_length") + return self._get_simple_size_calculation(name, force, "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_length_force which includes field ID + # For repeated fields, we need to use length_force which includes field ID field_id_size = self.calculate_field_id_size() - return f"size.add_length_force({field_id_size}, it.size());" + return f"size += ProtoSize::length_force({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_length({field_id_size}, this->{self.field_name}_ref_.size());" + return f"size += ProtoSize::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 @@ -721,7 +721,9 @@ class MessageType(TypeInfo): return o def get_size_calculation(self, name: str, force: bool = False) -> str: - return self._get_simple_size_calculation(name, force, "add_message_object") + field_id_size = self.calculate_field_id_size() + method = "message_force" if force else "message" + return f"size += ProtoSize::{method}({field_id_size}, {name}.calculate_size());" def get_estimated_size(self) -> int: # For message types, we can't easily estimate the submessage size without @@ -822,7 +824,7 @@ class BytesType(TypeInfo): ) def get_size_calculation(self, name: str, force: bool = False) -> str: - return f"size.add_length({self.calculate_field_id_size()}, this->{self.field_name}_len_);" + return f"size += ProtoSize::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 @@ -897,7 +899,7 @@ class PointerToBytesBufferType(PointerToBufferTypeBase): ) def get_size_calculation(self, name: str, force: bool = False) -> str: - return f"size.add_length({self.calculate_field_id_size()}, this->{self.field_name}_len);" + return f"size += ProtoSize::length({self.calculate_field_id_size()}, this->{self.field_name}_len);" class PointerToStringBufferType(PointerToBufferTypeBase): @@ -939,7 +941,7 @@ class PointerToStringBufferType(PointerToBufferTypeBase): return f'dump_field(out, "{self.name}", this->{self.field_name});' def get_size_calculation(self, name: str, force: bool = False) -> str: - return f"size.add_length({self.calculate_field_id_size()}, this->{self.field_name}.size());" + return f"size += ProtoSize::length({self.calculate_field_id_size()}, this->{self.field_name}.size());" def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 8 # field ID + 8 bytes typical string @@ -1103,9 +1105,9 @@ class FixedArrayBytesType(TypeInfo): if force: # For repeated fields, always calculate size (no zero check) - return f"size.add_length_force({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});" + return f"size += ProtoSize::length_force({field_id_size}, {length_field});" + # For non-repeated fields, length already checks for zero + return f"size += ProtoSize::length({field_id_size}, {length_field});" def get_estimated_size(self) -> int: # Estimate based on typical BLE advertisement size @@ -1132,7 +1134,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") + return self._get_simple_size_calculation(name, force, "uint32") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint @@ -1168,7 +1170,7 @@ class EnumType(TypeInfo): def get_size_calculation(self, name: str, force: bool = False) -> str: return self._get_simple_size_calculation( - name, force, "add_uint32", f"static_cast({name})" + name, force, "uint32", f"static_cast({name})" ) def get_estimated_size(self) -> int: @@ -1190,7 +1192,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_id_size}, {name});" + return f"size += ProtoSize::sfixed32({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 4 @@ -1214,7 +1216,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_id_size}, {name});" + return f"size += ProtoSize::sfixed64({field_id_size}, {name});" def get_fixed_size_bytes(self) -> int: return 8 @@ -1237,7 +1239,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") + return self._get_simple_size_calculation(name, force, "sint32") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint @@ -1257,7 +1259,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") + return self._get_simple_size_calculation(name, force, "sint64") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 3 # field ID + 3 bytes typical varint @@ -1694,11 +1696,17 @@ class RepeatedTypeInfo(TypeInfo): # For repeated fields, we always need to pass force=True to the underlying type's calculation # This is because the encode method always sets force=true for repeated fields - # Handle message types separately as they use a dedicated helper + # Handle message types separately - generate inline loop if isinstance(self._ti, MessageType): field_id_size = self._ti.calculate_field_id_size() - container = f"*{name}" if self._use_pointer else name - return f"size.add_repeated_message({field_id_size}, {container});" + container_ref = f"*{name}" if self._use_pointer else name + empty_check = f"{name}->empty()" if self._use_pointer else f"{name}.empty()" + o = f"if (!{empty_check}) {{\n" + o += f" for (const auto &it : {container_ref}) {{\n" + o += f" size += ProtoSize::message_force({field_id_size}, it.calculate_size());\n" + o += " }\n" + o += "}" + return o # For non-message types, generate size calculation with iteration container_ref = f"*{name}" if self._use_pointer else name @@ -1713,14 +1721,14 @@ class RepeatedTypeInfo(TypeInfo): field_id_size = self._ti.calculate_field_id_size() bytes_per_element = field_id_size + num_bytes size_expr = f"{name}->size()" if self._use_pointer else f"{name}.size()" - o += f" size.add_precalculated_size({size_expr} * {bytes_per_element});\n" + o += f" size += {size_expr} * {bytes_per_element};\n" else: # Other types need the actual value # Special handling for const char* elements if self._use_pointer and "const char" in self._container_no_template: field_id_size = self.calculate_field_id_size() o += f" for (const char *it : {container_ref}) {{\n" - o += f" size.add_length_force({field_id_size}, strlen(it));\n" + o += f" size += ProtoSize::length_force({field_id_size}, strlen(it));\n" else: auto_ref = "" if self._ti_is_bool else "&" o += f" for (const auto {auto_ref}it : {container_ref}) {{\n" @@ -2240,9 +2248,9 @@ 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"uint32_t {desc.name}::calculate_size() const {{\n" - o += " ProtoSize size;\n" + o += " uint32_t size = 0;\n" o += indent("\n".join(size_calc)) + "\n" - o += " return size.get_size();\n" + o += " return size;\n" o += "}\n" cpp += o prot = "uint32_t calculate_size() const;"