diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 32373fff6a..0001d3b874 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -2283,9 +2283,9 @@ class VoiceAssistantAudio : public ProtoDecodableMessage { #ifdef HAS_PROTO_MESSAGE_DUMP const char *message_name() const override { return "voice_assistant_audio"; } #endif + std::string data{}; const uint8_t *data_ptr_{nullptr}; size_t data_len_{0}; - std::string data{}; void set_data(const uint8_t *data, size_t len) { this->data_ptr_ = data; this->data_len_ = len; diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 69ce1809ea..2678b7009a 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -113,8 +113,15 @@ def force_str(force: bool) -> str: class TypeInfo(ABC): """Base class for all type information.""" - def __init__(self, field: descriptor.FieldDescriptorProto) -> None: + def __init__( + self, + field: descriptor.FieldDescriptorProto, + needs_decode: bool = True, + needs_encode: bool = True, + ) -> None: self._field = field + self._needs_decode = needs_decode + self._needs_encode = needs_encode @property def default_value(self) -> str: @@ -598,49 +605,29 @@ class BytesType(TypeInfo): reference_type = "std::string &" const_reference_type = "const std::string &" encode_func = "encode_bytes" + decode_length = "value.as_string()" wire_type = WireType.LENGTH_DELIMITED # Uses wire type 2 - def __init__( - self, - field: descriptor.FieldDescriptorProto, - needs_decode: bool = True, - needs_encode: bool = True, - ) -> None: - super().__init__(field) - self.needs_decode = needs_decode - self.needs_encode = needs_encode - # Only set decode_length if we need decoding - if needs_decode: - self.decode_length = "value.as_string()" - @property def public_content(self) -> list[str]: - content = [] - - # Add pointer/length fields if message needs encoding - if self.needs_encode: - content.extend( - [ - f"const uint8_t* {self.field_name}_ptr_{{nullptr}};", - f"size_t {self.field_name}_len_{{0}};", - ] - ) - + content: list[str] = [] # Add std::string storage if message needs decoding - if self.needs_decode: + if self._needs_decode: content.append(f"std::string {self.field_name}{{}};") - # Add setter method if message needs encoding - if self.needs_encode: + if self._needs_encode: content.extend( [ + # Add pointer/length fields if message needs encoding + f"const uint8_t* {self.field_name}_ptr_{{nullptr}};", + f"size_t {self.field_name}_len_{{0}};", + # Add setter method if message needs encoding f"void set_{self.field_name}(const uint8_t* data, size_t len) {{", f" this->{self.field_name}_ptr_ = data;", f" this->{self.field_name}_len_ = len;", "}", ] ) - return content @property @@ -652,11 +639,11 @@ class BytesType(TypeInfo): str_dump = f"format_hex_pretty(reinterpret_cast(this->{self.field_name}.data()), this->{self.field_name}.size())" # For SOURCE_CLIENT only, always use std::string - if not self.needs_encode: + if not self._needs_encode: return f"out.append({str_dump});" # For SOURCE_SERVER, always use pointer/length - if not self.needs_decode: + if not self._needs_decode: return f"out.append({ptr_dump});" # For SOURCE_BOTH, check if pointer is set (sending) or use string (received)