From bf312ad9ec0b4e3f76aa6398a35e93a930b18490 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 11 Nov 2025 08:50:54 -0600 Subject: [PATCH 1/2] fixes --- esphome/components/wifi/wifi_component.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index b7cac68cd7..83e8aa90cb 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -1301,14 +1301,21 @@ void WiFiComponent::clear_priorities_if_all_same_() { } int8_t first_priority = this->sta_priorities_[0].priority; + + // Only clear if all priorities have been decremented to the minimum value + // At this point, all BSSIDs have been equally penalized and priority info is useless + if (first_priority != std::numeric_limits::min()) { + return; + } + for (const auto &pri : this->sta_priorities_) { if (pri.priority != first_priority) { return; // Not all same, nothing to do } } - // All priorities are identical - clear the vector to save memory - ESP_LOGD(TAG, "Clearing BSSID priorities (all identical)"); + // All priorities are at minimum - clear the vector to save memory and reset + ESP_LOGD(TAG, "Clearing BSSID priorities (all at minimum)"); this->sta_priorities_.clear(); this->sta_priorities_.shrink_to_fit(); } From bee174150b0d4a31357173c99b07608497c0937b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 11 Nov 2025 08:52:12 -0600 Subject: [PATCH 2/2] fixes --- esphome/components/wifi/wifi_component.cpp | 17 +++++++++-------- esphome/components/wifi/wifi_component.h | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 83e8aa90cb..4a5a4cbe55 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -1062,8 +1062,8 @@ void WiFiComponent::check_connecting_finished() { this->state_ = WIFI_COMPONENT_STATE_STA_CONNECTED; this->num_retried_ = 0; - // Clear priority tracking if all priorities are identical - this->clear_priorities_if_all_same_(); + // Clear priority tracking if all priorities are at minimum + this->clear_priorities_if_all_min_(); #ifdef USE_WIFI_FAST_CONNECT this->save_fast_connect_settings_(); @@ -1293,9 +1293,10 @@ bool WiFiComponent::transition_to_phase_(WiFiRetryPhase new_phase) { return false; // Did not start scan, can proceed with connection } -/// Clear BSSID priority tracking if all priorities are identical (can't differentiate, saves memory) -/// Called when starting a fresh connection attempt or after successful connection -void WiFiComponent::clear_priorities_if_all_same_() { +/// Clear BSSID priority tracking if all priorities are at minimum (saves memory) +/// At minimum priority, all BSSIDs are equally bad, so priority tracking is useless +/// Called after successful connection or after failed connection attempts +void WiFiComponent::clear_priorities_if_all_min_() { if (this->sta_priorities_.empty()) { return; } @@ -1366,9 +1367,9 @@ void WiFiComponent::log_and_adjust_priority_for_failed_connect_() { ESP_LOGD(TAG, "Failed " LOG_SECRET("'%s'") " " LOG_SECRET("(%s)") ", priority %d → %d", ssid.c_str(), format_mac_address_pretty(failed_bssid.value().data()).c_str(), old_priority, new_priority); - // After adjusting priority, check if all priorities are now identical - // If so, clear the vector to save memory - this->clear_priorities_if_all_same_(); + // After adjusting priority, check if all priorities are now at minimum + // If so, clear the vector to save memory and reset for fresh start + this->clear_priorities_if_all_min_(); } /// Handle target advancement or retry counter increment when staying in the same phase diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 84dfa57ab1..b8223e8dc8 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -383,8 +383,8 @@ class WiFiComponent : public Component { int8_t find_next_hidden_sta_(int8_t start_index, bool include_explicit_hidden = true); /// Log failed connection and decrease BSSID priority to avoid repeated attempts void log_and_adjust_priority_for_failed_connect_(); - /// Clear BSSID priority tracking if all priorities are identical (saves memory) - void clear_priorities_if_all_same_(); + /// Clear BSSID priority tracking if all priorities are at minimum (saves memory) + void clear_priorities_if_all_min_(); /// Advance to next target (AP/SSID) within current phase, or increment retry counter /// Called when staying in the same phase after a failed connection attempt void advance_to_next_target_or_increment_retry_();