From b366bc8dbad8ece73fd6681b57793b50a8c952f1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 6 Nov 2025 15:40:32 -0600 Subject: [PATCH] defensive to make bot happy --- esphome/components/wifi/wifi_component.cpp | 21 +++++++++++++++++++++ esphome/components/wifi/wifi_component.h | 21 +-------------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index a6f44ac758..6e4611d765 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -396,6 +396,27 @@ WiFiAP WiFiComponent::build_selected_ap_() 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])) { + if (i > std::numeric_limits::max()) { + ESP_LOGE(TAG, "AP index %zu too large", i); + return false; + } + this->selected_sta_index_ = static_cast(i); // Links scan_result_[0] with sta_[i] + return true; + } + } + return false; +} + WiFiAP WiFiComponent::get_sta() { const WiFiAP *config = this->get_selected_sta_(); return config ? *config : WiFiAP{}; diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 3dc31880a8..6cdab1660f 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -365,26 +365,7 @@ class WiFiComponent : public Component { // Find which sta_[i] matches scan_result_[0] and set selected_sta_index_ (scan done path) // Returns true if match found, false otherwise - bool 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])) { - if (i > std::numeric_limits::max()) { - ESP_LOGE(TAG, "AP index %zu too large", i); - continue; - } - this->selected_sta_index_ = static_cast(i); // Links scan_result_[0] with sta_[i] - return true; - } - } - return false; - } + bool sync_selected_sta_to_best_scan_result_(); void start_connecting_to_selected_(bool two) { WiFiAP connection_params = this->build_selected_ap_();