From fef4ff4a8229281b01e664f8b023e53a590fb436 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 5 Apr 2026 19:04:01 -1000 Subject: [PATCH] =?UTF-8?q?[api]=20Remove=20encode=5Fsmall=5Fvarint=20?= =?UTF-8?q?=E2=80=94=20no=20real=20benefit=20over=20encode=5Fuint32?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esphome/components/api/api_pb2.cpp | 102 ++++++++++++++-------------- esphome/components/api/proto.h | 12 ---- script/api_protobuf/api_protobuf.py | 9 +-- 3 files changed, 52 insertions(+), 71 deletions(-) diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index c4a234d243..66623c0ab0 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -74,7 +74,7 @@ uint32_t DeviceInfo::calculate_size() const { #ifdef USE_SERIAL_PROXY void SerialProxyInfo::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(1, this->name); - buffer.encode_small_varint(16, static_cast(this->port_type)); + buffer.encode_uint32(2, static_cast(this->port_type)); } uint32_t SerialProxyInfo::calculate_size() const { uint32_t size = 0; @@ -216,7 +216,7 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer &buffer) const { #ifdef USE_ENTITY_ICON buffer.encode_string(8, this->icon); #endif - buffer.encode_small_varint(72, static_cast(this->entity_category)); + buffer.encode_uint32(9, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(10, this->device_id); #endif @@ -270,7 +270,7 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer &buffer) const { #ifdef USE_ENTITY_ICON buffer.encode_string(10, this->icon); #endif - buffer.encode_small_varint(88, static_cast(this->entity_category)); + buffer.encode_uint32(11, static_cast(this->entity_category)); buffer.encode_bool(12, this->supports_stop); #ifdef USE_DEVICES buffer.encode_uint32(13, this->device_id); @@ -300,7 +300,7 @@ void CoverStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); buffer.encode_float(3, this->position); buffer.encode_float(4, this->tilt); - buffer.encode_small_varint(40, static_cast(this->current_operation)); + buffer.encode_uint32(5, static_cast(this->current_operation)); #ifdef USE_DEVICES buffer.encode_uint32(6, this->device_id); #endif @@ -367,7 +367,7 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer &buffer) const { #ifdef USE_ENTITY_ICON buffer.encode_string(10, this->icon); #endif - buffer.encode_small_varint(88, static_cast(this->entity_category)); + buffer.encode_uint32(11, static_cast(this->entity_category)); for (const char *it : *this->supported_preset_modes) { buffer.encode_string(12, it, strlen(it), true); } @@ -403,7 +403,7 @@ void FanStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); buffer.encode_bool(2, this->state); buffer.encode_bool(3, this->oscillating); - buffer.encode_small_varint(40, static_cast(this->direction)); + buffer.encode_uint32(5, static_cast(this->direction)); buffer.encode_int32(6, this->speed_level); buffer.encode_string(7, this->preset_mode); #ifdef USE_DEVICES @@ -501,7 +501,7 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer &buffer) const { #ifdef USE_ENTITY_ICON buffer.encode_string(14, this->icon); #endif - buffer.encode_small_varint(120, static_cast(this->entity_category)); + buffer.encode_uint32(15, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(16, this->device_id); #endif @@ -535,7 +535,7 @@ void LightStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); buffer.encode_bool(2, this->state); buffer.encode_float(3, this->brightness); - buffer.encode_small_varint(88, static_cast(this->color_mode)); + buffer.encode_uint32(11, static_cast(this->color_mode)); buffer.encode_float(10, this->color_brightness); buffer.encode_float(4, this->red); buffer.encode_float(5, this->green); @@ -690,9 +690,9 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_int32(7, this->accuracy_decimals); buffer.encode_bool(8, this->force_update); buffer.encode_string(9, this->device_class); - buffer.encode_small_varint(80, static_cast(this->state_class)); + buffer.encode_uint32(10, static_cast(this->state_class)); buffer.encode_bool(12, this->disabled_by_default); - buffer.encode_small_varint(104, static_cast(this->entity_category)); + buffer.encode_uint32(13, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(14, this->device_id); #endif @@ -746,7 +746,7 @@ void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer &buffer) const { #endif buffer.encode_bool(6, this->assumed_state); buffer.encode_bool(7, this->disabled_by_default); - buffer.encode_small_varint(64, static_cast(this->entity_category)); + buffer.encode_uint32(8, static_cast(this->entity_category)); buffer.encode_string(9, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(10, this->device_id); @@ -820,7 +820,7 @@ void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(9, this->device_id); @@ -875,7 +875,7 @@ bool SubscribeLogsRequest::decode_varint(uint32_t field_id, proto_varint_value_t return true; } void SubscribeLogsResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_small_varint(8, static_cast(this->level)); + buffer.encode_uint32(1, static_cast(this->level)); buffer.encode_bytes(3, this->message_ptr_, this->message_len_); } uint32_t SubscribeLogsResponse::calculate_size() const { @@ -1112,7 +1112,7 @@ bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { #ifdef USE_API_USER_DEFINED_ACTIONS void ListEntitiesServicesArgument::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(1, this->name); - buffer.encode_small_varint(16, static_cast(this->type)); + buffer.encode_uint32(2, static_cast(this->type)); } uint32_t ListEntitiesServicesArgument::calculate_size() const { uint32_t size = 0; @@ -1126,7 +1126,7 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer &buffer) const { for (auto &it : this->args) { buffer.encode_sub_message(3, it); } - buffer.encode_small_varint(32, static_cast(this->supports_response)); + buffer.encode_uint32(4, static_cast(this->supports_response)); } uint32_t ListEntitiesServicesResponse::calculate_size() const { uint32_t size = 0; @@ -1273,7 +1273,7 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer &buffer) const { #ifdef USE_ENTITY_ICON buffer.encode_string(6, this->icon); #endif - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -1420,16 +1420,16 @@ uint32_t ListEntitiesClimateResponse::calculate_size() const { } void ClimateStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - buffer.encode_small_varint(16, static_cast(this->mode)); + buffer.encode_uint32(2, static_cast(this->mode)); buffer.encode_float(3, this->current_temperature); buffer.encode_float(4, this->target_temperature); buffer.encode_float(5, this->target_temperature_low); buffer.encode_float(6, this->target_temperature_high); - buffer.encode_small_varint(64, static_cast(this->action)); - buffer.encode_small_varint(72, static_cast(this->fan_mode)); - buffer.encode_small_varint(80, static_cast(this->swing_mode)); + buffer.encode_uint32(8, static_cast(this->action)); + buffer.encode_uint32(9, static_cast(this->fan_mode)); + buffer.encode_uint32(10, static_cast(this->swing_mode)); buffer.encode_string(11, this->custom_fan_mode); - buffer.encode_small_varint(96, static_cast(this->preset)); + buffer.encode_uint32(12, static_cast(this->preset)); buffer.encode_string(13, this->custom_preset); buffer.encode_float(14, this->current_humidity); buffer.encode_float(15, this->target_humidity); @@ -1559,7 +1559,7 @@ void ListEntitiesWaterHeaterResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(4, this->icon); #endif buffer.encode_bool(5, this->disabled_by_default); - buffer.encode_small_varint(48, static_cast(this->entity_category)); + buffer.encode_uint32(6, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(7, this->device_id); #endif @@ -1597,7 +1597,7 @@ void WaterHeaterStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); buffer.encode_float(2, this->current_temperature); buffer.encode_float(3, this->target_temperature); - buffer.encode_small_varint(32, static_cast(this->mode)); + buffer.encode_uint32(4, static_cast(this->mode)); #ifdef USE_DEVICES buffer.encode_uint32(5, this->device_id); #endif @@ -1672,9 +1672,9 @@ void ListEntitiesNumberResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_float(7, this->max_value); buffer.encode_float(8, this->step); buffer.encode_bool(9, this->disabled_by_default); - buffer.encode_small_varint(80, static_cast(this->entity_category)); + buffer.encode_uint32(10, static_cast(this->entity_category)); buffer.encode_string(11, this->unit_of_measurement); - buffer.encode_small_varint(96, static_cast(this->mode)); + buffer.encode_uint32(12, static_cast(this->mode)); buffer.encode_string(13, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(14, this->device_id); @@ -1757,7 +1757,7 @@ void ListEntitiesSelectResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(6, it, strlen(it), true); } buffer.encode_bool(7, this->disabled_by_default); - buffer.encode_small_varint(64, static_cast(this->entity_category)); + buffer.encode_uint32(8, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(9, this->device_id); #endif @@ -1848,7 +1848,7 @@ void ListEntitiesSirenResponse::encode(ProtoWriteBuffer &buffer) const { } buffer.encode_bool(8, this->supports_duration); buffer.encode_bool(9, this->supports_volume); - buffer.encode_small_varint(80, static_cast(this->entity_category)); + buffer.encode_uint32(10, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(11, this->device_id); #endif @@ -1955,7 +1955,7 @@ void ListEntitiesLockResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_bool(8, this->assumed_state); buffer.encode_bool(9, this->supports_open); buffer.encode_bool(10, this->requires_code); @@ -1985,7 +1985,7 @@ uint32_t ListEntitiesLockResponse::calculate_size() const { } void LockStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - buffer.encode_small_varint(16, static_cast(this->state)); + buffer.encode_uint32(2, static_cast(this->state)); #ifdef USE_DEVICES buffer.encode_uint32(3, this->device_id); #endif @@ -2048,7 +2048,7 @@ void ListEntitiesButtonResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(9, this->device_id); @@ -2098,7 +2098,7 @@ void MediaPlayerSupportedFormat::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(1, this->format); buffer.encode_uint32(2, this->sample_rate); buffer.encode_uint32(3, this->num_channels); - buffer.encode_small_varint(32, static_cast(this->purpose)); + buffer.encode_uint32(4, static_cast(this->purpose)); buffer.encode_uint32(5, this->sample_bytes); } uint32_t MediaPlayerSupportedFormat::calculate_size() const { @@ -2118,7 +2118,7 @@ void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_bool(8, this->supports_pause); for (auto &it : this->supported_formats) { buffer.encode_sub_message(9, it); @@ -2152,7 +2152,7 @@ uint32_t ListEntitiesMediaPlayerResponse::calculate_size() const { } void MediaPlayerStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - buffer.encode_small_varint(16, static_cast(this->state)); + buffer.encode_uint32(2, static_cast(this->state)); buffer.encode_float(3, this->volume); buffer.encode_bool(4, this->muted); #ifdef USE_DEVICES @@ -2613,9 +2613,9 @@ uint32_t BluetoothDeviceClearCacheResponse::calculate_size() const { return size; } void BluetoothScannerStateResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_small_varint(8, static_cast(this->state)); - buffer.encode_small_varint(16, static_cast(this->mode)); - buffer.encode_small_varint(24, static_cast(this->configured_mode)); + buffer.encode_uint32(1, static_cast(this->state)); + buffer.encode_uint32(2, static_cast(this->mode)); + buffer.encode_uint32(3, static_cast(this->configured_mode)); } uint32_t BluetoothScannerStateResponse::calculate_size() const { uint32_t size = 0; @@ -2940,7 +2940,7 @@ void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer &buffer) con buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_uint32(8, this->supported_features); buffer.encode_bool(9, this->requires_code); buffer.encode_bool(10, this->requires_code_to_arm); @@ -2968,7 +2968,7 @@ uint32_t ListEntitiesAlarmControlPanelResponse::calculate_size() const { } void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - buffer.encode_small_varint(16, static_cast(this->state)); + buffer.encode_uint32(2, static_cast(this->state)); #ifdef USE_DEVICES buffer.encode_uint32(3, this->device_id); #endif @@ -3028,11 +3028,11 @@ void ListEntitiesTextResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_uint32(8, this->min_length); buffer.encode_uint32(9, this->max_length); buffer.encode_string(10, this->pattern); - buffer.encode_small_varint(88, static_cast(this->mode)); + buffer.encode_uint32(11, static_cast(this->mode)); #ifdef USE_DEVICES buffer.encode_uint32(12, this->device_id); #endif @@ -3117,7 +3117,7 @@ void ListEntitiesDateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -3200,7 +3200,7 @@ void ListEntitiesTimeResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -3283,7 +3283,7 @@ void ListEntitiesEventResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); for (const char *it : *this->event_types) { buffer.encode_string(9, it, strlen(it), true); @@ -3339,7 +3339,7 @@ void ListEntitiesValveResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); buffer.encode_bool(9, this->assumed_state); buffer.encode_bool(10, this->supports_position); @@ -3370,7 +3370,7 @@ uint32_t ListEntitiesValveResponse::calculate_size() const { void ValveStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); buffer.encode_float(2, this->position); - buffer.encode_small_varint(24, static_cast(this->current_operation)); + buffer.encode_uint32(3, static_cast(this->current_operation)); #ifdef USE_DEVICES buffer.encode_uint32(4, this->device_id); #endif @@ -3426,7 +3426,7 @@ void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -3499,7 +3499,7 @@ void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_small_varint(56, static_cast(this->entity_category)); + buffer.encode_uint32(7, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(9, this->device_id); @@ -3621,7 +3621,7 @@ bool ZWaveProxyRequest::decode_length(uint32_t field_id, ProtoLengthDelimited va return true; } void ZWaveProxyRequest::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_small_varint(8, static_cast(this->type)); + buffer.encode_uint32(1, static_cast(this->type)); buffer.encode_bytes(2, this->data, this->data_len); } uint32_t ZWaveProxyRequest::calculate_size() const { @@ -3640,7 +3640,7 @@ void ListEntitiesInfraredResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(4, this->icon); #endif buffer.encode_bool(5, this->disabled_by_default); - buffer.encode_small_varint(48, static_cast(this->entity_category)); + buffer.encode_uint32(6, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(7, this->device_id); #endif @@ -3836,8 +3836,8 @@ bool SerialProxyRequest::decode_varint(uint32_t field_id, proto_varint_value_t v } void SerialProxyRequestResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(1, this->instance); - buffer.encode_small_varint(16, static_cast(this->type)); - buffer.encode_small_varint(24, static_cast(this->status)); + buffer.encode_uint32(2, static_cast(this->type)); + buffer.encode_uint32(3, static_cast(this->status)); buffer.encode_string(4, this->error_message); } uint32_t SerialProxyRequestResponse::calculate_size() const { diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index a99cba18b2..b629018a91 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -276,18 +276,6 @@ class ProtoWriteBuffer { void encode_bytes(uint32_t field_id, const uint8_t *data, size_t len, bool force = false) { this->encode_string(field_id, reinterpret_cast(data), len, force); } - /** Encode a small varint field where both tag and value fit in one byte each. - * The tag is precomputed by the code generator. value must be < 128. - * Skips encoding when value is zero (default protobuf behavior). */ - void encode_small_varint(uint8_t precomputed_tag, uint8_t value) { - if (value == 0) - return; - this->debug_check_bounds_(2); - auto *p = this->pos_; - p[0] = precomputed_tag; - p[1] = value; - this->pos_ = p + 2; - } void encode_uint32(uint32_t field_id, uint32_t value, bool force = false) { if (value == 0 && !force) return; diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index fa20a931c3..30d95f03c9 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -255,7 +255,7 @@ class TypeInfo(ABC): Returns the raw encode string if the tag is a single byte and the encode_func has a known raw equivalent, or None otherwise. - When max_value < 128, uses encode_small_varint instead of varint encoding. + When max_value < 128, uses direct byte write instead of varint encoding. """ if not self.force: return None @@ -1333,13 +1333,6 @@ class EnumType(TypeInfo): value_expr = f"static_cast(this->{self.field_name})" if result := self._encode_with_precomputed_tag(value_expr): return result - # For non-forced enum fields with max < 128 and single-byte tag, - # emit a zero-check + two raw byte writes instead of encode_uint32 - max_val = self.max_value - if max_val is not None and max_val < 128 and not self.force: - tag = self.calculate_tag() - if tag < 128: - return f"buffer.encode_small_varint({tag}, static_cast(this->{self.field_name}));" if self.force: return f"buffer.{self.encode_func}({self.number}, {value_expr}, true);" return f"buffer.{self.encode_func}({self.number}, {value_expr});"