diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 04ce317caca..55021186a62 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -498,6 +498,17 @@ class ProtoEncode { progmem_memcpy(pos, data, len); return pos + len; } + /// encode_progmem_bytes for a string field that may be in flash. + [[nodiscard]] static inline uint8_t *encode_progmem_string(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, + uint32_t field_id, const ProgmemStringRef &ref) { + return encode_progmem_bytes(pos PROTO_ENCODE_DEBUG_ARG, field_id, + reinterpret_cast(ref.progmem_ptr()), ref.size()); + } + [[nodiscard]] static inline uint8_t *encode_progmem_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, + uint32_t field_id, const ProgmemStringRef &ref) { + return encode_progmem_bytes_force(pos PROTO_ENCODE_DEBUG_ARG, field_id, + reinterpret_cast(ref.progmem_ptr()), ref.size()); + } [[nodiscard]] static inline uint8_t *encode_uint32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM, uint32_t field_id, uint32_t value) { pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0); diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 5c29733f342..aaa1663dbba 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -1285,10 +1285,9 @@ class PointerToStringBufferType(PointerToBufferTypeBase): return _encode_call(encode_fn, str(tag), f"this->{self.field_name}") if self.progmem: return _encode_call( - "encode_progmem_bytes", + "encode_progmem_string", str(self.number), - f"reinterpret_cast(this->{self.field_name}.progmem_ptr())", - f"this->{self.field_name}.size()", + f"this->{self.field_name}", force=self.force, ) if result := self._encode_bytes_with_precomputed_tag( diff --git a/tests/unit_tests/components/api/test_api_protobuf_generator.py b/tests/unit_tests/components/api/test_api_protobuf_generator.py index 4927b620205..9d8eab69b90 100644 --- a/tests/unit_tests/components/api/test_api_protobuf_generator.py +++ b/tests/unit_tests/components/api/test_api_protobuf_generator.py @@ -419,8 +419,8 @@ def test_progmem_rejected_on_non_bytes_field() -> None: ("force", "max_len", "encode_fn"), [ (True, 120, "encode_short_progmem_string_force("), - (False, None, "encode_progmem_bytes("), - (True, None, "encode_progmem_bytes_force("), + (False, None, "encode_progmem_string("), + (True, None, "encode_progmem_string_force("), ], ) def test_progmem_string_field_copies_from_flash(