From bac96086be2a3702c5f01afa317670d6ebb2d27a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 25 Jan 2026 07:16:07 -1000 Subject: [PATCH] [wifi] Fix scan flag race condition causing reconnect failure on ESP8266/LibreTiny (#13514) --- esphome/components/wifi/wifi_component_esp8266.cpp | 4 ++++ esphome/components/wifi/wifi_component_libretiny.cpp | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/esphome/components/wifi/wifi_component_esp8266.cpp b/esphome/components/wifi/wifi_component_esp8266.cpp index de0600cf5b..91db7ae0eb 100644 --- a/esphome/components/wifi/wifi_component_esp8266.cpp +++ b/esphome/components/wifi/wifi_component_esp8266.cpp @@ -698,6 +698,10 @@ bool WiFiComponent::wifi_scan_start_(bool passive) { if (!this->wifi_mode_(true, {})) return false; + // Reset scan_done_ before starting new scan to prevent stale flag from previous scan + // (e.g., roaming scan completed just before unexpected disconnect) + this->scan_done_ = false; + struct scan_config config {}; memset(&config, 0, sizeof(config)); config.ssid = nullptr; diff --git a/esphome/components/wifi/wifi_component_libretiny.cpp b/esphome/components/wifi/wifi_component_libretiny.cpp index cc9f4ec193..20cd32fa8f 100644 --- a/esphome/components/wifi/wifi_component_libretiny.cpp +++ b/esphome/components/wifi/wifi_component_libretiny.cpp @@ -649,6 +649,10 @@ bool WiFiComponent::wifi_scan_start_(bool passive) { if (!this->wifi_mode_(true, {})) return false; + // Reset scan_done_ before starting new scan to prevent stale flag from previous scan + // (e.g., roaming scan completed just before unexpected disconnect) + this->scan_done_ = false; + // need to use WiFi because of WiFiScanClass allocations :( int16_t err = WiFi.scanNetworks(true, true, passive, 200); if (err != WIFI_SCAN_RUNNING) {