diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index c0dc01cd728..c14ae396897 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -768,14 +768,15 @@ void WiFiComponent::restart_adapter() { void WiFiComponent::on_powerdown() { // Leave the AP so it no longer holds the association when we come back. +#ifdef USE_LN882X + if (!this->wifi_powerdown_disconnect_()) + return; +#else if (!this->is_connected()) return; - ESP_LOGD(TAG, "Disconnecting before powerdown"); -#ifdef USE_LN882X - this->wifi_powerdown_disconnect_(); -#else this->wifi_disconnect_(); #endif + ESP_LOGD(TAG, "Disconnected before powerdown"); delay(100); // NOLINT: once per reboot or deep sleep, lets the deauth go out } diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 02db109a89a..4a454a44363 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -793,7 +793,7 @@ class WiFiComponent final : public Component { bool wifi_disconnect_(); #ifdef USE_LN882X - void wifi_powerdown_disconnect_(); + bool wifi_powerdown_disconnect_(); #endif network::IPAddress wifi_subnet_mask_(); diff --git a/esphome/components/wifi/wifi_component_libretiny.cpp b/esphome/components/wifi/wifi_component_libretiny.cpp index 4ad065edb05..c7d87154efd 100644 --- a/esphome/components/wifi/wifi_component_libretiny.cpp +++ b/esphome/components/wifi/wifi_component_libretiny.cpp @@ -790,8 +790,11 @@ network::IPAddress WiFiComponent::wifi_soft_ap_ip() { return {WiFi.softAPIP()}; #endif // USE_WIFI_AP #ifdef USE_LN882X -void WiFiComponent::wifi_powerdown_disconnect_() { +bool WiFiComponent::wifi_powerdown_disconnect_() { + if (WiFi.status() != WL_CONNECTED) // associated, even before DHCP has finished + return false; wifi_sta_disconnect(); // not WiFi.disconnect(): it frees the SSID its event handlers read + return true; } #endif