Compare commits

...

2 Commits

Author SHA1 Message Date
J. Nick Koston
cc1cb28856 needless heap 2026-02-02 03:02:43 +01:00
J. Nick Koston
5b68d9b589 [wifi] Conditionally compile on_connect/on_disconnect triggers 2026-02-01 20:13:34 +01:00
6 changed files with 40 additions and 16 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

@@ -48,7 +48,7 @@ template<typename... Ts> class WiFiConfigureAction : public Action<Ts...>, publi
char ssid_buf[SSID_BUFFER_SIZE];
if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), ssid.c_str()) == 0) {
// Callback to notify the user that the connection was successful
this->connect_trigger_->trigger();
this->connect_trigger_.trigger();
return;
}
// Create a new WiFiAP object with the new SSID and password
@@ -79,13 +79,13 @@ template<typename... Ts> class WiFiConfigureAction : public Action<Ts...>, publi
// Start a timeout for the fallback if the connection to the old AP fails
this->set_timeout("wifi-fallback-timeout", this->connection_timeout_.value(x...), [this]() {
this->connecting_ = false;
this->error_trigger_->trigger();
this->error_trigger_.trigger();
});
});
}
Trigger<> *get_connect_trigger() const { return this->connect_trigger_; }
Trigger<> *get_error_trigger() const { return this->error_trigger_; }
Trigger<> *get_connect_trigger() { return &this->connect_trigger_; }
Trigger<> *get_error_trigger() { return &this->error_trigger_; }
void loop() override {
if (!this->connecting_)
@@ -98,10 +98,10 @@ template<typename... Ts> class WiFiConfigureAction : public Action<Ts...>, publi
char ssid_buf[SSID_BUFFER_SIZE];
if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), this->new_sta_.get_ssid().c_str()) == 0) {
// Callback to notify the user that the connection was successful
this->connect_trigger_->trigger();
this->connect_trigger_.trigger();
} else {
// Callback to notify the user that the connection failed
this->error_trigger_->trigger();
this->error_trigger_.trigger();
}
}
}
@@ -110,8 +110,8 @@ template<typename... Ts> class WiFiConfigureAction : public Action<Ts...>, publi
bool connecting_{false};
WiFiAP new_sta_;
WiFiAP old_sta_;
Trigger<> *connect_trigger_{new Trigger<>()};
Trigger<> *error_trigger_{new Trigger<>()};
Trigger<> connect_trigger_;
Trigger<> error_trigger_;
};
} // namespace esphome::wifi

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 {
this->connect_trigger_->trigger();
this->disconnect_trigger_.trigger();
}
#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() { return &this->connect_trigger_; }
#endif
#ifdef USE_WIFI_DISCONNECT_TRIGGER
Trigger<> *get_disconnect_trigger() { 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};
@@ -733,9 +739,12 @@ class WiFiComponent : public Component {
SemaphoreHandle_t high_performance_semaphore_{nullptr};
#endif
// Pointers at the end (naturally aligned)
Trigger<> *connect_trigger_{new Trigger<>()};
Trigger<> *disconnect_trigger_{new Trigger<>()};
#ifdef USE_WIFI_CONNECT_TRIGGER
Trigger<> connect_trigger_;
#endif
#ifdef USE_WIFI_DISCONNECT_TRIGGER
Trigger<> disconnect_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!"