diff --git a/esphome/components/api/api_frame_helper.h b/esphome/components/api/api_frame_helper.h index f93b086186..a582307738 100644 --- a/esphome/components/api/api_frame_helper.h +++ b/esphome/components/api/api_frame_helper.h @@ -163,9 +163,10 @@ class APIFrameHelper { } // Write a single protobuf message - the hot path (87-100% of all writes) virtual APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) = 0; - // Write multiple protobuf messages in a single batched operation - // messages contains (message_type, offset, length) for each message in the buffer - // The buffer contains all messages with appropriate padding before each + // Write multiple protobuf messages in a single batched operation. + // messages must not be empty — caller is responsible for checking. + // messages contains (message_type, offset, length) for each message in the buffer. + // The buffer contains all messages with appropriate padding before each. virtual APIError write_protobuf_messages(ProtoWriteBuffer buffer, std::span messages) = 0; // Get the frame header padding required by this protocol uint8_t frame_header_padding() const { return frame_header_padding_; } diff --git a/esphome/components/api/api_frame_helper_noise.cpp b/esphome/components/api/api_frame_helper_noise.cpp index 429f6fde1f..d6b4a72cae 100644 --- a/esphome/components/api/api_frame_helper_noise.cpp +++ b/esphome/components/api/api_frame_helper_noise.cpp @@ -511,10 +511,9 @@ APIError APINoiseFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffer, s APIError aerr = this->check_data_state_(); if (aerr != APIError::OK) return aerr; - - if (messages.empty()) { - return APIError::OK; - } +#ifdef ESPHOME_DEBUG_API + assert(!messages.empty()); +#endif uint8_t *buffer_data = buffer.get_buffer()->data(); StaticVector iovs; diff --git a/esphome/components/api/api_frame_helper_plaintext.cpp b/esphome/components/api/api_frame_helper_plaintext.cpp index 6e33f68154..a21d177b7f 100644 --- a/esphome/components/api/api_frame_helper_plaintext.cpp +++ b/esphome/components/api/api_frame_helper_plaintext.cpp @@ -302,10 +302,9 @@ APIError APIPlaintextFrameHelper::write_protobuf_messages(ProtoWriteBuffer buffe APIError aerr = this->check_data_state_(); if (aerr != APIError::OK) return aerr; - - if (messages.empty()) { - return APIError::OK; - } +#ifdef ESPHOME_DEBUG_API + assert(!messages.empty()); +#endif uint8_t *buffer_data = buffer.get_buffer()->data(); StaticVector iovs;