diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 765f750193..0182d27b15 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; @@ -1305,7 +1307,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 @@ -2023,10 +2026,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; @@ -2064,20 +2063,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); 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_();