1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-19 19:52:20 +01:00

more api loop reductions

This commit is contained in:
J. Nick Koston
2025-06-23 10:59:00 +02:00
parent d6725fc1ca
commit e8c250a03c
2 changed files with 11 additions and 9 deletions

View File

@@ -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();

View File

@@ -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
// 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
}
}