Compare commits

..

1 Commits

Author SHA1 Message Date
J. Nick Koston
5b68d9b589 [wifi] Conditionally compile on_connect/on_disconnect triggers 2026-02-01 20:13:34 +01:00
5 changed files with 28 additions and 3 deletions

View File

@@ -585,11 +585,13 @@ async def to_code(config):
await cg.past_safe_mode()
if on_connect_config := config.get(CONF_ON_CONNECT):
cg.add_define("USE_WIFI_CONNECT_TRIGGER")
await automation.build_automation(
var.get_connect_trigger(), [], on_connect_config
)
if on_disconnect_config := config.get(CONF_ON_DISCONNECT):
cg.add_define("USE_WIFI_DISCONNECT_TRIGGER")
await automation.build_automation(
var.get_disconnect_trigger(), [], on_disconnect_config
)

View File

@@ -651,14 +651,21 @@ void WiFiComponent::loop() {
const uint32_t now = App.get_loop_component_start_time();
if (this->has_sta()) {
#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
if (this->is_connected() != this->handled_connected_state_) {
#ifdef USE_WIFI_DISCONNECT_TRIGGER
if (this->handled_connected_state_) {
this->disconnect_trigger_->trigger();
} else {
}
#endif
#ifdef USE_WIFI_CONNECT_TRIGGER
if (!this->handled_connected_state_) {
this->connect_trigger_->trigger();
}
#endif
this->handled_connected_state_ = this->is_connected();
}
#endif // USE_WIFI_CONNECT_TRIGGER || USE_WIFI_DISCONNECT_TRIGGER
switch (this->state_) {
case WIFI_COMPONENT_STATE_COOLDOWN: {

View File

@@ -454,8 +454,12 @@ class WiFiComponent : public Component {
void set_keep_scan_results(bool keep_scan_results) { this->keep_scan_results_ = keep_scan_results; }
void set_post_connect_roaming(bool enabled) { this->post_connect_roaming_ = enabled; }
Trigger<> *get_connect_trigger() const { return this->connect_trigger_; };
Trigger<> *get_disconnect_trigger() const { return this->disconnect_trigger_; };
#ifdef USE_WIFI_CONNECT_TRIGGER
Trigger<> *get_connect_trigger() const { return this->connect_trigger_; }
#endif
#ifdef USE_WIFI_DISCONNECT_TRIGGER
Trigger<> *get_disconnect_trigger() const { return this->disconnect_trigger_; }
#endif
int32_t get_wifi_channel();
@@ -706,7 +710,9 @@ class WiFiComponent : public Component {
// Group all boolean values together
bool has_ap_{false};
#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
bool handled_connected_state_{false};
#endif
bool error_from_callback_{false};
bool scan_done_{false};
bool ap_setup_{false};
@@ -734,8 +740,12 @@ class WiFiComponent : public Component {
#endif
// Pointers at the end (naturally aligned)
#ifdef USE_WIFI_CONNECT_TRIGGER
Trigger<> *connect_trigger_{new Trigger<>()};
#endif
#ifdef USE_WIFI_DISCONNECT_TRIGGER
Trigger<> *disconnect_trigger_{new Trigger<>()};
#endif
private:
// Stores a pointer to a string literal (static storage duration).

View File

@@ -227,6 +227,8 @@
#define USE_WIFI_SCAN_RESULTS_LISTENERS
#define USE_WIFI_CONNECT_STATE_LISTENERS
#define USE_WIFI_POWER_SAVE_LISTENERS
#define USE_WIFI_CONNECT_TRIGGER
#define USE_WIFI_DISCONNECT_TRIGGER
#define ESPHOME_WIFI_IP_STATE_LISTENERS 2
#define ESPHOME_WIFI_SCAN_RESULTS_LISTENERS 2
#define ESPHOME_WIFI_CONNECT_STATE_LISTENERS 2

View File

@@ -26,3 +26,7 @@ wifi:
- ssid: MySSID3
password: password3
priority: 0
on_connect:
- logger.log: "WiFi connected!"
on_disconnect:
- logger.log: "WiFi disconnected!"