From 4b747f82fbb0f0f84c69f7514ee4eb5590ba2b29 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 4 Oct 2026 21:06:51 -0500 Subject: [PATCH] [wifi] Leave the AP before a reboot on every platform --- esphome/components/wifi/wifi_component.cpp | 13 +++++++++++++ esphome/components/wifi/wifi_component.h | 2 -- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 125139ad163..04e9e5a7a0b 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -766,6 +766,19 @@ void WiFiComponent::restart_adapter() { this->error_from_callback_ = false; } +#ifndef USE_LN882X // LN882x leaves through the SDK directly; see wifi_component_libretiny.cpp +void WiFiComponent::on_powerdown() { + // Leave the AP before a reboot or deep sleep, after teardown has let the + // API say goodbye: otherwise the AP still holds the association when the + // device comes back and the first connects can stall or fail. + if (!this->is_connected()) + return; + ESP_LOGD(TAG, "Disconnecting before reboot"); + this->wifi_disconnect_(); + delay(100); // let the deauth go out before the reset +} +#endif + void WiFiComponent::loop() { bool events_processed = this->wifi_loop_(); const uint32_t now = App.get_loop_component_start_time(); diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 9418165501c..02c893e5dc7 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -506,9 +506,7 @@ class WiFiComponent final : public Component { // (In most use cases you won't need these) /// Setup WiFi interface. void setup() override; -#ifdef USE_LN882X void on_powerdown() override; -#endif void start(); void dump_config() override; void restart_adapter();