diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index fc6c4d4cf7..ac729e7652 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -93,13 +93,6 @@ void APIConnection::loop() { if (this->remove_) return; - if (!network::is_connected()) { - // when network is disconnected force disconnect immediately - // don't wait for timeout - this->on_fatal_error(); - ESP_LOGW(TAG, "%s: Network unavailable; disconnecting", this->get_client_combined_info().c_str()); - return; - } if (this->next_close_) { // requested a disconnect this->helper_->close(); diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index 8f7add646c..23c8ef30cd 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -159,6 +159,9 @@ void APIServer::loop() { // Process clients and remove disconnected ones in a single pass if (!this->clients_.empty()) { + // Check network connectivity once for all clients + bool network_connected = network::is_connected(); + size_t client_index = 0; while (client_index < this->clients_.size()) { auto &client = this->clients_[client_index]; @@ -181,8 +184,14 @@ void APIServer::loop() { // Don't increment client_index since we need to process the swapped element } else { - // Process active client - client->loop(); + // Process active client only if network is connected + if (network_connected) { + client->loop(); + } else { + // Force disconnect when network is unavailable + client->on_fatal_error(); + ESP_LOGW(TAG, "%s: Network unavailable; disconnecting", client->get_client_combined_info().c_str()); + } client_index++; // Move to next client } }