diff --git a/esphome/components/esp32_improv/esp32_improv_component.cpp b/esphome/components/esp32_improv/esp32_improv_component.cpp index 5c32f82abb..b3258aedac 100644 --- a/esphome/components/esp32_improv/esp32_improv_component.cpp +++ b/esphome/components/esp32_improv/esp32_improv_component.cpp @@ -40,9 +40,6 @@ void ESP32ImprovComponent::setup() { #endif global_ble_server->on_disconnect([this](uint16_t conn_id) { this->set_error_(improv::ERROR_NONE); }); - // Listen for WiFi connections to detect when provisioning happens via captive portal or other means - wifi::global_wifi_component->get_connect_trigger()->add_callback([this]() { this->on_wifi_connected_(); }); - // Start with loop disabled - will be enabled by start() when needed this->disable_loop(); } @@ -146,6 +143,7 @@ void ESP32ImprovComponent::loop() { #else this->set_state_(improv::STATE_AUTHORIZED); #endif + this->check_wifi_connection_(); break; } case improv::STATE_AUTHORIZED: { @@ -159,10 +157,12 @@ void ESP32ImprovComponent::loop() { if (!this->check_identify_()) { this->set_status_indicator_state_((now % 1000) < 500); } + this->check_wifi_connection_(); break; } case improv::STATE_PROVISIONING: { this->set_status_indicator_state_((now % 200) < 100); + this->check_wifi_connection_(); break; } case improv::STATE_PROVISIONED: { @@ -374,6 +374,12 @@ void ESP32ImprovComponent::on_wifi_connect_timeout_() { wifi::global_wifi_component->clear_sta(); } +void ESP32ImprovComponent::check_wifi_connection_() { + if (wifi::global_wifi_component->is_connected()) { + this->on_wifi_connected_(); + } +} + void ESP32ImprovComponent::on_wifi_connected_() { // Handle WiFi connection, whether from Improv provisioning or external (e.g., captive portal) if (this->state_ == improv::STATE_PROVISIONING) { diff --git a/esphome/components/esp32_improv/esp32_improv_component.h b/esphome/components/esp32_improv/esp32_improv_component.h index 39c3483b2a..da670f54bc 100644 --- a/esphome/components/esp32_improv/esp32_improv_component.h +++ b/esphome/components/esp32_improv/esp32_improv_component.h @@ -112,6 +112,7 @@ class ESP32ImprovComponent : public Component { void process_incoming_data_(); void on_wifi_connect_timeout_(); void on_wifi_connected_(); + void check_wifi_connection_(); bool check_identify_(); void advertise_service_data_(); #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG