ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
wifi_component.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_WIFI
9#ifdef USE_ESP32
11#endif
13
14#include <span>
15#include <string>
16#include <type_traits>
17#include <vector>
18
19#ifdef USE_LIBRETINY
20#include <WiFi.h>
21#endif
22
23#if defined(USE_ESP32) && defined(USE_WIFI_WPA2_EAP)
24#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
25#include <esp_eap_client.h>
26#else
27#include <esp_wpa2.h>
28#endif
29#endif
30
31#ifdef USE_ESP8266
32#include <ESP8266WiFi.h>
33#include <ESP8266WiFiType.h>
34
35#if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0)
36extern "C" {
37#include <user_interface.h>
38};
39#endif
40#endif
41
42#ifdef USE_RP2040
43extern "C" {
44#include "cyw43.h"
45#include "cyw43_country.h"
46#include "pico/cyw43_arch.h"
47}
48
49#include <WiFi.h>
50#endif
51
52#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
53#include <esp_wifi_types.h>
54#endif
55
56#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
57#include <freertos/FreeRTOS.h>
58#include <freertos/semphr.h>
59#endif
60
61namespace esphome::wifi {
62
64static constexpr int8_t WIFI_RSSI_DISCONNECTED = -127;
65
67static constexpr size_t SSID_BUFFER_SIZE = 33;
68
70 char ssid[33];
71 char password[65];
72} PACKED; // NOLINT
73
75 uint8_t bssid[6];
76 uint8_t channel;
77 int8_t ap_index;
78} PACKED; // NOLINT
79
96
104
106enum class WiFiRetryPhase : uint8_t {
109#ifdef USE_WIFI_FAST_CONNECT
112#endif
121};
122
124enum class RoamingState : uint8_t {
126 IDLE,
128 SCANNING,
133};
134
136enum class RetryHiddenMode : uint8_t {
143};
144
153
154#ifdef USE_WIFI_WPA2_EAP
155struct EAPAuth {
156 std::string identity; // required for all auth types
157 std::string username;
158 std::string password;
159 const char *ca_cert; // optionally verify authentication server
160 // used for EAP-TLS
161 const char *client_cert;
162 const char *client_key;
163// used for EAP-TTLS
164#ifdef USE_ESP32
165 esp_eap_ttls_phase2_types ttls_phase_2;
166#endif
167};
168#endif // USE_WIFI_WPA2_EAP
169
170using bssid_t = std::array<uint8_t, 6>;
171
173static constexpr size_t WIFI_SCAN_RESULT_FILTERED_RESERVE = 8;
174
175// Use std::vector for RP2040 (callback-based) and ESP32 (destructive scan API)
176// Use FixedVector for ESP8266 and LibreTiny where two-pass exact allocation is possible
177#if defined(USE_RP2040) || defined(USE_ESP32)
178template<typename T> using wifi_scan_vector_t = std::vector<T>;
179#else
180template<typename T> using wifi_scan_vector_t = FixedVector<T>;
181#endif
182
186 public:
187 static constexpr uint8_t MAX_LENGTH = 127;
188 static constexpr uint8_t INLINE_CAPACITY = 18; // 18 chars + null terminator fits in 19 bytes
189
190 CompactString() : length_(0), is_heap_(0) { this->storage_[0] = '\0'; }
191 CompactString(const char *str, size_t len);
192 CompactString(const CompactString &other);
193 CompactString(CompactString &&other) noexcept;
195 CompactString &operator=(CompactString &&other) noexcept;
197
198 const char *data() const { return this->is_heap_ ? this->get_heap_ptr_() : this->storage_; }
199 const char *c_str() const { return this->data(); } // Always null-terminated
200 size_t size() const { return this->length_; }
201 bool empty() const { return this->length_ == 0; }
202
204 StringRef ref() const { return StringRef(this->data(), this->size()); }
205
206 bool operator==(const CompactString &other) const;
207 bool operator!=(const CompactString &other) const { return !(*this == other); }
208 bool operator==(const StringRef &other) const;
209 bool operator!=(const StringRef &other) const { return !(*this == other); }
210 bool operator==(const char *other) const { return *this == StringRef(other); }
211 bool operator!=(const char *other) const { return !(*this == other); }
212
213 protected:
214 char *get_heap_ptr_() const {
215 char *ptr;
216 std::memcpy(&ptr, this->storage_, sizeof(ptr));
217 return ptr;
218 }
219 void set_heap_ptr_(char *ptr) { std::memcpy(this->storage_, &ptr, sizeof(ptr)); }
220
221 // Storage for string data. When is_heap_=0, contains the string directly (null-terminated).
222 // When is_heap_=1, first sizeof(char*) bytes contain pointer to heap allocation.
223 char storage_[INLINE_CAPACITY + 1]; // 19 bytes: 18 chars + null terminator
224 uint8_t length_ : 7; // String length (0-127)
225 uint8_t is_heap_ : 1; // 1 if using heap pointer, 0 if using inline storage
226 // Total size: 20 bytes (19 bytes storage + 1 byte bitfields)
227};
228
229static_assert(sizeof(CompactString) == 20, "CompactString must be exactly 20 bytes");
230// CompactString is not trivially copyable (non-trivial destructor/copy for heap case).
231// However, its layout has no self-referential pointers: storage_[] contains either inline
232// data or an external heap pointer — never a pointer to itself. This is unlike libstdc++
233// std::string SSO where _M_p points to _M_local_buf within the same object.
234// This property allows memcpy-based permutation sorting where each element ends up in
235// exactly one slot (no ownership duplication). These asserts document that layout property.
236static_assert(std::is_standard_layout<CompactString>::value, "CompactString must be standard layout");
237static_assert(!std::is_polymorphic<CompactString>::value, "CompactString must not have vtable");
238
239class WiFiAP {
240 friend class WiFiComponent;
241 friend class WiFiScanResult;
242
243 public:
244 void set_ssid(const std::string &ssid);
245 void set_ssid(const char *ssid);
246 void set_ssid(StringRef ssid) { this->ssid_ = CompactString(ssid.c_str(), ssid.size()); }
247 void set_bssid(const bssid_t &bssid);
248 void clear_bssid();
249 void set_password(const std::string &password);
250 void set_password(const char *password);
251 void set_password(StringRef password) { this->password_ = CompactString(password.c_str(), password.size()); }
252#ifdef USE_WIFI_WPA2_EAP
253 void set_eap(optional<EAPAuth> eap_auth);
254#endif // USE_WIFI_WPA2_EAP
255 void set_channel(uint8_t channel);
256 void clear_channel();
258#ifdef USE_WIFI_MANUAL_IP
259 void set_manual_ip(optional<ManualIP> manual_ip);
260#endif
261 void set_hidden(bool hidden);
262 StringRef get_ssid() const { return this->ssid_.ref(); }
263 StringRef get_password() const { return this->password_.ref(); }
264 const bssid_t &get_bssid() const;
265 bool has_bssid() const;
266#ifdef USE_WIFI_WPA2_EAP
267 const optional<EAPAuth> &get_eap() const;
268#endif // USE_WIFI_WPA2_EAP
269 uint8_t get_channel() const { return this->channel_; }
270 bool has_channel() const { return this->channel_ != 0; }
271 int8_t get_priority() const { return priority_; }
272#ifdef USE_WIFI_MANUAL_IP
273 const optional<ManualIP> &get_manual_ip() const;
274#endif
275 bool get_hidden() const;
276
277 protected:
280#ifdef USE_WIFI_WPA2_EAP
281 optional<EAPAuth> eap_;
282#endif // USE_WIFI_WPA2_EAP
283#ifdef USE_WIFI_MANUAL_IP
284 optional<ManualIP> manual_ip_;
285#endif
286 // Group small types together to minimize padding
287 bssid_t bssid_{}; // 6 bytes, all zeros = any/not set
288 uint8_t channel_{0}; // 1 byte, 0 = auto/not set
289 int8_t priority_{0}; // 1 byte
290 bool hidden_{false}; // 1 byte (+ 3 bytes end padding to 4-byte align)
291};
292
294 friend class WiFiComponent;
295
296 public:
297 WiFiScanResult(const bssid_t &bssid, const char *ssid, size_t ssid_len, uint8_t channel, int8_t rssi, bool with_auth,
298 bool is_hidden);
299
300 bool matches(const WiFiAP &config) const;
301
302 bool get_matches() const;
303 void set_matches(bool matches);
304 const bssid_t &get_bssid() const;
305 StringRef get_ssid() const { return this->ssid_.ref(); }
306 uint8_t get_channel() const;
307 int8_t get_rssi() const;
308 bool get_with_auth() const;
309 bool get_is_hidden() const;
310 int8_t get_priority() const { return priority_; }
312
313 bool operator==(const WiFiScanResult &rhs) const;
314
315 protected:
317 uint8_t channel_;
318 int8_t rssi_;
320 int8_t priority_{0};
321 bool matches_{false};
324};
325
330
336
342
343#ifdef USE_ESP32
344struct IDFWiFiEvent;
345#endif
346
347#ifdef USE_LIBRETINY
348struct LTWiFiEvent;
349#endif
350
360 public:
361 virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1,
362 const network::IPAddress &dns2) = 0;
363};
364
374 public:
376};
377
387 public:
388 virtual void on_wifi_connect_state(StringRef ssid, std::span<const uint8_t, 6> bssid) = 0;
389};
390
400 public:
402};
403
405class WiFiComponent final : public Component {
406 public:
409
410 void set_sta(const WiFiAP &ap);
411 // Returns a copy of the currently selected AP configuration
412 WiFiAP get_sta() const;
413 void init_sta(size_t count);
414 void add_sta(const WiFiAP &ap);
415 void clear_sta();
416
417#ifdef USE_WIFI_AP
425 void set_ap(const WiFiAP &ap);
426 WiFiAP get_ap() { return this->ap_; }
427 void set_ap_timeout(uint32_t ap_timeout) { ap_timeout_ = ap_timeout; }
428#endif // USE_WIFI_AP
429
430 void enable();
431 void disable();
432 bool is_disabled();
433 void start_scanning();
435 void start_connecting(const WiFiAP &ap);
436 // Backward compatibility overload - ignores 'two' parameter
437 void start_connecting(const WiFiAP &ap, bool /* two */) { this->start_connecting(ap); }
438
440
441 void retry_connect();
442
443 void set_reboot_timeout(uint32_t reboot_timeout);
444
445 bool is_connected() const { return this->connected_; }
446
447 void set_power_save_mode(WiFiPowerSaveMode power_save);
448 void set_min_auth_mode(WifiMinAuthMode min_auth_mode) { min_auth_mode_ = min_auth_mode; }
449 void set_output_power(float output_power) { output_power_ = output_power; }
450#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
451 void set_band_mode(wifi_band_mode_t band_mode) { this->band_mode_ = band_mode; }
452#endif
453
454 void set_passive_scan(bool passive);
455
456 void save_wifi_sta(const std::string &ssid, const std::string &password);
457 void save_wifi_sta(const char *ssid, const char *password);
458 void save_wifi_sta(StringRef ssid, StringRef password) { this->save_wifi_sta(ssid.c_str(), password.c_str()); }
459
460 // ========== INTERNAL METHODS ==========
461 // (In most use cases you won't need these)
463 void setup() override;
464 void start();
465 void dump_config() override;
466 void restart_adapter();
468 float get_setup_priority() const override;
470 void loop() override;
471
472 bool has_sta() const { return !this->sta_.empty(); }
473 bool has_ap() const { return this->has_ap_; }
474 bool is_ap_active() const { return this->ap_started_; }
475
476#ifdef USE_WIFI_11KV_SUPPORT
477 void set_btm(bool btm);
478 void set_rrm(bool rrm);
479#endif
480
483 const char *get_use_address() const { return this->use_address_; }
484 void set_use_address(const char *use_address) { this->use_address_ = use_address; }
485
487
489
490 bool has_sta_priority(const bssid_t &bssid) {
491 for (auto &it : this->sta_priorities_) {
492 if (it.bssid == bssid)
493 return true;
494 }
495 return false;
496 }
497 int8_t get_sta_priority(const bssid_t bssid) {
498 for (auto &it : this->sta_priorities_) {
499 if (it.bssid == bssid)
500 return it.priority;
501 }
502 return 0;
503 }
504 void set_sta_priority(bssid_t bssid, int8_t priority);
505
507 // Remove before 2026.9.0
508 ESPDEPRECATED("Use wifi_ssid_to() instead. Removed in 2026.9.0", "2026.3.0")
509 std::string wifi_ssid();
512 const char *wifi_ssid_to(std::span<char, SSID_BUFFER_SIZE> buffer);
514
515 int8_t wifi_rssi();
516
517 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
518 void set_keep_scan_results(bool keep_scan_results) { this->keep_scan_results_ = keep_scan_results; }
519 void set_post_connect_roaming(bool enabled) { this->post_connect_roaming_ = enabled; }
520
521#ifdef USE_WIFI_CONNECT_TRIGGER
523#endif
524#ifdef USE_WIFI_DISCONNECT_TRIGGER
526#endif
527
528 int32_t get_wifi_channel();
529
530#ifdef USE_WIFI_IP_STATE_LISTENERS
534 void add_ip_state_listener(WiFiIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
535#endif // USE_WIFI_IP_STATE_LISTENERS
536#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
539 this->scan_results_listeners_.push_back(listener);
540 }
541#endif // USE_WIFI_SCAN_RESULTS_LISTENERS
542#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
547 this->connect_state_listeners_.push_back(listener);
548 }
549#endif // USE_WIFI_CONNECT_STATE_LISTENERS
550#ifdef USE_WIFI_POWER_SAVE_LISTENERS
554 void add_power_save_listener(WiFiPowerSaveListener *listener) { this->power_save_listeners_.push_back(listener); }
555#endif // USE_WIFI_POWER_SAVE_LISTENERS
556
557#ifdef USE_WIFI_RUNTIME_POWER_SAVE
573
586#endif // USE_WIFI_RUNTIME_POWER_SAVE
587
588 protected:
589#ifdef USE_WIFI_AP
590 void setup_ap_config_();
591#endif // USE_WIFI_AP
592
595
600 bool transition_to_phase_(WiFiRetryPhase new_phase);
603 bool needs_scan_results_() const;
609 int8_t find_first_non_hidden_index_() const;
612 bool ssid_was_seen_in_scan_(const CompactString &ssid) const;
614 bool needs_full_scan_results_() const;
617 bool matches_configured_network_(const char *ssid, const uint8_t *bssid) const;
619 void log_discarded_scan_result_(const char *ssid, const uint8_t *bssid, int8_t rssi, uint8_t channel);
623 int8_t find_next_hidden_sta_(int8_t start_index);
635 const WiFiAP *get_selected_sta_() const {
636 if (this->selected_sta_index_ >= 0 && static_cast<size_t>(this->selected_sta_index_) < this->sta_.size()) {
637 return &this->sta_[this->selected_sta_index_];
638 }
639 return nullptr;
640 }
641
643 if (this->selected_sta_index_ < 0 || static_cast<size_t>(this->selected_sta_index_) >= this->sta_.size()) {
644 this->selected_sta_index_ = this->sta_.empty() ? -1 : 0;
645 }
646 }
647
648 bool all_networks_hidden_() const {
649 if (this->sta_.empty())
650 return false;
651 for (const auto &ap : this->sta_) {
652 if (!ap.get_hidden())
653 return false;
654 }
655 return true;
656 }
657
658 void connect_soon_();
659
660 void wifi_loop_();
661#ifdef USE_ESP8266
663#endif
664 bool wifi_mode_(optional<bool> sta, optional<bool> ap);
665 bool wifi_sta_pre_setup_();
666 bool wifi_apply_output_power_(float output_power);
668#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
670#endif
671 bool wifi_sta_ip_config_(const optional<ManualIP> &manual_ip);
673 bool wifi_sta_connect_(const WiFiAP &ap);
674 void wifi_pre_setup_();
681 bool wifi_scan_start_(bool passive);
682
683#ifdef USE_WIFI_AP
684 bool wifi_ap_ip_config_(const optional<ManualIP> &manual_ip);
685 bool wifi_start_ap_(const WiFiAP &ap);
686#endif // USE_WIFI_AP
687
688 bool wifi_disconnect_();
689
693
696
697#ifdef USE_WIFI_FAST_CONNECT
700#endif
701
702 // Post-connect roaming methods
703 void check_roaming_(uint32_t now);
706
709
710#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
715#endif
716#ifdef USE_WIFI_IP_STATE_LISTENERS
719#endif
720#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
723#endif
724
725#ifdef USE_ESP8266
726 static void wifi_event_callback(System_Event_t *event);
727 void wifi_scan_done_callback_(void *arg, STATUS status);
728 static void s_wifi_scan_done_callback(void *arg, STATUS status);
729#endif
730
731#ifdef USE_ESP32
732 void wifi_process_event_(IDFWiFiEvent *data);
733 friend void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
734#endif
735
736#ifdef USE_RP2040
737 static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
738 void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result);
739#endif
740
741#ifdef USE_LIBRETINY
742 void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info);
743 void wifi_process_event_(LTWiFiEvent *event);
745#endif
746
747 // Large/pointer-aligned members first
749 std::vector<WiFiSTAPriority> sta_priorities_;
751#ifdef USE_WIFI_AP
753#endif
754#ifdef USE_WIFI_IP_STATE_LISTENERS
756#endif
757#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
759#endif
760#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
762#endif
763#ifdef USE_WIFI_POWER_SAVE_LISTENERS
765#endif
767#ifdef USE_WIFI_FAST_CONNECT
769#endif
770#ifdef USE_WIFI_CONNECT_TRIGGER
772#endif
773#ifdef USE_WIFI_DISCONNECT_TRIGGER
775#endif
776#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
777 SemaphoreHandle_t high_performance_semaphore_{nullptr};
778#endif
779
780 static constexpr uint8_t FIRST_5GHZ_CHANNEL = 36;
781
782 // Post-connect roaming constants
783 static constexpr uint32_t ROAMING_CHECK_INTERVAL = 5 * 60 * 1000; // 5 minutes
784 static constexpr int8_t ROAMING_MIN_IMPROVEMENT = 10; // dB
785 static constexpr int8_t ROAMING_GOOD_RSSI = -49; // Skip scan if signal is excellent
786 static constexpr uint8_t ROAMING_MAX_ATTEMPTS = 3;
787 // Grace period after roaming scan completes. If WiFi disconnects within this
788 // window (e.g., ESP8266 Beacon Timeout caused by going off-channel during scan),
789 // the disconnect is treated as roaming-related and the attempts counter is preserved.
790 static constexpr uint32_t ROAMING_SCAN_GRACE_PERIOD = 30 * 1000; // 30 seconds
791
792 // 4-byte members
793 float output_power_{NAN};
798 uint32_t roaming_scan_end_{0}; // Timestamp when last roaming scan completed
799#ifdef USE_WIFI_AP
801#endif
802
803 // 1-byte enums and integers
806#if defined(USE_ESP32) && defined(SOC_WIFI_SUPPORT_5G)
807 wifi_band_mode_t band_mode_{WIFI_BAND_MODE_AUTO};
808#endif
811 uint8_t num_retried_{0};
812 // Index into sta_ array for the currently selected AP configuration (-1 = none selected)
813 // Used to access password, manual_ip, priority, EAP settings, and hidden flag
814 // int8_t limits to 127 APs (enforced in __init__.py via MAX_WIFI_NETWORKS)
817#if USE_NETWORK_IPV6
819#endif /* USE_NETWORK_IPV6 */
821#if defined(USE_ESP8266) || defined(USE_LIBRETINY)
822 // Platform-specific STA state enum, defined in platform cpp file.
823 // On ESP8266, written from SDK system context (wifi_event_callback) —
824 // uint8_t writes are atomic on Xtensa LX106 so no synchronization is needed.
825 uint8_t sta_state_{0};
826#endif
829 bssid_t roaming_target_bssid_{}; // BSSID of the AP we're trying to roam to
830#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
832#endif
833
834 // Bools and bitfields
835 // Pending listener callbacks deferred from platform callbacks to main loop.
836 struct {
837#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
838 // Deferred until state machine reaches STA_CONNECTED so wifi.connected
839 // condition returns true in listener automations.
841#ifdef USE_ESP8266
842 // ESP8266: also defer disconnect notification to main loop
843 bool disconnect : 1;
844#endif
845#endif
846#if defined(USE_ESP8266) && defined(USE_WIFI_IP_STATE_LISTENERS)
847 bool got_ip : 1;
848#endif
849#if defined(USE_ESP8266) && defined(USE_WIFI_SCAN_RESULTS_LISTENERS)
851#endif
853 bool has_ap_{false};
854#if defined(USE_WIFI_CONNECT_TRIGGER) || defined(USE_WIFI_DISCONNECT_TRIGGER)
856#endif
857 bool scan_done_{false};
858 bool ap_setup_{false};
859 bool ap_started_{false};
860 bool passive_scan_{false};
862#ifdef USE_WIFI_11KV_SUPPORT
863 bool btm_{false};
864 bool rrm_{false};
865#endif
866 bool enable_on_boot_{true};
867 bool got_ipv4_address_{false};
870 false}; // Tracks if we've completed a scan after captive portal started
872 bool connected_{false};
873 bool post_connect_roaming_{true}; // Enabled by default
874#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
876#endif
877
878#ifdef USE_ESP32
879 // Lock-free SPSC queue for WiFi events from ESP-IDF event handler.
880 // 17 slots = 16 usable (ring buffer reserves one slot). WiFi events are rare.
881 // Placed at end of class to avoid padding between smaller fields.
883#endif
884
885 private:
886 // Stores a pointer to a string literal (static storage duration).
887 // ONLY set from Python-generated code with string literals - never dynamic strings.
888 const char *use_address_{""};
889};
890
891extern WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
892
893} // namespace esphome::wifi
894#endif
BedjetMode mode
BedJet operating mode.
ESPDEPRECATED("Use mark_failed(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary " "strings. Will stop working in 2026.6.0", "2025.12.0") void mark_failed(const char *message)
Definition component.h:224
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:522
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:210
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
constexpr const char * c_str() const
Definition string_ref.h:73
constexpr size_type size() const
Definition string_ref.h:74
20-byte string: 18 chars inline + null, heap for longer.
const char * data() const
StringRef ref() const
Return a StringRef view of this string (zero-copy)
bool operator!=(const CompactString &other) const
CompactString & operator=(const CompactString &other)
bool operator==(const CompactString &other) const
static constexpr uint8_t INLINE_CAPACITY
bool operator==(const char *other) const
bool operator!=(const StringRef &other) const
const char * c_str() const
char storage_[INLINE_CAPACITY+1]
static constexpr uint8_t MAX_LENGTH
bool operator!=(const char *other) const
uint8_t get_channel() const
StringRef get_ssid() const
void set_ssid(const std::string &ssid)
const optional< EAPAuth > & get_eap() const
void set_ssid(StringRef ssid)
void set_bssid(const bssid_t &bssid)
void set_channel(uint8_t channel)
optional< EAPAuth > eap_
StringRef get_password() const
optional< ManualIP > manual_ip_
void set_eap(optional< EAPAuth > eap_auth)
void set_password(const std::string &password)
void set_manual_ip(optional< ManualIP > manual_ip)
const optional< ManualIP > & get_manual_ip() const
int8_t get_priority() const
void set_hidden(bool hidden)
const bssid_t & get_bssid() const
void set_priority(int8_t priority)
void set_password(StringRef password)
This component is responsible for managing the ESP WiFi interface.
void notify_scan_results_listeners_()
Notify scan results listeners with current scan results.
void add_sta(const WiFiAP &ap)
bool load_fast_connect_settings_(WiFiAP &params)
void add_connect_state_listener(WiFiConnectStateListener *listener)
Add a listener for WiFi connection state changes.
void set_ap(const WiFiAP &ap)
Setup an Access Point that should be created if no connection to a station can be made.
bool request_high_performance()
Request high-performance mode (no power saving) for improved WiFi latency.
void set_sta(const WiFiAP &ap)
bool has_sta_priority(const bssid_t &bssid)
const WiFiAP * get_selected_sta_() const
WiFiSTAConnectStatus wifi_sta_connect_status_() const
void set_band_mode(wifi_band_mode_t band_mode)
int8_t get_sta_priority(const bssid_t bssid)
void log_and_adjust_priority_for_failed_connect_()
Log failed connection and decrease BSSID priority to avoid repeated attempts.
void save_wifi_sta(const std::string &ssid, const std::string &password)
void notify_connect_state_listeners_()
Notify connect state listeners (called after state machine reaches STA_CONNECTED)
wifi_scan_vector_t< WiFiScanResult > scan_result_
WiFiPowerSaveMode configured_power_save_
struct esphome::wifi::WiFiComponent::@190 pending_
void set_sta_priority(bssid_t bssid, int8_t priority)
StaticVector< WiFiScanResultsListener *, ESPHOME_WIFI_SCAN_RESULTS_LISTENERS > scan_results_listeners_
void loop() override
Reconnect WiFi if required.
void notify_ip_state_listeners_()
Notify IP state listeners with current addresses.
void start_connecting(const WiFiAP &ap)
void set_enable_on_boot(bool enable_on_boot)
void advance_to_next_target_or_increment_retry_()
Advance to next target (AP/SSID) within current phase, or increment retry counter Called when staying...
void add_power_save_listener(WiFiPowerSaveListener *listener)
Add a listener for WiFi power save mode changes.
bool wifi_sta_ip_config_(const optional< ManualIP > &manual_ip)
static constexpr uint32_t ROAMING_CHECK_INTERVAL
static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
SemaphoreHandle_t high_performance_semaphore_
network::IPAddress get_dns_address(int num)
static void wifi_event_callback(System_Event_t *event)
WiFiComponent()
Construct a WiFiComponent.
void wifi_process_event_(IDFWiFiEvent *data)
std::vector< WiFiSTAPriority > sta_priorities_
static constexpr int8_t ROAMING_GOOD_RSSI
void notify_disconnect_state_listeners_()
Notify connect state listeners of disconnection.
void set_min_auth_mode(WifiMinAuthMode min_auth_mode)
friend void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
StaticVector< WiFiConnectStateListener *, ESPHOME_WIFI_CONNECT_STATE_LISTENERS > connect_state_listeners_
void log_discarded_scan_result_(const char *ssid, const uint8_t *bssid, int8_t rssi, uint8_t channel)
Log a discarded scan result at VERBOSE level (skipped during roaming scans to avoid log overflow)
ESPDEPRECATED("Use wifi_ssid_to() instead. Removed in 2026.9.0", "2026.3.0") std const char * wifi_ssid_to(std::span< char, SSID_BUFFER_SIZE > buffer)
Write SSID to buffer without heap allocation.
void start_connecting(const WiFiAP &ap, bool)
static constexpr uint8_t ROAMING_MAX_ATTEMPTS
void set_passive_scan(bool passive)
void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info)
static void s_wifi_scan_done_callback(void *arg, STATUS status)
void set_power_save_mode(WiFiPowerSaveMode power_save)
int8_t find_next_hidden_sta_(int8_t start_index)
Find next SSID that wasn't in scan results (might be hidden) Returns index of next potentially hidden...
void add_ip_state_listener(WiFiIPStateListener *listener)
Add a listener for IP state changes.
ESPPreferenceObject fast_connect_pref_
void clear_priorities_if_all_min_()
Clear BSSID priority tracking if all priorities are at minimum (saves memory)
static constexpr uint32_t ROAMING_SCAN_GRACE_PERIOD
void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
WiFiRetryPhase determine_next_phase_()
Determine next retry phase based on current state and failure conditions.
network::IPAddress wifi_dns_ip_(int num)
network::IPAddresses get_ip_addresses()
static constexpr int8_t ROAMING_MIN_IMPROVEMENT
bool matches_configured_network_(const char *ssid, const uint8_t *bssid) const
Check if network matches any configured network (for scan result filtering) Matches by SSID when conf...
float get_setup_priority() const override
WIFI setup_priority.
void set_output_power(float output_power)
StaticVector< WiFiIPStateListener *, ESPHOME_WIFI_IP_STATE_LISTENERS > ip_state_listeners_
FixedVector< WiFiAP > sta_
int8_t find_first_non_hidden_index_() const
Find the index of the first non-hidden network Returns where EXPLICIT_HIDDEN phase would have stopped...
void release_scan_results_()
Free scan results memory unless a component needs them.
bool wifi_ap_ip_config_(const optional< ManualIP > &manual_ip)
bool needs_scan_results_() const
Check if we need valid scan results for the current phase but don't have any Returns true if the phas...
void add_scan_results_listener(WiFiScanResultsListener *listener)
Add a listener for WiFi scan results.
bool transition_to_phase_(WiFiRetryPhase new_phase)
Transition to a new retry phase with logging Returns true if a scan was started (caller should wait),...
bool needs_full_scan_results_() const
Check if full scan results are needed (captive portal active, improv, listeners)
static constexpr uint8_t FIRST_5GHZ_CHANNEL
LockFreeQueue< IDFWiFiEvent, 17 > event_queue_
void set_ap_timeout(uint32_t ap_timeout)
bool release_high_performance()
Release a high-performance mode request.
StaticVector< WiFiPowerSaveListener *, ESPHOME_WIFI_POWER_SAVE_LISTENERS > power_save_listeners_
bool wifi_apply_output_power_(float output_power)
void set_post_connect_roaming(bool enabled)
const char * get_use_address() const
bool went_through_explicit_hidden_phase_() const
Check if we went through EXPLICIT_HIDDEN phase (first network is marked hidden) Used in RETRY_HIDDEN ...
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
void set_reboot_timeout(uint32_t reboot_timeout)
network::IPAddresses wifi_sta_ip_addresses()
void check_connecting_finished(uint32_t now)
void set_keep_scan_results(bool keep_scan_results)
void start_initial_connection_()
Start initial connection - either scan or connect directly to hidden networks.
bool ssid_was_seen_in_scan_(const CompactString &ssid) const
Check if an SSID was seen in the most recent scan results Used to skip hidden mode for SSIDs we know ...
void save_wifi_sta(StringRef ssid, StringRef password)
void setup() override
Setup WiFi interface.
void clear_all_bssid_priorities_()
Clear all BSSID priority penalties after successful connection (stale after disconnect)
void set_use_address(const char *use_address)
const wifi_scan_vector_t< WiFiScanResult > & get_scan_result() const
Listener interface for WiFi connection state changes.
virtual void on_wifi_connect_state(StringRef ssid, std::span< const uint8_t, 6 > bssid)=0
Listener interface for WiFi IP state changes.
virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1, const network::IPAddress &dns2)=0
Listener interface for WiFi power save mode changes.
virtual void on_wifi_power_save(WiFiPowerSaveMode mode)=0
WiFiScanResult(const bssid_t &bssid, const char *ssid, size_t ssid_len, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden)
const bssid_t & get_bssid() const
bool matches(const WiFiAP &config) const
void set_priority(int8_t priority)
bool operator==(const WiFiScanResult &rhs) const
Listener interface for WiFi scan results.
virtual void on_wifi_scan_results(const wifi_scan_vector_t< WiFiScanResult > &results)=0
uint8_t priority
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:187
std::array< uint8_t, 6 > bssid_t
RetryHiddenMode
Controls how RETRY_HIDDEN phase selects networks to try.
@ BLIND_RETRY
Blind retry mode: scanning disabled (captive portal/improv active), try ALL configured networks seque...
@ SCAN_BASED
Normal mode: scan completed, only try networks NOT visible in scan results (truly hidden networks tha...
std::vector< T > wifi_scan_vector_t
struct esphome::wifi::SavedWifiSettings PACKED
WiFiRetryPhase
Tracks the current retry strategy/phase for WiFi connection attempts.
@ RETRY_HIDDEN
Retry networks not found in scan (might be hidden)
@ RESTARTING_ADAPTER
Restarting WiFi adapter to clear stuck state.
@ INITIAL_CONNECT
Initial connection attempt (varies based on fast_connect setting)
@ EXPLICIT_HIDDEN
Explicitly hidden networks (user marked as hidden, try before scanning)
@ FAST_CONNECT_CYCLING_APS
Fast connect mode: cycling through configured APs (config-only, no scan)
@ SCAN_CONNECTING
Scan-based: connecting to best AP from scan results.
WiFiComponent * global_wifi_component
RoamingState
Tracks post-connect roaming state machine.
@ SCANNING
Scanning for better AP.
@ IDLE
Not roaming, waiting for next check interval.
@ RECONNECTING
Roam connection failed, reconnecting to any available AP.
@ WIFI_COMPONENT_STATE_DISABLED
WiFi is disabled.
@ WIFI_COMPONENT_STATE_AP
WiFi is in AP-only mode and internal AP is already enabled.
@ WIFI_COMPONENT_STATE_STA_CONNECTING
WiFi is in STA(+AP) mode and currently connecting to an AP.
@ WIFI_COMPONENT_STATE_OFF
Nothing has been initialized yet.
@ WIFI_COMPONENT_STATE_STA_SCANNING
WiFi is in STA-only mode and currently scanning for APs.
@ WIFI_COMPONENT_STATE_COOLDOWN
WiFi is in cooldown mode because something went wrong, scanning will begin after a short period of ti...
@ WIFI_COMPONENT_STATE_STA_CONNECTED
WiFi is in STA(+AP) mode and successfully connected.
std::string size_t len
Definition helpers.h:1045
static void uint32_t
esp_eap_ttls_phase2_types ttls_phase_2
Struct for setting static IPs in WiFiComponent.
network::IPAddress static_ip
network::IPAddress dns1
The first DNS server. 0.0.0.0 for default.
network::IPAddress gateway
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
network::IPAddress subnet
uint8_t event_id
Definition tt21100.cpp:3