ESPHome 2025.12.1
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 if (global_wifi_component->wifi_ssid() == ssid) {
49 // Callback to notify the user that the connection was successful
51 return;
52 }
53 // Create a new WiFiAP object with the new SSID and password
54 this->new_sta_.set_ssid(ssid);
55 this->new_sta_.set_password(password);
56 // Save the current STA
58 // Disable WiFi
60 // Set the state to connecting
61 this->connecting_ = true;
62 // Store the new STA so once the WiFi is enabled, it will connect to it
63 // This is necessary because the WiFiComponent will raise an error and fallback to the saved STA
64 // if trying to connect to a new STA while already connected to another one
65 if (this->save_.value(x...)) {
66 global_wifi_component->save_wifi_sta(new_sta_.get_ssid(), new_sta_.get_password());
67 } else {
69 }
70 // Enable WiFi
72 // Set timeout for the connection
73 this->set_timeout("wifi-connect-timeout", this->connection_timeout_.value(x...), [this, x...]() {
74 // If the timeout is reached, stop connecting and revert to the old AP
75 global_wifi_component->disable();
76 global_wifi_component->save_wifi_sta(old_sta_.get_ssid(), old_sta_.get_password());
77 global_wifi_component->enable();
78 // Start a timeout for the fallback if the connection to the old AP fails
79 this->set_timeout("wifi-fallback-timeout", this->connection_timeout_.value(x...), [this]() {
80 this->connecting_ = false;
81 this->error_trigger_->trigger();
82 });
83 });
84 }
85
86 Trigger<> *get_connect_trigger() const { return this->connect_trigger_; }
87 Trigger<> *get_error_trigger() const { return this->error_trigger_; }
88
89 void loop() override {
90 if (!this->connecting_)
91 return;
93 // The WiFi is connected, stop the timeout and reset the connecting flag
94 this->cancel_timeout("wifi-connect-timeout");
95 this->cancel_timeout("wifi-fallback-timeout");
96 this->connecting_ = false;
97 if (global_wifi_component->wifi_ssid() == this->new_sta_.get_ssid()) {
98 // Callback to notify the user that the connection was successful
99 this->connect_trigger_->trigger();
100 } else {
101 // Callback to notify the user that the connection failed
102 this->error_trigger_->trigger();
103 }
104 }
105 }
106
107 protected:
108 bool connecting_{false};
111 Trigger<> *connect_trigger_{new Trigger<>()};
112 Trigger<> *error_trigger_{new Trigger<>()};
113};
114
115} // namespace esphome::wifi
116#endif
virtual void play(const Ts &...x)=0
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Base class for all automation conditions.
Definition automation.h:183
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:204
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)
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