13#ifdef USE_ESP32_FRAMEWORK_ARDUINO
23#if defined(USE_ESP32) && defined(USE_WIFI_WPA2_EAP)
24#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
25#include <esp_eap_client.h>
32#include <ESP8266WiFi.h>
33#include <ESP8266WiFiType.h>
35#if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0)
37#include <user_interface.h>
45#include "cyw43_country.h"
46#include "pico/cyw43_arch.h"
56static constexpr int8_t WIFI_RSSI_DISCONNECTED = -127;
98#ifdef USE_WIFI_FAST_CONNECT
121#ifdef USE_WIFI_WPA2_EAP
149 void set_ssid(
const std::string &ssid);
153#ifdef USE_WIFI_WPA2_EAP
158#ifdef USE_WIFI_MANUAL_IP
162 const std::string &
get_ssid()
const;
165#ifdef USE_WIFI_WPA2_EAP
170#ifdef USE_WIFI_MANUAL_IP
179#ifdef USE_WIFI_WPA2_EAP
182#ifdef USE_WIFI_MANUAL_IP
192 WiFiScanResult(
const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi,
bool with_auth,
bool is_hidden);
199 const std::string &
get_ssid()
const;
293 void save_wifi_sta(
const std::string &ssid,
const std::string &password);
297 void setup()
override;
306 void loop()
override;
311#ifdef USE_WIFI_11KV_SUPPORT
327 if (it.bssid == bssid)
334 if (it.bssid == bssid)
341 if (it.bssid == bssid) {
418 if (this->
sta_.empty())
420 for (
const auto &ap : this->
sta_) {
453#ifdef USE_WIFI_FAST_CONNECT
464#ifdef USE_ESP32_FRAMEWORK_ARDUINO
490#ifdef USE_WIFI_FAST_CONNECT
525#ifdef USE_WIFI_11KV_SUPPORT
540 const char *use_address_{
""};
567 TEMPLATABLE_VALUE(std::string, ssid)
568 TEMPLATABLE_VALUE(std::string, password)
569 TEMPLATABLE_VALUE(
bool, save)
570 TEMPLATABLE_VALUE(uint32_t, connection_timeout)
572 void play(
const Ts &...
x)
override {
573 auto ssid = this->ssid_.value(
x...);
574 auto password = this->password_.value(
x...);
576 if (this->connecting_)
586 this->
new_sta_.set_password(password);
588 this->
old_sta_ = global_wifi_component->get_sta();
592 this->connecting_ =
true;
596 if (this->save_.value(
x...)) {
604 this->
set_timeout(
"wifi-connect-timeout", this->connection_timeout_.value(
x...), [
this,
x...]() {
606 global_wifi_component->disable();
607 global_wifi_component->save_wifi_sta(old_sta_.get_ssid(), old_sta_.get_password());
608 global_wifi_component->enable();
610 this->set_timeout(
"wifi-fallback-timeout", this->connection_timeout_.value(x...), [this]() {
611 this->connecting_ = false;
612 this->error_trigger_->trigger();
617 Trigger<> *get_connect_trigger()
const {
return this->connect_trigger_; }
618 Trigger<> *get_error_trigger()
const {
return this->error_trigger_; }
620 void loop()
override {
621 if (!this->connecting_)
625 this->cancel_timeout(
"wifi-connect-timeout");
626 this->cancel_timeout(
"wifi-fallback-timeout");
627 this->connecting_ =
false;
630 this->connect_trigger_->trigger();
633 this->error_trigger_->trigger();
639 bool connecting_{
false};
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.
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
const optional< bssid_t > & get_bssid() const
const std::string & get_ssid() const
void set_ssid(const std::string &ssid)
const optional< uint8_t > & get_channel() const
const optional< EAPAuth > & get_eap() const
void set_channel(optional< uint8_t > channel)
const std::string & get_password() const
void set_bssid(bssid_t bssid)
optional< uint8_t > channel_
optional< bssid_t > bssid_
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)
void set_priority(int8_t priority)
This component is responsible for managing the ESP WiFi interface.
bool is_captive_portal_active_()
Trigger * get_connect_trigger() const
bool error_from_callback_
void add_sta(const WiFiAP &ap)
bool load_fast_connect_settings_(WiFiAP ¶ms)
bool wifi_apply_power_save_()
void set_ap(const WiFiAP &ap)
Setup an Access Point that should be created if no connection to a station can be made.
ESPPreferenceObject pref_
void set_sta(const WiFiAP &ap)
WiFiPowerSaveMode power_save_
bool has_sta_priority(const bssid_t &bssid)
const WiFiAP * get_selected_sta_() const
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 init_sta(size_t count)
void check_connecting_finished()
void save_wifi_sta(const std::string &ssid, const std::string &password)
WiFiComponentState state_
wifi_scan_vector_t< WiFiScanResult > scan_result_
void set_sta_priority(const bssid_t bssid, int8_t priority)
void print_connect_params_()
void loop() override
Reconnect WiFi if required.
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...
bool wifi_sta_ip_config_(const optional< ManualIP > &manual_ip)
void check_scanning_finished()
static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
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_
int8_t selected_sta_index_
void save_fast_connect_settings_()
void set_min_auth_mode(WifiMinAuthMode min_auth_mode)
bool wifi_start_ap_(const WiFiAP &ap)
int32_t get_wifi_channel()
void start_connecting(const WiFiAP &ap, bool)
network::IPAddress wifi_subnet_mask_()
bool has_saved_wifi_settings_
network::IPAddress wifi_soft_ap_ip()
void set_passive_scan(bool passive)
void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info)
network::IPAddress wifi_gateway_ip_()
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...
Trigger * connect_trigger_
ESPPreferenceObject fast_connect_pref_
void clear_priorities_if_all_min_()
Clear BSSID priority tracking if all priorities are at minimum (saves memory)
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)
float get_loop_priority() const override
bool wifi_apply_hostname_()
bool wifi_sta_pre_setup_()
network::IPAddresses get_ip_addresses()
Trigger * disconnect_trigger_
float get_setup_priority() const override
WIFI setup_priority.
void set_output_power(float output_power)
bool is_esp32_improv_active_()
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...
bool ssid_was_seen_in_scan_(const std::string &ssid) const
Check if an SSID was seen in the most recent scan results Used to skip hidden mode for SSIDs we know ...
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...
WiFiRetryPhase retry_phase_
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),...
Trigger * get_disconnect_trigger() const
WiFiAP build_params_for_current_phase_()
void dump_config() override
optional< float > output_power_
void set_ap_timeout(uint32_t ap_timeout)
WifiMinAuthMode min_auth_mode_
bool wifi_apply_output_power_(float output_power)
const char * get_use_address() const
WiFiSTAConnectStatus wifi_sta_connect_status_()
bool wifi_sta_connect_(const WiFiAP &ap)
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()
uint8_t num_ipv6_addresses_
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 wifi_scan_start_(bool passive)
bool all_networks_hidden_() const
bool handled_connected_state_
void wifi_scan_done_callback_()
void setup() override
Setup WiFi interface.
void set_use_address(const char *use_address)
void reset_selected_ap_to_first_if_invalid_()
const wifi_scan_vector_t< WiFiScanResult > & get_scan_result() const
bool check(const Ts &...x) override
void play(const Ts &...x) override
void play(const Ts &...x) override
bool check(const Ts &...x) override
const std::string & get_ssid() const
int8_t get_priority() const
uint8_t get_channel() const
bool get_with_auth() const
const bssid_t & get_bssid() const
WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden)
bool matches(const WiFiAP &config) const
void set_matches(bool matches)
void set_priority(int8_t priority)
bool get_is_hidden() const
bool operator==(const WiFiScanResult &rhs) const
std::array< IPAddress, 5 > IPAddresses
std::array< uint8_t, 6 > bssid_t
@ WIFI_MIN_AUTH_MODE_WPA2
@ WIFI_MIN_AUTH_MODE_WPA3
std::vector< T > wifi_scan_vector_t
struct esphome::wifi::SavedWifiSettings PACKED
@ ERROR_NETWORK_NOT_FOUND
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
@ 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.
Providing packet encoding functions for exchanging data with a remote host.
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