From 9bd4b229e3cdc5d599c4b2612b744c85494fcfad Mon Sep 17 00:00:00 2001 From: Keith Burzinski Date: Tue, 7 Nov 2023 12:00:36 -0600 Subject: [PATCH] Handle on_disconnect when Wi-Fi is disabled (#5691) --- esphome/components/wifi/wifi_component.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 95d430de4f..cd87bb48a7 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -109,6 +109,15 @@ void WiFiComponent::loop() { const uint32_t now = millis(); if (this->has_sta()) { + if (this->is_connected() != this->handled_connected_state_) { + if (this->handled_connected_state_) { + this->disconnect_trigger_->trigger(); + } else { + this->connect_trigger_->trigger(); + } + this->handled_connected_state_ = this->is_connected(); + } + switch (this->state_) { case WIFI_COMPONENT_STATE_COOLDOWN: { this->status_set_warning(); @@ -138,19 +147,9 @@ void WiFiComponent::loop() { ESP_LOGW(TAG, "WiFi Connection lost... Reconnecting..."); this->state_ = WIFI_COMPONENT_STATE_STA_CONNECTING; this->retry_connect(); - - if (this->handled_connected_state_) { - this->disconnect_trigger_->trigger(); - this->handled_connected_state_ = false; - } } else { this->status_clear_warning(); this->last_connected_ = now; - - if (!this->handled_connected_state_) { - this->connect_trigger_->trigger(); - this->handled_connected_state_ = true; - } } break; }