From 25ef0043d24906d84a952fa10433adfd2fc0bda9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 6 Nov 2025 12:07:42 -0600 Subject: [PATCH] preen --- esphome/components/wifi/wifi_component.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index ee4a27e4d4..d2db78ae00 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -111,7 +111,9 @@ void WiFiComponent::start() { #ifdef USE_WIFI_FAST_CONNECT this->trying_loaded_ap_ = this->load_fast_connect_settings_(); if (!this->trying_loaded_ap_) { - // Fast connect failed - start from first configured AP without scan result + // FAST CONNECT FALLBACK: No saved settings available + // Set selected_sta_index_ to first config without any scan result + // build_selected_ap_() will use config data only (no SSID/BSSID/channel from scan) this->selected_sta_index_ = 0; } this->start_connecting_to_selected_(false); @@ -688,6 +690,10 @@ void WiFiComponent::check_scanning_finished() { } // Find matching config for on-demand connection params building + // SYNCHRONIZATION POINT: Establish link between scan_result_[0] and selected_sta_index_ + // After sorting, scan_result_[0] contains the best network. Now find which sta_[i] config + // matches that network and record it in selected_sta_index_. This keeps the two indices + // synchronized so build_selected_ap_() can safely use both to build connection parameters. const WiFiScanResult &scan_res = this->scan_result_[0]; if (!scan_res.get_matches()) { @@ -702,7 +708,7 @@ void WiFiComponent::check_scanning_finished() { continue; } - this->selected_sta_index_ = i; + this->selected_sta_index_ = i; // Links scan_result_[0] with sta_[i] break; } @@ -887,12 +893,15 @@ bool WiFiComponent::load_fast_connect_settings_() { bssid_t bssid{}; std::copy(fast_connect_save.bssid, fast_connect_save.bssid + 6, bssid.begin()); + // FAST CONNECT SUCCESS: Restore saved settings without scanning // Create a temporary scan result with the fast connect BSSID and channel this->scan_result_.init(1); WiFiScanResult fast_connect_scan(bssid, "", fast_connect_save.channel, 0, false, false); this->scan_result_.push_back(fast_connect_scan); - // Set index to use the loaded AP config with temporary scan result + // SYNCHRONIZATION: Link scan_result_[0] (temporary) with sta_[saved_index] + // Unlike wifi_scan_done() which sorts then finds the match, here we know exactly + // which config was used before and create a matching temporary scan result this->selected_sta_index_ = fast_connect_save.ap_index; ESP_LOGD(TAG, "Loaded fast_connect settings");