From 369f32b4967f8c413ed5ae05d02c4e8a0eba3bff Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 2 Jan 2026 22:50:38 -1000 Subject: [PATCH 1/7] reduce some code size --- esphome/components/wifi/wifi_component.cpp | 22 +++++++++------------- esphome/components/wifi/wifi_component.h | 6 ++++++ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 2027a5dc55..3254fcf61c 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -1277,9 +1277,7 @@ void WiFiComponent::check_connecting_finished(uint32_t now) { this->roaming_connect_active_ = false; // Clear all priority penalties - successful connection forgives past failures - if (!this->sta_priorities_.empty()) { - decltype(this->sta_priorities_)().swap(this->sta_priorities_); - } + this->clear_all_bssid_priorities_(); #ifdef USE_WIFI_FAST_CONNECT this->save_fast_connect_settings_(); @@ -1557,7 +1555,7 @@ void WiFiComponent::clear_priorities_if_all_min_() { // All priorities are at minimum - clear the vector to save memory and reset ESP_LOGD(TAG, "Clearing BSSID priorities (all at minimum)"); - decltype(this->sta_priorities_)().swap(this->sta_priorities_); + this->clear_all_bssid_priorities_(); } /// Log failed connection attempt and decrease BSSID priority to avoid repeated failures @@ -2004,12 +2002,14 @@ void WiFiComponent::process_roaming_scan_(uint32_t now) { // Get current connection info bssid_t current_bssid = this->wifi_bssid(); int8_t current_rssi = this->wifi_rssi(); - std::string current_ssid = this->wifi_ssid(); + char ssid_buf[SSID_BUFFER_SIZE]; + const char *current_ssid = this->wifi_ssid_to(ssid_buf); // Find best candidate: same SSID, different BSSID bssid_t best_bssid{}; uint8_t best_channel = 0; int8_t best_rssi = WIFI_RSSI_DISCONNECTED; + char bssid_buf[18]; for (const auto &result : this->scan_result_) { // Must be same SSID as current connection @@ -2021,11 +2021,8 @@ void WiFiComponent::process_roaming_scan_(uint32_t now) { continue; #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE - { - char bssid_buf[18]; - format_mac_addr_upper(result.get_bssid().data(), bssid_buf); - ESP_LOGV(TAG, "Roaming: candidate %s RSSI %d dBm", bssid_buf, result.get_rssi()); - } + format_mac_addr_upper(result.get_bssid().data(), bssid_buf); + ESP_LOGV(TAG, "Roaming: candidate %s RSSI %d dBm", bssid_buf, result.get_rssi()); #endif // Track the best candidate @@ -2050,9 +2047,8 @@ void WiFiComponent::process_roaming_scan_(uint32_t now) { if (selected == nullptr) return; // Defensive: shouldn't happen since clear_sta() clears roaming_scan_active_ - char bssid_s[18]; - format_mac_addr_upper(best_bssid.data(), bssid_s); - ESP_LOGI(TAG, "Roaming to %s (%+d dB)", bssid_s, improvement); + format_mac_addr_upper(best_bssid.data(), bssid_buf); + ESP_LOGI(TAG, "Roaming to %s (%+d dB)", bssid_buf, improvement); WiFiAP roam_params = *selected; roam_params.set_bssid(best_bssid); diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index cb02394bd6..69dcab3fae 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -507,6 +507,12 @@ 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) + void clear_all_bssid_priorities_() { + if (!this->sta_priorities_.empty()) { + decltype(this->sta_priorities_)().swap(this->sta_priorities_); + } + } /// 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 From 039ae65ed8dad48a3300403f56c62281a90a2a39 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 2 Jan 2026 22:52:01 -1000 Subject: [PATCH 2/7] Update esphome/components/wifi/wifi_component.cpp --- esphome/components/wifi/wifi_component.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 5491e84d90..634691a6d8 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -163,7 +163,7 @@ static const char *const TAG = "wifi"; /// │ │ Start scan │ (same as normal scan) │ │ /// │ └────────┬────────┘ │ │ /// │ ↓ │ │ -/// │ ┌─────────────────────────┐ │ │ +/// │ ┌────────────────────────┐ │ │ /// │ │ process_roaming_scan_ │ roaming_attempts_++ │ │ /// │ └────────┬───────────────┘ │ │ /// │ ↓ │ │ From 828a27b1b6524d46f192938f3a7a90c38c042e2b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 2 Jan 2026 22:53:42 -1000 Subject: [PATCH 3/7] reduce some code size --- esphome/components/wifi/wifi_component.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 634691a6d8..35b521cc28 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -1968,11 +1968,6 @@ void WiFiComponent::check_roaming_(uint32_t now) { if (!this->post_connect_roaming_) return; - // Guard: not for hidden networks (may not appear in scan) - const WiFiAP *selected = this->get_selected_sta_(); - if (selected == nullptr || selected->get_hidden()) - return; - // Guard: attempt limit if (this->roaming_attempts_ >= ROAMING_MAX_ATTEMPTS) return; @@ -1990,6 +1985,11 @@ void WiFiComponent::check_roaming_(uint32_t now) { if (current_rssi == WIFI_RSSI_DISCONNECTED) return; + // Guard: not for hidden networks (may not appear in scan) + const WiFiAP *selected = this->get_selected_sta_(); + if (selected == nullptr || selected->get_hidden()) + return; + this->roaming_last_check_ = now; ESP_LOGD(TAG, "Roaming: scanning for better AP (current RSSI %d dBm)", current_rssi); this->roaming_scan_active_ = true; From 1c9e0f6b225aaba43eb0c18fa4091636b1b46d60 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 2 Jan 2026 22:56:48 -1000 Subject: [PATCH 4/7] optimize --- esphome/components/wifi/wifi_component.cpp | 28 +++++----------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 35b521cc28..8abad140ed 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -575,8 +575,12 @@ void WiFiComponent::loop() { this->last_connected_ = now; // Post-connect roaming: check for better AP - this->check_roaming_(now); - this->process_roaming_scan_(now); + if (this->roaming_scan_active_) { + this->process_roaming_scan_(now); + } else if (this->post_connect_roaming_ && this->roaming_attempts_ < ROAMING_MAX_ATTEMPTS && + now - this->roaming_last_check_ >= ROAMING_CHECK_INTERVAL) { + this->check_roaming_(now); + } } break; } @@ -1964,22 +1968,6 @@ void WiFiComponent::clear_roaming_state_() { } void WiFiComponent::check_roaming_(uint32_t now) { - // Guard: feature enabled - if (!this->post_connect_roaming_) - return; - - // Guard: attempt limit - if (this->roaming_attempts_ >= ROAMING_MAX_ATTEMPTS) - return; - - // Guard: scan not already active - if (this->roaming_scan_active_) - return; - - // Guard: interval check - if (now - this->roaming_last_check_ < ROAMING_CHECK_INTERVAL) - return; - // Guard: must have valid RSSI reading int8_t current_rssi = this->wifi_rssi(); if (current_rssi == WIFI_RSSI_DISCONNECTED) @@ -1997,10 +1985,6 @@ void WiFiComponent::check_roaming_(uint32_t now) { } void WiFiComponent::process_roaming_scan_(uint32_t now) { - // Not our scan - if (!this->roaming_scan_active_) - return; - // Scan not done yet if (!this->scan_done_) return; From 516c074b8fffa88084e2f3592a8e67da9557dcee Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 2 Jan 2026 22:58:18 -1000 Subject: [PATCH 5/7] optimize --- esphome/components/wifi/wifi_component.cpp | 4 ++-- esphome/components/wifi/wifi_component.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 8abad140ed..4fc4009360 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -576,7 +576,7 @@ void WiFiComponent::loop() { // Post-connect roaming: check for better AP if (this->roaming_scan_active_) { - this->process_roaming_scan_(now); + 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); @@ -1984,7 +1984,7 @@ void WiFiComponent::check_roaming_(uint32_t now) { this->wifi_scan_start_(this->passive_scan_); } -void WiFiComponent::process_roaming_scan_(uint32_t now) { +void WiFiComponent::process_roaming_scan_() { // Scan not done yet if (!this->scan_done_) return; diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index b40dc6e849..56d8357f59 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -572,7 +572,7 @@ class WiFiComponent : public Component { // Post-connect roaming methods void check_roaming_(uint32_t now); - void process_roaming_scan_(uint32_t now); + void process_roaming_scan_(); void clear_roaming_state_(); /// Free scan results memory unless a component needs them From 996bd12871ed97da4a594c9f92ddaa816b69e5f4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 2 Jan 2026 23:03:52 -1000 Subject: [PATCH 6/7] optimize --- esphome/components/wifi/wifi_component.cpp | 25 ++++++++++------------ 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 4fc4009360..5fa6623a7d 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -2000,9 +2000,7 @@ void WiFiComponent::process_roaming_scan_() { const char *current_ssid = this->wifi_ssid_to(ssid_buf); // Find best candidate: same SSID, different BSSID - bssid_t best_bssid{}; - uint8_t best_channel = 0; - int8_t best_rssi = WIFI_RSSI_DISCONNECTED; + const WiFiScanResult *best = nullptr; char bssid_buf[18]; for (const auto &result : this->scan_result_) { @@ -2020,33 +2018,32 @@ void WiFiComponent::process_roaming_scan_() { #endif // Track the best candidate - if (result.get_rssi() > best_rssi) { - best_rssi = result.get_rssi(); - best_bssid = result.get_bssid(); - best_channel = result.get_channel(); + if (best == nullptr || result.get_rssi() > best->get_rssi()) { + best = &result; } } - this->release_scan_results_(); - // Check if best candidate meets minimum improvement threshold - int8_t improvement = (best_rssi == WIFI_RSSI_DISCONNECTED) ? 0 : best_rssi - current_rssi; + int8_t improvement = (best == nullptr) ? 0 : best->get_rssi() - current_rssi; if (improvement < ROAMING_MIN_IMPROVEMENT) { ESP_LOGV(TAG, "Roaming: best candidate %+d dB (need +%d dB)", improvement, ROAMING_MIN_IMPROVEMENT); + this->release_scan_results_(); return; } // Found better AP - initiate roam const WiFiAP *selected = this->get_selected_sta_(); - if (selected == nullptr) + if (selected == nullptr) { + this->release_scan_results_(); return; // Defensive: shouldn't happen since clear_sta() clears roaming_scan_active_ + } - format_mac_addr_upper(best_bssid.data(), bssid_buf); + format_mac_addr_upper(best->get_bssid().data(), bssid_buf); ESP_LOGI(TAG, "Roaming to %s (%+d dB)", bssid_buf, improvement); WiFiAP roam_params = *selected; - roam_params.set_bssid(best_bssid); - roam_params.set_channel(best_channel); + apply_scan_result_to_params(roam_params, *best); + this->release_scan_results_(); // Mark as roaming attempt - affects retry behavior if connection fails this->roaming_connect_active_ = true; From f32c1909054cef0772373965b74c372e1d4fc5c2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 2 Jan 2026 23:05:39 -1000 Subject: [PATCH 7/7] optimize --- esphome/components/wifi/wifi_component.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 5fa6623a7d..b95def4121 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -2001,7 +2001,7 @@ void WiFiComponent::process_roaming_scan_() { // Find best candidate: same SSID, different BSSID const WiFiScanResult *best = nullptr; - char bssid_buf[18]; + char bssid_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE]; for (const auto &result : this->scan_result_) { // Must be same SSID as current connection