From 39f77a3315eea22ab74fd933a742fadb4500944a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 13 Jan 2026 08:14:46 -1000 Subject: [PATCH] [wifi] Fix ESP8266 disconnect callback order to set error flag before notifying listeners --- esphome/components/wifi/wifi_component_esp8266.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/esphome/components/wifi/wifi_component_esp8266.cpp b/esphome/components/wifi/wifi_component_esp8266.cpp index a68111f045..61c4584d09 100644 --- a/esphome/components/wifi/wifi_component_esp8266.cpp +++ b/esphome/components/wifi/wifi_component_esp8266.cpp @@ -543,10 +543,12 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) { } s_sta_connected = false; s_sta_connecting = false; - // Set error flag BEFORE notifying listeners so is_connected() returns - // correct state during listener callbacks (matches ESP-IDF behavior) + // IMPORTANT: Set error flag BEFORE notifying listeners. + // This ensures is_connected() returns false during listener callbacks, + // which is critical for proper reconnection logic (e.g., roaming). global_wifi_component->error_from_callback_ = true; #ifdef USE_WIFI_LISTENERS + // Notify listeners AFTER setting error flag so they see correct state static constexpr uint8_t EMPTY_BSSID[6] = {}; for (auto *listener : global_wifi_component->connect_state_listeners_) { listener->on_wifi_connect_state(StringRef(), EMPTY_BSSID);