diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index febc2927d9..3d51f010d0 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_uint32(2, static_cast(this->port_type)); + buffer.encode_small_varint(16, 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_uint32(9, static_cast(this->entity_category)); + buffer.encode_small_varint(72, 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_uint32(11, static_cast(this->entity_category)); + buffer.encode_small_varint(88, 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_uint32(5, static_cast(this->current_operation)); + buffer.encode_small_varint(40, 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_uint32(11, static_cast(this->entity_category)); + buffer.encode_small_varint(88, 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_uint32(5, static_cast(this->direction)); + buffer.encode_small_varint(40, 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_uint32(15, static_cast(this->entity_category)); + buffer.encode_small_varint(120, 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_uint32(11, static_cast(this->color_mode)); + buffer.encode_small_varint(88, 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_uint32(10, static_cast(this->state_class)); + buffer.encode_small_varint(80, static_cast(this->state_class)); buffer.encode_bool(12, this->disabled_by_default); - buffer.encode_uint32(13, static_cast(this->entity_category)); + buffer.encode_small_varint(104, 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_uint32(8, static_cast(this->entity_category)); + buffer.encode_small_varint(64, 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_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, 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_uint32(1, static_cast(this->level)); + buffer.encode_small_varint(8, static_cast(this->level)); buffer.encode_bytes(3, this->message_ptr_, this->message_len_); } uint32_t SubscribeLogsResponse::calculate_size() const { @@ -1108,7 +1108,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_uint32(2, static_cast(this->type)); + buffer.encode_small_varint(16, static_cast(this->type)); } uint32_t ListEntitiesServicesArgument::calculate_size() const { uint32_t size = 0; @@ -1122,7 +1122,7 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer &buffer) const { for (auto &it : this->args) { buffer.encode_sub_message(3, it); } - buffer.encode_uint32(4, static_cast(this->supports_response)); + buffer.encode_small_varint(32, static_cast(this->supports_response)); } uint32_t ListEntitiesServicesResponse::calculate_size() const { uint32_t size = 0; @@ -1269,7 +1269,7 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer &buffer) const { #ifdef USE_ENTITY_ICON buffer.encode_string(6, this->icon); #endif - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -1416,16 +1416,16 @@ uint32_t ListEntitiesClimateResponse::calculate_size() const { } void ClimateStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - buffer.encode_uint32(2, static_cast(this->mode)); + buffer.encode_small_varint(16, 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_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_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_string(11, this->custom_fan_mode); - buffer.encode_uint32(12, static_cast(this->preset)); + buffer.encode_small_varint(96, 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); @@ -1555,7 +1555,7 @@ void ListEntitiesWaterHeaterResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(4, this->icon); #endif buffer.encode_bool(5, this->disabled_by_default); - buffer.encode_uint32(6, static_cast(this->entity_category)); + buffer.encode_small_varint(48, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(7, this->device_id); #endif @@ -1593,7 +1593,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_uint32(4, static_cast(this->mode)); + buffer.encode_small_varint(32, static_cast(this->mode)); #ifdef USE_DEVICES buffer.encode_uint32(5, this->device_id); #endif @@ -1668,9 +1668,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_uint32(10, static_cast(this->entity_category)); + buffer.encode_small_varint(80, static_cast(this->entity_category)); buffer.encode_string(11, this->unit_of_measurement); - buffer.encode_uint32(12, static_cast(this->mode)); + buffer.encode_small_varint(96, static_cast(this->mode)); buffer.encode_string(13, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(14, this->device_id); @@ -1753,7 +1753,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_uint32(8, static_cast(this->entity_category)); + buffer.encode_small_varint(64, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(9, this->device_id); #endif @@ -1844,7 +1844,7 @@ void ListEntitiesSirenResponse::encode(ProtoWriteBuffer &buffer) const { } buffer.encode_bool(8, this->supports_duration); buffer.encode_bool(9, this->supports_volume); - buffer.encode_uint32(10, static_cast(this->entity_category)); + buffer.encode_small_varint(80, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(11, this->device_id); #endif @@ -1951,7 +1951,7 @@ void ListEntitiesLockResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, 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); @@ -1981,7 +1981,7 @@ uint32_t ListEntitiesLockResponse::calculate_size() const { } void LockStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - buffer.encode_uint32(2, static_cast(this->state)); + buffer.encode_small_varint(16, static_cast(this->state)); #ifdef USE_DEVICES buffer.encode_uint32(3, this->device_id); #endif @@ -2044,7 +2044,7 @@ void ListEntitiesButtonResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(9, this->device_id); @@ -2094,7 +2094,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_uint32(4, static_cast(this->purpose)); + buffer.encode_small_varint(32, static_cast(this->purpose)); buffer.encode_uint32(5, this->sample_bytes); } uint32_t MediaPlayerSupportedFormat::calculate_size() const { @@ -2114,7 +2114,7 @@ void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_bool(8, this->supports_pause); for (auto &it : this->supported_formats) { buffer.encode_sub_message(9, it); @@ -2148,7 +2148,7 @@ uint32_t ListEntitiesMediaPlayerResponse::calculate_size() const { } void MediaPlayerStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - buffer.encode_uint32(2, static_cast(this->state)); + buffer.encode_small_varint(16, static_cast(this->state)); buffer.encode_float(3, this->volume); buffer.encode_bool(4, this->muted); #ifdef USE_DEVICES @@ -2609,9 +2609,9 @@ uint32_t BluetoothDeviceClearCacheResponse::calculate_size() const { return size; } void BluetoothScannerStateResponse::encode(ProtoWriteBuffer &buffer) const { - 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)); + 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)); } uint32_t BluetoothScannerStateResponse::calculate_size() const { uint32_t size = 0; @@ -2936,7 +2936,7 @@ void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer &buffer) con buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, 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); @@ -2964,7 +2964,7 @@ uint32_t ListEntitiesAlarmControlPanelResponse::calculate_size() const { } void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - buffer.encode_uint32(2, static_cast(this->state)); + buffer.encode_small_varint(16, static_cast(this->state)); #ifdef USE_DEVICES buffer.encode_uint32(3, this->device_id); #endif @@ -3024,11 +3024,11 @@ void ListEntitiesTextResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, 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_uint32(11, static_cast(this->mode)); + buffer.encode_small_varint(88, static_cast(this->mode)); #ifdef USE_DEVICES buffer.encode_uint32(12, this->device_id); #endif @@ -3113,7 +3113,7 @@ void ListEntitiesDateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -3196,7 +3196,7 @@ void ListEntitiesTimeResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -3279,7 +3279,7 @@ void ListEntitiesEventResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, 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); @@ -3335,7 +3335,7 @@ void ListEntitiesValveResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, 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); @@ -3366,7 +3366,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_uint32(3, static_cast(this->current_operation)); + buffer.encode_small_varint(24, static_cast(this->current_operation)); #ifdef USE_DEVICES buffer.encode_uint32(4, this->device_id); #endif @@ -3422,7 +3422,7 @@ void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -3495,7 +3495,7 @@ void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - buffer.encode_uint32(7, static_cast(this->entity_category)); + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(9, this->device_id); @@ -3617,7 +3617,7 @@ bool ZWaveProxyRequest::decode_length(uint32_t field_id, ProtoLengthDelimited va return true; } void ZWaveProxyRequest::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_uint32(1, static_cast(this->type)); + buffer.encode_small_varint(8, static_cast(this->type)); buffer.encode_bytes(2, this->data, this->data_len); } uint32_t ZWaveProxyRequest::calculate_size() const { @@ -3636,7 +3636,7 @@ void ListEntitiesInfraredResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(4, this->icon); #endif buffer.encode_bool(5, this->disabled_by_default); - buffer.encode_uint32(6, static_cast(this->entity_category)); + buffer.encode_small_varint(48, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(7, this->device_id); #endif @@ -3832,8 +3832,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_uint32(2, static_cast(this->type)); - buffer.encode_uint32(3, static_cast(this->status)); + buffer.encode_small_varint(16, static_cast(this->type)); + buffer.encode_small_varint(24, 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 1295426663..60f9429a06 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -335,6 +335,18 @@ 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 7b0e81af4e..a755b8a413 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -249,7 +249,7 @@ class TypeInfo(ABC): "encode_bool": "buffer.write_raw_byte({value} ? 0x01 : 0x00);", } - # When max_value < 128, the varint is always 1 byte — use a direct byte write + # When max_value < 128 and forced, the varint is always 1 byte — write tag + value directly RAW_ENCODE_SMALL_MAP: dict[str, str] = { "encode_uint32": "buffer.write_raw_byte(static_cast({value}));", "encode_uint64": "buffer.write_raw_byte(static_cast({value}));", @@ -260,7 +260,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 direct byte write instead of varint encoding. + When max_value < 128, uses encode_small_varint instead of varint encoding. """ if not self.force: return None @@ -1356,13 +1356,19 @@ class EnumType(TypeInfo): @property def encode_content(self) -> str: - if result := self._encode_with_precomputed_tag( - f"static_cast(this->{self.field_name})" - ): + 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}, static_cast(this->{self.field_name}), true);" - return f"buffer.{self.encode_func}({self.number}, static_cast(this->{self.field_name}));" + return f"buffer.{self.encode_func}({self.number}, {value_expr}, true);" + return f"buffer.{self.encode_func}({self.number}, {value_expr});" def dump(self, name: str) -> str: return f"out.append_p(proto_enum_to_string<{self.cpp_type}>({name}));"