From e8de6627d841a3c0f4756f9483dfa9adc80c280a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 3 Jan 2026 00:44:37 -1000 Subject: [PATCH 1/2] document, document, document --- esphome/components/wifi/wifi_component.cpp | 3 ++- esphome/components/wifi/wifi_component.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 9853d26fd3..2c78bbe4d3 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -1293,7 +1293,8 @@ void WiFiComponent::check_connecting_finished(uint32_t now) { } this->roaming_connect_active_ = false; - // Clear all priority penalties - successful connection forgives past failures + // Clear all priority penalties - the next reconnect will happen when an AP disconnects, + // which means the landscape has likely changed and previous tracked failures are stale this->clear_all_bssid_priorities_(); #ifdef USE_WIFI_FAST_CONNECT diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 9b2bcf221f..178e27dfaa 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -501,7 +501,7 @@ class WiFiComponent : public Component { int8_t find_next_hidden_sta_(int8_t start_index); /// Log failed connection and decrease BSSID priority to avoid repeated attempts void log_and_adjust_priority_for_failed_connect_(); - /// Clear all BSSID priority penalties (e.g., after successful connection) + /// Clear all BSSID priority penalties after successful connection (stale after disconnect) void clear_all_bssid_priorities_(); /// Clear BSSID priority tracking if all priorities are at minimum (saves memory) void clear_priorities_if_all_min_(); From f7d9ebcf01d5c5e676ff4f05f1653b4a90c95982 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 3 Jan 2026 09:08:23 -1000 Subject: [PATCH 2/2] reduce --- esphome/components/wifi/wifi_component.cpp | 26 ++++++++-------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 2c78bbe4d3..1d798495c1 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -578,11 +578,13 @@ void WiFiComponent::loop() { this->last_connected_ = now; // Post-connect roaming: check for better AP - if (this->roaming_scan_active_) { - this->process_roaming_scan_(); - } else if (this->post_connect_roaming_ && this->roaming_attempts_ < ROAMING_MAX_ATTEMPTS && - now - this->roaming_last_check_ >= ROAMING_CHECK_INTERVAL) { - this->check_roaming_(now); + if (this->post_connect_roaming_) { + if (this->roaming_scan_active_ && this->scan_done_) { + this->process_roaming_scan_(); + } else if (this->roaming_attempts_ < ROAMING_MAX_ATTEMPTS && + now - this->roaming_last_check_ >= ROAMING_CHECK_INTERVAL) { + this->check_roaming_(now); + } } } break; @@ -2011,10 +2013,6 @@ void WiFiComponent::check_roaming_(uint32_t now) { } void WiFiComponent::process_roaming_scan_() { - // Scan not done yet - if (!this->scan_done_) - return; - this->scan_done_ = false; this->roaming_scan_active_ = false; @@ -2052,20 +2050,14 @@ void WiFiComponent::process_roaming_scan_() { } // Check if best candidate meets minimum improvement threshold + const WiFiAP *selected = this->get_selected_sta_(); int8_t improvement = (best == nullptr) ? 0 : best->get_rssi() - current_rssi; - if (improvement < ROAMING_MIN_IMPROVEMENT) { + if (selected == nullptr || improvement < ROAMING_MIN_IMPROVEMENT) { ESP_LOGV(TAG, "Roam best %+d dB (need +%d)", improvement, ROAMING_MIN_IMPROVEMENT); this->release_scan_results_(); return; } - // Found better AP - initiate roam - const WiFiAP *selected = this->get_selected_sta_(); - if (selected == nullptr) { - this->release_scan_results_(); - return; // Defensive: shouldn't happen since clear_sta() clears roaming_scan_active_ - } - format_mac_addr_upper(best->get_bssid().data(), bssid_buf); ESP_LOGI(TAG, "Roaming to %s (%+d dB)", bssid_buf, improvement);