diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 6fc0004fbd..94bd164913 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1442,11 +1442,6 @@ bool APIConnection::try_to_clear_buffer(bool log_out_of_space) { return false; } bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint16_t message_type) { - // If we're in batch mode, just return success (message already encoded) - if (this->batch_mode_) { - return true; - } - if (!this->try_to_clear_buffer(message_type != 29)) { // SubscribeLogsResponse return false; } @@ -1520,9 +1515,6 @@ void APIConnection::process_batch_() { static_cast(MAX_BATCH_SIZE_BYTES)); ProtoWriteBuffer batch_buffer = this->create_buffer(estimated_size); - // Enable batch mode AFTER creating the initial buffer to capture message types - this->batch_mode_ = true; - for (size_t i = 0; i < this->deferred_batch_.items.size(); i++) { const auto &item = this->deferred_batch_.items[i]; @@ -1576,9 +1568,6 @@ void APIConnection::process_batch_() { total_size += packet_size; } - // Disable batch mode - this->batch_mode_ = false; - // Send all collected packets if (!packet_info.empty()) { // Add final footer space for Noise if needed diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 81ff74f72d..c2b52c51eb 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -316,24 +316,13 @@ class APIConnection : public APIServerConnection { // Get header padding size - used for both reserve and insert uint8_t header_padding = this->helper_->frame_header_padding(); - if (!this->batch_mode_) { - this->proto_write_buffer_.clear(); - // Reserve space for header padding + message + footer - // - Header padding: space for protocol headers (7 bytes for Noise, 6 for Plaintext) - // - Footer: space for MAC (16 bytes for Noise, 0 for Plaintext) - this->proto_write_buffer_.reserve(reserve_size + header_padding + this->helper_->frame_footer_size()); - // Insert header padding bytes so message encoding starts at the correct position - this->proto_write_buffer_.insert(this->proto_write_buffer_.begin(), header_padding, 0); - } else { - // In batch mode, extend the existing buffer - // Don't clear, just ensure we have enough capacity - size_t current_size = this->proto_write_buffer_.size(); - size_t needed_size = current_size + reserve_size + header_padding + this->helper_->frame_footer_size(); - if (this->proto_write_buffer_.capacity() < needed_size) { - this->proto_write_buffer_.reserve(needed_size); - } - // Don't add padding here - that's done by extend_buffer() when actually extending - } + this->proto_write_buffer_.clear(); + // Reserve space for header padding + message + footer + // - Header padding: space for protocol headers (7 bytes for Noise, 6 for Plaintext) + // - Footer: space for MAC (16 bytes for Noise, 0 for Plaintext) + this->proto_write_buffer_.reserve(reserve_size + header_padding + this->helper_->frame_footer_size()); + // Insert header padding bytes so message encoding starts at the correct position + this->proto_write_buffer_.insert(this->proto_write_buffer_.begin(), header_padding, 0); return {&this->proto_write_buffer_}; } @@ -467,9 +456,6 @@ class APIConnection : public APIServerConnection { static constexpr uint32_t BATCH_DELAY_MS = 100; static constexpr size_t MAX_BATCH_SIZE_BYTES = 1360; // MTU - 100 bytes safety margin - // Batch mode state - bool batch_mode_{false}; - void schedule_batch_(); void process_batch_(); };