diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 2b97d7a759..39ffc279f1 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1752,10 +1752,10 @@ void APIConnection::complete_authentication_() { bool APIConnection::send_hello_response_(const HelloRequest &msg) { // Copy client name with truncation if needed (set_client_name handles truncation) this->helper_->set_client_name(msg.client_info.c_str(), msg.client_info.size()); - this->client_api_version_major_ = msg.api_version_major; - this->client_api_version_minor_ = msg.api_version_minor; + this->client_api_version_major_ = static_cast(std::min(msg.api_version_major, 255)); + this->client_api_version_minor_ = static_cast(std::min(msg.api_version_minor, 255)); char peername[socket::SOCKADDR_STR_LEN]; - ESP_LOGV(TAG, "Hello from client: '%s' | %s | API Version %" PRIu16 ".%" PRIu16, this->helper_->get_client_name(), + ESP_LOGV(TAG, "Hello from client: '%s' | %s | API Version %u.%u", this->helper_->get_client_name(), this->helper_->get_peername_to(peername), this->client_api_version_major_, this->client_api_version_minor_); HelloResponse resp; diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index a0a814243a..fb2d473fb9 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -757,13 +757,15 @@ class APIConnection final : public APIServerConnectionBase { #endif } flags_{}; // 2 bytes total - // 2-byte types immediately after flags_ (no padding between them) - uint16_t client_api_version_major_{0}; - uint16_t client_api_version_minor_{0}; + // 2-byte type immediately after flags_ (no padding between them) uint16_t batch_message_type_{0}; // Current message type during batch encoding // 1-byte types to fill remaining space before next 4-byte boundary + // Client API versions are clamped to 255 on receive (see send_hello_response_) + uint8_t client_api_version_major_{0}; + uint8_t client_api_version_minor_{0}; ActiveIterator active_iterator_{ActiveIterator::NONE}; - // Total: 2 (flags) + 2 + 2 + 2 + 1 = 9 bytes + // Total: 2 (flags) + 2 + 1 + 1 + 1 + 1 (batch_header_size_ below) = 8 bytes, + // aligned to 4-byte boundary // Actual header size used by encode_to_buffer for the current message. // Read by process_batch_multi_ to pass into MessageInfo.