From 81bc2d82d691bf64eb53eee0aa6e125531d9d5f3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 6 Nov 2025 17:30:16 -0600 Subject: [PATCH] dry --- esphome/components/wifi/wifi_component.cpp | 39 +++++++++++----------- esphome/components/wifi/wifi_component.h | 4 --- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 9531c6dfc8..910edb01ac 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -377,25 +377,6 @@ WiFiAP WiFiComponent::build_wifi_ap_from_selected_() const { return params; } -bool WiFiComponent::sync_selected_sta_to_best_scan_result_() { - if (this->scan_result_.empty()) - return false; - - const WiFiScanResult &scan_res = this->scan_result_[0]; - if (!scan_res.get_matches()) - return false; - - for (size_t i = 0; i < this->sta_.size(); i++) { - if (scan_res.matches(this->sta_[i])) { - // Safe cast: sta_.size() limited to MAX_WIFI_NETWORKS (127) in __init__.py validation - // No overflow check needed - YAML validation prevents >127 networks - this->selected_sta_index_ = static_cast(i); // Links scan_result_[0] with sta_[i] - return true; - } - } - return false; -} - WiFiAP WiFiComponent::get_sta() const { const WiFiAP *config = this->get_selected_sta_(); return config ? *config : WiFiAP{}; @@ -699,7 +680,25 @@ void WiFiComponent::check_scanning_finished() { // 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_wifi_ap_from_selected_() can safely use both to build connection parameters. - if (!this->sync_selected_sta_to_best_scan_result_()) { + const WiFiScanResult &scan_res = this->scan_result_[0]; + if (!scan_res.get_matches()) { + ESP_LOGW(TAG, "No matching network found"); + this->retry_connect(); + return; + } + + bool found_match = false; + for (size_t i = 0; i < this->sta_.size(); i++) { + if (scan_res.matches(this->sta_[i])) { + // Safe cast: sta_.size() limited to MAX_WIFI_NETWORKS (127) in __init__.py validation + // No overflow check needed - YAML validation prevents >127 networks + this->selected_sta_index_ = static_cast(i); // Links scan_result_[0] with sta_[i] + found_match = true; + break; + } + } + + if (!found_match) { ESP_LOGW(TAG, "No matching network found"); this->retry_connect(); return; diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index f5b50f29e9..a457ae6bd9 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -353,10 +353,6 @@ class WiFiComponent : public Component { } } - // SYNCHRONIZATION HELPER: Find which sta_[i] matches scan_result_[0] and set selected_sta_index_ - // Returns true if match found, false otherwise (scan done path) - bool sync_selected_sta_to_best_scan_result_(); - #ifdef USE_WIFI_FAST_CONNECT // Reset state for next fast connect AP attempt // Clears old scan data so the new AP is tried with config only (SSID without specific BSSID/channel)