mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[wifi] Conditionally compile on_connect/on_disconnect triggers (#13684)
This commit is contained in:
@@ -585,11 +585,13 @@ async def to_code(config):
|
|||||||
await cg.past_safe_mode()
|
await cg.past_safe_mode()
|
||||||
|
|
||||||
if on_connect_config := config.get(CONF_ON_CONNECT):
|
if on_connect_config := config.get(CONF_ON_CONNECT):
|
||||||
|
cg.add_define("USE_WIFI_CONNECT_TRIGGER")
|
||||||
await automation.build_automation(
|
await automation.build_automation(
|
||||||
var.get_connect_trigger(), [], on_connect_config
|
var.get_connect_trigger(), [], on_connect_config
|
||||||
)
|
)
|
||||||
|
|
||||||
if on_disconnect_config := config.get(CONF_ON_DISCONNECT):
|
if on_disconnect_config := config.get(CONF_ON_DISCONNECT):
|
||||||
|
cg.add_define("USE_WIFI_DISCONNECT_TRIGGER")
|
||||||
await automation.build_automation(
|
await automation.build_automation(
|
||||||
var.get_disconnect_trigger(), [], on_disconnect_config
|
var.get_disconnect_trigger(), [], on_disconnect_config
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ template<typename... Ts> class WiFiConfigureAction : public Action<Ts...>, publi
|
|||||||
char ssid_buf[SSID_BUFFER_SIZE];
|
char ssid_buf[SSID_BUFFER_SIZE];
|
||||||
if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), ssid.c_str()) == 0) {
|
if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), ssid.c_str()) == 0) {
|
||||||
// Callback to notify the user that the connection was successful
|
// Callback to notify the user that the connection was successful
|
||||||
this->connect_trigger_->trigger();
|
this->connect_trigger_.trigger();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Create a new WiFiAP object with the new SSID and password
|
// 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
|
// 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->set_timeout("wifi-fallback-timeout", this->connection_timeout_.value(x...), [this]() {
|
||||||
this->connecting_ = false;
|
this->connecting_ = false;
|
||||||
this->error_trigger_->trigger();
|
this->error_trigger_.trigger();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Trigger<> *get_connect_trigger() const { return this->connect_trigger_; }
|
Trigger<> *get_connect_trigger() { return &this->connect_trigger_; }
|
||||||
Trigger<> *get_error_trigger() const { return this->error_trigger_; }
|
Trigger<> *get_error_trigger() { return &this->error_trigger_; }
|
||||||
|
|
||||||
void loop() override {
|
void loop() override {
|
||||||
if (!this->connecting_)
|
if (!this->connecting_)
|
||||||
@@ -98,10 +98,10 @@ template<typename... Ts> class WiFiConfigureAction : public Action<Ts...>, publi
|
|||||||
char ssid_buf[SSID_BUFFER_SIZE];
|
char ssid_buf[SSID_BUFFER_SIZE];
|
||||||
if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), this->new_sta_.get_ssid().c_str()) == 0) {
|
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
|
// Callback to notify the user that the connection was successful
|
||||||
this->connect_trigger_->trigger();
|
this->connect_trigger_.trigger();
|
||||||
} else {
|
} else {
|
||||||
// Callback to notify the user that the connection failed
|
// 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};
|
bool connecting_{false};
|
||||||
WiFiAP new_sta_;
|
WiFiAP new_sta_;
|
||||||
WiFiAP old_sta_;
|
WiFiAP old_sta_;
|
||||||
Trigger<> *connect_trigger_{new Trigger<>()};
|
Trigger<> connect_trigger_;
|
||||||
Trigger<> *error_trigger_{new Trigger<>()};
|
Trigger<> error_trigger_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace esphome::wifi
|
} // namespace esphome::wifi
|
||||||
|
|||||||
@@ -651,14 +651,21 @@ void WiFiComponent::loop() {
|
|||||||
const uint32_t now = App.get_loop_component_start_time();
|
const uint32_t now = App.get_loop_component_start_time();
|
||||||
|
|
||||||
if (this->has_sta()) {
|
if (this->has_sta()) {
|
||||||
|
#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
|
||||||
if (this->is_connected() != this->handled_connected_state_) {
|
if (this->is_connected() != this->handled_connected_state_) {
|
||||||
|
#ifdef USE_WIFI_DISCONNECT_TRIGGER
|
||||||
if (this->handled_connected_state_) {
|
if (this->handled_connected_state_) {
|
||||||
this->disconnect_trigger_->trigger();
|
this->disconnect_trigger_.trigger();
|
||||||
} else {
|
|
||||||
this->connect_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();
|
this->handled_connected_state_ = this->is_connected();
|
||||||
}
|
}
|
||||||
|
#endif // USE_WIFI_CONNECT_TRIGGER || USE_WIFI_DISCONNECT_TRIGGER
|
||||||
|
|
||||||
switch (this->state_) {
|
switch (this->state_) {
|
||||||
case WIFI_COMPONENT_STATE_COOLDOWN: {
|
case WIFI_COMPONENT_STATE_COOLDOWN: {
|
||||||
|
|||||||
@@ -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_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; }
|
void set_post_connect_roaming(bool enabled) { this->post_connect_roaming_ = enabled; }
|
||||||
|
|
||||||
Trigger<> *get_connect_trigger() const { return this->connect_trigger_; };
|
#ifdef USE_WIFI_CONNECT_TRIGGER
|
||||||
Trigger<> *get_disconnect_trigger() const { return this->disconnect_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();
|
int32_t get_wifi_channel();
|
||||||
|
|
||||||
@@ -706,7 +710,9 @@ class WiFiComponent : public Component {
|
|||||||
|
|
||||||
// Group all boolean values together
|
// Group all boolean values together
|
||||||
bool has_ap_{false};
|
bool has_ap_{false};
|
||||||
|
#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
|
||||||
bool handled_connected_state_{false};
|
bool handled_connected_state_{false};
|
||||||
|
#endif
|
||||||
bool error_from_callback_{false};
|
bool error_from_callback_{false};
|
||||||
bool scan_done_{false};
|
bool scan_done_{false};
|
||||||
bool ap_setup_{false};
|
bool ap_setup_{false};
|
||||||
@@ -733,9 +739,12 @@ class WiFiComponent : public Component {
|
|||||||
SemaphoreHandle_t high_performance_semaphore_{nullptr};
|
SemaphoreHandle_t high_performance_semaphore_{nullptr};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Pointers at the end (naturally aligned)
|
#ifdef USE_WIFI_CONNECT_TRIGGER
|
||||||
Trigger<> *connect_trigger_{new Trigger<>()};
|
Trigger<> connect_trigger_;
|
||||||
Trigger<> *disconnect_trigger_{new Trigger<>()};
|
#endif
|
||||||
|
#ifdef USE_WIFI_DISCONNECT_TRIGGER
|
||||||
|
Trigger<> disconnect_trigger_;
|
||||||
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Stores a pointer to a string literal (static storage duration).
|
// Stores a pointer to a string literal (static storage duration).
|
||||||
|
|||||||
@@ -227,6 +227,8 @@
|
|||||||
#define USE_WIFI_SCAN_RESULTS_LISTENERS
|
#define USE_WIFI_SCAN_RESULTS_LISTENERS
|
||||||
#define USE_WIFI_CONNECT_STATE_LISTENERS
|
#define USE_WIFI_CONNECT_STATE_LISTENERS
|
||||||
#define USE_WIFI_POWER_SAVE_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_IP_STATE_LISTENERS 2
|
||||||
#define ESPHOME_WIFI_SCAN_RESULTS_LISTENERS 2
|
#define ESPHOME_WIFI_SCAN_RESULTS_LISTENERS 2
|
||||||
#define ESPHOME_WIFI_CONNECT_STATE_LISTENERS 2
|
#define ESPHOME_WIFI_CONNECT_STATE_LISTENERS 2
|
||||||
|
|||||||
@@ -26,3 +26,7 @@ wifi:
|
|||||||
- ssid: MySSID3
|
- ssid: MySSID3
|
||||||
password: password3
|
password: password3
|
||||||
priority: 0
|
priority: 0
|
||||||
|
on_connect:
|
||||||
|
- logger.log: "WiFi connected!"
|
||||||
|
on_disconnect:
|
||||||
|
- logger.log: "WiFi disconnected!"
|
||||||
|
|||||||
Reference in New Issue
Block a user