ESPHome 2026.11.0b6
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 WiFiConfigureAction final : public Action<Ts...>, public Component {
10 public:
11 TEMPLATABLE_VALUE(std::string, ssid)
12 TEMPLATABLE_VALUE(std::string, password)
13 TEMPLATABLE_VALUE(bool, save)
14 TEMPLATABLE_VALUE(uint32_t, connection_timeout)
15
16 void play(const Ts &...x) override {
17 auto ssid = this->ssid_.value(x...);
18 auto password = this->password_.value(x...);
19 // Avoid multiple calls
20 if (this->connecting_)
21 return;
22 // If already connected to the same AP, do nothing
23 char ssid_buf[SSID_BUFFER_SIZE];
24 if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), ssid.c_str()) == 0) {
25 // Callback to notify the user that the connection was successful
27 return;
28 }
29 // Create a new WiFiAP object with the new SSID and password
30 this->new_sta_.set_ssid(ssid);
31 this->new_sta_.set_password(password);
32 // Save the current STA
34 // Disable WiFi
36 // Set the state to connecting
37 this->connecting_ = true;
38 // Store the new STA so once the WiFi is enabled, it will connect to it
39 // This is necessary because the WiFiComponent will raise an error and fallback to the saved STA
40 // if trying to connect to a new STA while already connected to another one
41 if (this->save_.value(x...)) {
43 } else {
45 }
46 // Enable WiFi
48 // Set timeout for the connection
49 this->set_timeout(CONNECT_TIMEOUT_ID, this->connection_timeout_.value(x...), [this, x...]() {
50 // If the timeout is reached, stop connecting and revert to the old AP
51 global_wifi_component->disable();
52 global_wifi_component->save_wifi_sta(old_sta_.get_ssid(), old_sta_.get_password());
53 global_wifi_component->enable();
54 // Start a timeout for the fallback if the connection to the old AP fails
55 this->set_timeout(FALLBACK_TIMEOUT_ID, this->connection_timeout_.value(x...), [this]() {
56 this->connecting_ = false;
57 this->error_trigger_.trigger();
58 });
59 });
60 }
61
62 Trigger<> *get_connect_trigger() { return &this->connect_trigger_; }
63 Trigger<> *get_error_trigger() { return &this->error_trigger_; }
64
65 void loop() override {
66 if (!this->connecting_)
67 return;
69 // The WiFi is connected, stop the timeout and reset the connecting flag
70 this->cancel_timeout(CONNECT_TIMEOUT_ID);
71 this->cancel_timeout(FALLBACK_TIMEOUT_ID);
72 this->connecting_ = false;
73 char ssid_buf[SSID_BUFFER_SIZE];
74 if (strcmp(global_wifi_component->wifi_ssid_to(ssid_buf), this->new_sta_.get_ssid().c_str()) == 0) {
75 // Callback to notify the user that the connection was successful
76 this->connect_trigger_.trigger();
77 } else {
78 // Callback to notify the user that the connection failed
79 this->error_trigger_.trigger();
80 }
81 }
82 }
83
84 protected:
85 static constexpr uint32_t CONNECT_TIMEOUT_ID = 0;
86 static constexpr uint32_t FALLBACK_TIMEOUT_ID = 1;
87
88 bool connecting_{false};
93};
94
95} // namespace esphome::wifi
96#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
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:557
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.
TEMPLATABLE_VALUE(std::string, ssid) TEMPLATABLE_VALUE(std
Definition automation.h:11
static constexpr uint32_t CONNECT_TIMEOUT_ID
Definition automation.h:85
WiFiComponent * global_wifi_component
static void uint32_t
uint16_t x
Definition tt21100.cpp:5