ESPHome 2026.11.0-dev
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 final : 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 final : 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 final : 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 final : public Action<Ts...> {
25 public:
26 void play(const Ts &...x) override { global_wifi_component->enable(); }
27};
28
29template<typename... Ts> class WiFiDisableAction final : public Action<Ts...> {
30 public:
31 void play(const Ts &...x) override { global_wifi_component->disable(); }
32};
33
34template<typename... Ts> class WiFiRoamAction final : public Action<Ts...> {
35 public:
36 void play(const Ts &...x) override { global_wifi_component->force_roam_check(); }
37};
38
39template<typename... Ts> class WiFiConfigureAction final : public Action<Ts...>, public Component {
40 public:
41 TEMPLATABLE_VALUE(std::string, ssid)
42 TEMPLATABLE_VALUE(std::string, password)
43 TEMPLATABLE_VALUE(bool, save)
44 TEMPLATABLE_VALUE(uint32_t, connection_timeout)
45
46 void play(const Ts &...x) override {
47 auto ssid = this->ssid_.value(x...);
48 auto password = this->password_.value(x...);
49 // Avoid multiple calls
50 if (this->connecting_)
51 return;
52 // If already connected to the same AP, do nothing
53 char ssid_buf[SSID_BUFFER_SIZE];
54 if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), ssid.c_str()) == 0) {
55 // Callback to notify the user that the connection was successful
57 return;
58 }
59 // Create a new WiFiAP object with the new SSID and password
60 this->new_sta_.set_ssid(ssid);
61 this->new_sta_.set_password(password);
62 // Save the current STA
64 // Disable WiFi
66 // Set the state to connecting
67 this->connecting_ = true;
68 // Store the new STA so once the WiFi is enabled, it will connect to it
69 // This is necessary because the WiFiComponent will raise an error and fallback to the saved STA
70 // if trying to connect to a new STA while already connected to another one
71 if (this->save_.value(x...)) {
73 } else {
75 }
76 // Enable WiFi
78 // Set timeout for the connection
79 this->set_timeout(CONNECT_TIMEOUT_ID, this->connection_timeout_.value(x...), [this, x...]() {
80 // If the timeout is reached, stop connecting and revert to the old AP
81 global_wifi_component->disable();
82 global_wifi_component->save_wifi_sta(old_sta_.get_ssid(), old_sta_.get_password());
83 global_wifi_component->enable();
84 // Start a timeout for the fallback if the connection to the old AP fails
85 this->set_timeout(FALLBACK_TIMEOUT_ID, this->connection_timeout_.value(x...), [this]() {
86 this->connecting_ = false;
87 this->error_trigger_.trigger();
88 });
89 });
90 }
91
92 Trigger<> *get_connect_trigger() { return &this->connect_trigger_; }
93 Trigger<> *get_error_trigger() { return &this->error_trigger_; }
94
95 void loop() override {
96 if (!this->connecting_)
97 return;
99 // The WiFi is connected, stop the timeout and reset the connecting flag
100 this->cancel_timeout(CONNECT_TIMEOUT_ID);
101 this->cancel_timeout(FALLBACK_TIMEOUT_ID);
102 this->connecting_ = false;
103 char ssid_buf[SSID_BUFFER_SIZE];
104 if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), this->new_sta_.get_ssid().c_str()) == 0) {
105 // Callback to notify the user that the connection was successful
106 this->connect_trigger_.trigger();
107 } else {
108 // Callback to notify the user that the connection failed
109 this->error_trigger_.trigger();
110 }
111 }
112 }
113
114 protected:
115 static constexpr uint32_t CONNECT_TIMEOUT_ID = 0;
116 static constexpr uint32_t FALLBACK_TIMEOUT_ID = 1;
117
118 bool connecting_{false};
123};
124
125} // namespace esphome::wifi
126#endif
virtual void play(const Ts &...x)=0
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
Base class for all automation conditions.
Definition automation.h:510
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:533
bool check(const Ts &...x) override
Definition automation.h:21
StringRef get_ssid() const
void set_ssid(const std::string &ssid)
StringRef get_password() const
void set_password(const std::string &password)
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.
void force_roam_check()
Force an immediate post-connect roaming check, bypassing the periodic interval and the per-connection...
TEMPLATABLE_VALUE(std::string, ssid) TEMPLATABLE_VALUE(std
Definition automation.h:41
static constexpr uint32_t CONNECT_TIMEOUT_ID
Definition automation.h:115
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 play(const Ts &...x) override
Definition automation.h:36
WiFiComponent * global_wifi_component
static void uint32_t
uint16_t x
Definition tt21100.cpp:5