From 427560f81452f69fda08a4e2e0e96f898ec9c08d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 11 Jul 2025 17:24:55 -1000 Subject: [PATCH 1/2] address bot review comments --- esphome/components/api/proto.h | 13 +++++++++---- script/api_protobuf/api_protobuf.py | 5 ++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 2a77116f5a..a5fd5c67b2 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -132,15 +132,20 @@ class ProtoVarInt { uint64_t value_; }; -// Forward declaration for decode_to_message -class ProtoMessage; - class ProtoLengthDelimited { public: explicit ProtoLengthDelimited(const uint8_t *value, size_t length) : value_(value), length_(length) {} std::string as_string() const { return std::string(reinterpret_cast(this->value_), this->length_); } - // Non-template method to decode into an existing message instance + /** + * Decode the length-delimited data into an existing ProtoMessage instance. + * + * This method allows decoding without templates, enabling use in contexts + * where the message type is not known at compile time. The ProtoMessage's + * decode() method will be called with the raw data and length. + * + * @param msg The ProtoMessage instance to decode into + */ void decode_to_message(ProtoMessage &msg) const; protected: diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 8395045b3c..1bb8789904 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -540,7 +540,10 @@ class MessageType(TypeInfo): @property def decode_length(self) -> str: - # For non-template decoding, we need to handle this differently + # Override to return None for message types because we can't use template-based + # decoding when the specific message type isn't known at compile time. + # Instead, we use the non-template decode_to_message() method which allows + # runtime polymorphism through virtual function calls. return None @property From 1f35c35e2a9ffdb8e997b6de4e7294f057c7d0ae Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 11 Jul 2025 17:32:07 -1000 Subject: [PATCH 2/2] oops, removed wrong one --- esphome/components/api/proto.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index a5fd5c67b2..936e732af1 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -132,6 +132,9 @@ class ProtoVarInt { uint64_t value_; }; +// Forward declaration for decode_to_message and encode_to_writer +class ProtoMessage; + class ProtoLengthDelimited { public: explicit ProtoLengthDelimited(const uint8_t *value, size_t length) : value_(value), length_(length) {} @@ -189,9 +192,6 @@ class Proto64Bit { const uint64_t value_; }; -// Forward declaration needed for method declaration -class ProtoMessage; - class ProtoWriteBuffer { public: ProtoWriteBuffer(std::vector *buffer) : buffer_(buffer) {}