Add WiFi shutdown hook to fix long deep sleep issue

Co-authored-by: jesserockz <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-07-17 10:06:02 +00:00
parent 331d98830a
commit 12db82e03a
3 changed files with 34 additions and 0 deletions

View File

@ -210,6 +210,13 @@ void WiFiComponent::loop() {
}
}
void WiFiComponent::on_safe_shutdown() {
if (this->has_sta()) {
ESP_LOGD(TAG, "Disconnecting from WiFi...");
this->wifi_disconnect_();
}
}
WiFiComponent::WiFiComponent() { global_wifi_component = this; }
bool WiFiComponent::has_ap() const { return this->has_ap_; }

View File

@ -263,6 +263,9 @@ class WiFiComponent : public Component {
/// Reconnect WiFi if required.
void loop() override;
/// Safely shutdown WiFi before deep sleep
void on_safe_shutdown() override;
bool has_sta() const;
bool has_ap() const;

View File

@ -0,0 +1,24 @@
---
esphome:
name: test_esp8266_wifi_deep_sleep
esp8266:
board: esp12e
wifi:
ssid: "test"
password: "test"
api:
actions:
- action: goto_sleep
variables:
duration_ms: int
then:
- deep_sleep.enter:
id: deep_sleep_1
sleep_duration: !lambda 'return duration_ms;'
deep_sleep:
id: deep_sleep_1
run_duration: 10s