ESPHome 2026.1.4
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_WIFI
5#include "wifi_component.h"
6
7namespace esphome::wifi {
8
9template<typename... Ts> class WiFiConnectedCondition : public Condition<Ts...> {
10 public:
11 bool check(const Ts &...x) override { return global_wifi_component->is_connected(); }
12};
13
14template<typename... Ts> class WiFiEnabledCondition : public Condition<Ts...> {
15 public:
16 bool check(const Ts &...x) override { return !global_wifi_component->is_disabled(); }
17};
18
19template<typename... Ts> class WiFiAPActiveCondition : public Condition<Ts...> {
20 public:
21 bool check(const Ts &...x) override { return global_wifi_component->is_ap_active(); }
22};
23
24template<typename... Ts> class WiFiEnableAction : public Action<Ts...> {
25 public:
26 void play(const Ts &...x) override { global_wifi_component->enable(); }
27};
28
29template<typename... Ts> class WiFiDisableAction : public Action<Ts...> {
30 public:
31 void play(const Ts &...x) override { global_wifi_component->disable(); }
32};
33
34template<typename... Ts> class WiFiConfigureAction : public Action<Ts...>, public Component {
35 public:
36 TEMPLATABLE_VALUE(std::string, ssid)
37 TEMPLATABLE_VALUE(std::string, password)
38 TEMPLATABLE_VALUE(bool, save)
39 TEMPLATABLE_VALUE(uint32_t, connection_timeout)
40
41 void play(const Ts &...x) override {
42 auto ssid = this->ssid_.value(x...);
43 auto password = this->password_.value(x...);
44 // Avoid multiple calls
45 if (this->connecting_)
46 return;
47 // If already connected to the same AP, do nothing
48 char ssid_buf[SSID_BUFFER_SIZE];
49 if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), ssid.c_str()) == 0) {
50 // Callback to notify the user that the connection was successful
52 return;
53 }
54 // Create a new WiFiAP object with the new SSID and password
55 this->new_sta_.set_ssid(ssid);
56 this->new_sta_.set_password(password);
57 // Save the current STA
59 // Disable WiFi
61 // Set the state to connecting
62 this->connecting_ = true;
63 // Store the new STA so once the WiFi is enabled, it will connect to it
64 // This is necessary because the WiFiComponent will raise an error and fallback to the saved STA
65 // if trying to connect to a new STA while already connected to another one
66 if (this->save_.value(x...)) {
67 global_wifi_component->save_wifi_sta(new_sta_.get_ssid(), new_sta_.get_password());
68 } else {
70 }
71 // Enable WiFi
73 // Set timeout for the connection
74 this->set_timeout("wifi-connect-timeout", this->connection_timeout_.value(x...), [this, x...]() {
75 // If the timeout is reached, stop connecting and revert to the old AP
76 global_wifi_component->disable();
77 global_wifi_component->save_wifi_sta(old_sta_.get_ssid(), old_sta_.get_password());
78 global_wifi_component->enable();
79 // Start a timeout for the fallback if the connection to the old AP fails
80 this->set_timeout("wifi-fallback-timeout", this->connection_timeout_.value(x...), [this]() {
81 this->connecting_ = false;
82 this->error_trigger_->trigger();
83 });
84 });
85 }
86
87 Trigger<> *get_connect_trigger() const { return this->connect_trigger_; }
88 Trigger<> *get_error_trigger() const { return this->error_trigger_; }
89
90 void loop() override {
91 if (!this->connecting_)
92 return;
94 // The WiFi is connected, stop the timeout and reset the connecting flag
95 this->cancel_timeout("wifi-connect-timeout");
96 this->cancel_timeout("wifi-fallback-timeout");
97 this->connecting_ = false;
98 char ssid_buf[SSID_BUFFER_SIZE];
99 if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), this->new_sta_.get_ssid().c_str()) == 0) {
100 // Callback to notify the user that the connection was successful
101 this->connect_trigger_->trigger();
102 } else {
103 // Callback to notify the user that the connection failed
104 this->error_trigger_->trigger();
105 }
106 }
107 }
108
109 protected:
110 bool connecting_{false};
113 Trigger<> *connect_trigger_{new Trigger<>()};
114 Trigger<> *error_trigger_{new Trigger<>()};
115};
116
117} // namespace esphome::wifi
118#endif
virtual void play(const Ts &...x)=0
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:445
Base class for all automation conditions.
Definition automation.h:217
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:238
bool check(const Ts &...x) override
Definition automation.h:21
void set_sta(const WiFiAP &ap)
void save_wifi_sta(const std::string &ssid, const std::string &password)
const char * wifi_ssid_to(std::span< char, SSID_BUFFER_SIZE > buffer)
Write SSID to buffer without heap allocation.
TEMPLATABLE_VALUE(std::string, ssid) TEMPLATABLE_VALUE(std WiFiAP new_sta_
Definition automation.h:36
bool check(const Ts &...x) override
Definition automation.h:11
void play(const Ts &...x) override
Definition automation.h:31
void play(const Ts &...x) override
Definition automation.h:26
bool check(const Ts &...x) override
Definition automation.h:16
void loop()
WiFiComponent * global_wifi_component
uint16_t x
Definition tt21100.cpp:5