ESPHome  2023.11.6
wifi_component.cpp
Go to the documentation of this file.
1 #include "wifi_component.h"
2 #include <cinttypes>
3 
4 #if defined(USE_ESP32) || defined(USE_ESP_IDF)
5 #include <esp_wifi.h>
6 #endif
7 #ifdef USE_ESP8266
8 #include <user_interface.h>
9 #endif
10 
11 #include <algorithm>
12 #include <utility>
13 #include "lwip/dns.h"
14 #include "lwip/err.h"
15 
17 #include "esphome/core/hal.h"
18 #include "esphome/core/helpers.h"
19 #include "esphome/core/log.h"
20 #include "esphome/core/util.h"
21 
22 #ifdef USE_CAPTIVE_PORTAL
24 #endif
25 
26 #ifdef USE_IMPROV
28 #endif
29 
30 namespace esphome {
31 namespace wifi {
32 
33 static const char *const TAG = "wifi";
34 
36 
38  ESP_LOGCONFIG(TAG, "Setting up WiFi...");
39  this->wifi_pre_setup_();
40  if (this->enable_on_boot_) {
41  this->start();
42  } else {
43 #ifdef USE_ESP32
44  esp_netif_init();
45 #endif
47  }
48 }
49 
51  ESP_LOGCONFIG(TAG, "Starting WiFi...");
52  ESP_LOGCONFIG(TAG, " Local MAC: %s", get_mac_address_pretty().c_str());
53  this->last_connected_ = millis();
54 
55  uint32_t hash = this->has_sta() ? fnv1_hash(App.get_compilation_time()) : 88491487UL;
56 
58 
59  SavedWifiSettings save{};
60  if (this->pref_.load(&save)) {
61  ESP_LOGD(TAG, "Loaded saved wifi settings: %s", save.ssid);
62 
63  WiFiAP sta{};
64  sta.set_ssid(save.ssid);
65  sta.set_password(save.password);
66  this->set_sta(sta);
67  }
68 
69  if (this->has_sta()) {
70  this->wifi_sta_pre_setup_();
71  if (this->output_power_.has_value() && !this->wifi_apply_output_power_(*this->output_power_)) {
72  ESP_LOGV(TAG, "Setting Output Power Option failed!");
73  }
74 
75  if (!this->wifi_apply_power_save_()) {
76  ESP_LOGV(TAG, "Setting Power Save Option failed!");
77  }
78 
79  if (this->fast_connect_) {
80  this->selected_ap_ = this->sta_[0];
81  this->start_connecting(this->selected_ap_, false);
82  } else {
83  this->start_scanning();
84  }
85  } else if (this->has_ap()) {
86  this->setup_ap_config_();
87  if (this->output_power_.has_value() && !this->wifi_apply_output_power_(*this->output_power_)) {
88  ESP_LOGV(TAG, "Setting Output Power Option failed!");
89  }
90 #ifdef USE_CAPTIVE_PORTAL
92  this->wifi_sta_pre_setup_();
93  this->start_scanning();
95  }
96 #endif
97  }
98 #ifdef USE_IMPROV
99  if (!this->has_sta() && esp32_improv::global_improv_component != nullptr) {
100  if (this->wifi_mode_(true, {}))
102  }
103 #endif
104  this->wifi_apply_hostname_();
105 }
106 
108  this->wifi_loop_();
109  const uint32_t now = millis();
110 
111  if (this->has_sta()) {
112  if (this->is_connected() != this->handled_connected_state_) {
113  if (this->handled_connected_state_) {
114  this->disconnect_trigger_->trigger();
115  } else {
116  this->connect_trigger_->trigger();
117  }
118  this->handled_connected_state_ = this->is_connected();
119  }
120 
121  switch (this->state_) {
123  this->status_set_warning();
124  if (millis() - this->action_started_ > 5000) {
125  if (this->fast_connect_) {
126  this->start_connecting(this->sta_[0], false);
127  } else {
128  this->start_scanning();
129  }
130  }
131  break;
132  }
134  this->status_set_warning();
135  this->check_scanning_finished();
136  break;
137  }
140  this->status_set_warning();
142  break;
143  }
144 
146  if (!this->is_connected()) {
147  ESP_LOGW(TAG, "WiFi Connection lost... Reconnecting...");
149  this->retry_connect();
150  } else {
151  this->status_clear_warning();
152  this->last_connected_ = now;
153  }
154  break;
155  }
158  break;
160  return;
161  }
162 
163  if (this->has_ap() && !this->ap_setup_) {
164  if (now - this->last_connected_ > this->ap_timeout_) {
165  ESP_LOGI(TAG, "Starting fallback AP!");
166  this->setup_ap_config_();
167 #ifdef USE_CAPTIVE_PORTAL
170 #endif
171  }
172  }
173 
174 #ifdef USE_IMPROV
176  if (now - this->last_connected_ > esp32_improv::global_improv_component->get_wifi_timeout()) {
177  if (this->wifi_mode_(true, {}))
179  }
180  }
181 
182 #endif
183 
184  if (!this->has_ap() && this->reboot_timeout_ != 0) {
185  if (now - this->last_connected_ > this->reboot_timeout_) {
186  ESP_LOGE(TAG, "Can't connect to WiFi, rebooting...");
187  App.reboot();
188  }
189  }
190  }
191 }
192 
194 
195 bool WiFiComponent::has_ap() const { return this->has_ap_; }
196 bool WiFiComponent::has_sta() const { return !this->sta_.empty(); }
197 void WiFiComponent::set_fast_connect(bool fast_connect) { this->fast_connect_ = fast_connect; }
198 #ifdef USE_WIFI_11KV_SUPPORT
199 void WiFiComponent::set_btm(bool btm) { this->btm_ = btm; }
200 void WiFiComponent::set_rrm(bool rrm) { this->rrm_ = rrm; }
201 #endif
203  if (this->has_sta())
204  return this->wifi_sta_ip();
205  if (this->has_ap())
206  return this->wifi_soft_ap_ip();
207  return {};
208 }
210  if (this->has_sta())
211  return this->wifi_dns_ip_(num);
212  return {};
213 }
214 std::string WiFiComponent::get_use_address() const {
215  if (this->use_address_.empty()) {
216  return App.get_name() + ".local";
217  }
218  return this->use_address_;
219 }
220 void WiFiComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
222  this->wifi_mode_({}, true);
223 
224  if (this->ap_setup_)
225  return;
226 
227  if (this->ap_.get_ssid().empty()) {
228  std::string name = App.get_name();
229  if (name.length() > 32) {
231  name.erase(name.begin() + 25, name.end() - 7); // Remove characters between 25 and the mac address
232  } else {
233  name = name.substr(0, 32);
234  }
235  }
236  this->ap_.set_ssid(name);
237  }
238 
239  ESP_LOGCONFIG(TAG, "Setting up AP...");
240 
241  ESP_LOGCONFIG(TAG, " AP SSID: '%s'", this->ap_.get_ssid().c_str());
242  ESP_LOGCONFIG(TAG, " AP Password: '%s'", this->ap_.get_password().c_str());
243  if (this->ap_.get_manual_ip().has_value()) {
244  auto manual = *this->ap_.get_manual_ip();
245  ESP_LOGCONFIG(TAG, " AP Static IP: '%s'", manual.static_ip.str().c_str());
246  ESP_LOGCONFIG(TAG, " AP Gateway: '%s'", manual.gateway.str().c_str());
247  ESP_LOGCONFIG(TAG, " AP Subnet: '%s'", manual.subnet.str().c_str());
248  }
249 
250  this->ap_setup_ = this->wifi_start_ap_(this->ap_);
251  ESP_LOGCONFIG(TAG, " IP Address: %s", this->wifi_soft_ap_ip().str().c_str());
252 
253  if (!this->has_sta()) {
255  }
256 }
257 
259  return 10.0f; // before other loop components
260 }
261 void WiFiComponent::set_ap(const WiFiAP &ap) {
262  this->ap_ = ap;
263  this->has_ap_ = true;
264 }
265 void WiFiComponent::add_sta(const WiFiAP &ap) { this->sta_.push_back(ap); }
267  this->clear_sta();
268  this->add_sta(ap);
269 }
270 void WiFiComponent::clear_sta() { this->sta_.clear(); }
271 void WiFiComponent::save_wifi_sta(const std::string &ssid, const std::string &password) {
272  SavedWifiSettings save{};
273  strncpy(save.ssid, ssid.c_str(), sizeof(save.ssid));
274  strncpy(save.password, password.c_str(), sizeof(save.password));
275  this->pref_.save(&save);
276  // ensure it's written immediately
278 
279  WiFiAP sta{};
280  sta.set_ssid(ssid);
281  sta.set_password(password);
282  this->set_sta(sta);
283 }
284 
285 void WiFiComponent::start_connecting(const WiFiAP &ap, bool two) {
286  ESP_LOGI(TAG, "WiFi Connecting to '%s'...", ap.get_ssid().c_str());
287 #ifdef ESPHOME_LOG_HAS_VERBOSE
288  ESP_LOGV(TAG, "Connection Params:");
289  ESP_LOGV(TAG, " SSID: '%s'", ap.get_ssid().c_str());
290  if (ap.get_bssid().has_value()) {
291  bssid_t b = *ap.get_bssid();
292  ESP_LOGV(TAG, " BSSID: %02X:%02X:%02X:%02X:%02X:%02X", b[0], b[1], b[2], b[3], b[4], b[5]);
293  } else {
294  ESP_LOGV(TAG, " BSSID: Not Set");
295  }
296 
297 #ifdef USE_WIFI_WPA2_EAP
298  if (ap.get_eap().has_value()) {
299  ESP_LOGV(TAG, " WPA2 Enterprise authentication configured:");
300  EAPAuth eap_config = ap.get_eap().value();
301  ESP_LOGV(TAG, " Identity: " LOG_SECRET("'%s'"), eap_config.identity.c_str());
302  ESP_LOGV(TAG, " Username: " LOG_SECRET("'%s'"), eap_config.username.c_str());
303  ESP_LOGV(TAG, " Password: " LOG_SECRET("'%s'"), eap_config.password.c_str());
304  bool ca_cert_present = eap_config.ca_cert != nullptr && strlen(eap_config.ca_cert);
305  bool client_cert_present = eap_config.client_cert != nullptr && strlen(eap_config.client_cert);
306  bool client_key_present = eap_config.client_key != nullptr && strlen(eap_config.client_key);
307  ESP_LOGV(TAG, " CA Cert: %s", ca_cert_present ? "present" : "not present");
308  ESP_LOGV(TAG, " Client Cert: %s", client_cert_present ? "present" : "not present");
309  ESP_LOGV(TAG, " Client Key: %s", client_key_present ? "present" : "not present");
310  } else {
311 #endif
312  ESP_LOGV(TAG, " Password: " LOG_SECRET("'%s'"), ap.get_password().c_str());
313 #ifdef USE_WIFI_WPA2_EAP
314  }
315 #endif
316  if (ap.get_channel().has_value()) {
317  ESP_LOGV(TAG, " Channel: %u", *ap.get_channel());
318  } else {
319  ESP_LOGV(TAG, " Channel: Not Set");
320  }
321  if (ap.get_manual_ip().has_value()) {
322  ManualIP m = *ap.get_manual_ip();
323  ESP_LOGV(TAG, " Manual IP: Static IP=%s Gateway=%s Subnet=%s DNS1=%s DNS2=%s", m.static_ip.str().c_str(),
324  m.gateway.str().c_str(), m.subnet.str().c_str(), m.dns1.str().c_str(), m.dns2.str().c_str());
325  } else {
326  ESP_LOGV(TAG, " Using DHCP IP");
327  }
328  ESP_LOGV(TAG, " Hidden: %s", YESNO(ap.get_hidden()));
329 #endif
330 
331  if (!this->wifi_sta_connect_(ap)) {
332  ESP_LOGE(TAG, "wifi_sta_connect_ failed!");
333  this->retry_connect();
334  return;
335  }
336 
337  if (!two) {
339  } else {
341  }
342  this->action_started_ = millis();
343 }
344 
345 const LogString *get_signal_bars(int8_t rssi) {
346  // LOWER ONE QUARTER BLOCK
347  // Unicode: U+2582, UTF-8: E2 96 82
348  // LOWER HALF BLOCK
349  // Unicode: U+2584, UTF-8: E2 96 84
350  // LOWER THREE QUARTERS BLOCK
351  // Unicode: U+2586, UTF-8: E2 96 86
352  // FULL BLOCK
353  // Unicode: U+2588, UTF-8: E2 96 88
354  if (rssi >= -50) {
355  return LOG_STR("\033[0;32m" // green
356  "\xe2\x96\x82"
357  "\xe2\x96\x84"
358  "\xe2\x96\x86"
359  "\xe2\x96\x88"
360  "\033[0m");
361  } else if (rssi >= -65) {
362  return LOG_STR("\033[0;33m" // yellow
363  "\xe2\x96\x82"
364  "\xe2\x96\x84"
365  "\xe2\x96\x86"
366  "\033[0;37m"
367  "\xe2\x96\x88"
368  "\033[0m");
369  } else if (rssi >= -85) {
370  return LOG_STR("\033[0;33m" // yellow
371  "\xe2\x96\x82"
372  "\xe2\x96\x84"
373  "\033[0;37m"
374  "\xe2\x96\x86"
375  "\xe2\x96\x88"
376  "\033[0m");
377  } else {
378  return LOG_STR("\033[0;31m" // red
379  "\xe2\x96\x82"
380  "\033[0;37m"
381  "\xe2\x96\x84"
382  "\xe2\x96\x86"
383  "\xe2\x96\x88"
384  "\033[0m");
385  }
386 }
387 
389  bssid_t bssid = wifi_bssid();
390 
391  ESP_LOGCONFIG(TAG, " Local MAC: %s", get_mac_address_pretty().c_str());
392  if (this->is_disabled()) {
393  ESP_LOGCONFIG(TAG, " WiFi is disabled!");
394  return;
395  }
396  ESP_LOGCONFIG(TAG, " SSID: " LOG_SECRET("'%s'"), wifi_ssid().c_str());
397  ESP_LOGCONFIG(TAG, " IP Address: %s", wifi_sta_ip().str().c_str());
398  ESP_LOGCONFIG(TAG, " BSSID: " LOG_SECRET("%02X:%02X:%02X:%02X:%02X:%02X"), bssid[0], bssid[1], bssid[2], bssid[3],
399  bssid[4], bssid[5]);
400  ESP_LOGCONFIG(TAG, " Hostname: '%s'", App.get_name().c_str());
401  int8_t rssi = wifi_rssi();
402  ESP_LOGCONFIG(TAG, " Signal strength: %d dB %s", rssi, LOG_STR_ARG(get_signal_bars(rssi)));
403  if (this->selected_ap_.get_bssid().has_value()) {
404  ESP_LOGV(TAG, " Priority: %.1f", this->get_sta_priority(*this->selected_ap_.get_bssid()));
405  }
406  ESP_LOGCONFIG(TAG, " Channel: %" PRId32, wifi_channel_());
407  ESP_LOGCONFIG(TAG, " Subnet: %s", wifi_subnet_mask_().str().c_str());
408  ESP_LOGCONFIG(TAG, " Gateway: %s", wifi_gateway_ip_().str().c_str());
409  ESP_LOGCONFIG(TAG, " DNS1: %s", wifi_dns_ip_(0).str().c_str());
410  ESP_LOGCONFIG(TAG, " DNS2: %s", wifi_dns_ip_(1).str().c_str());
411 #ifdef USE_WIFI_11KV_SUPPORT
412  ESP_LOGCONFIG(TAG, " BTM: %s", this->btm_ ? "enabled" : "disabled");
413  ESP_LOGCONFIG(TAG, " RRM: %s", this->rrm_ ? "enabled" : "disabled");
414 #endif
415 }
416 
419  return;
420 
421  ESP_LOGD(TAG, "Enabling WIFI...");
422  this->error_from_callback_ = false;
424  this->start();
425 }
426 
429  return;
430 
431  ESP_LOGD(TAG, "Disabling WIFI...");
433  this->wifi_disconnect_();
434  this->wifi_mode_(false, false);
435 }
436 
438 
440  this->action_started_ = millis();
441  ESP_LOGD(TAG, "Starting scan...");
442  this->wifi_scan_start_(this->passive_scan_);
444 }
445 
447  if (!this->scan_done_) {
448  if (millis() - this->action_started_ > 30000) {
449  ESP_LOGE(TAG, "Scan timeout!");
450  this->retry_connect();
451  }
452  return;
453  }
454  this->scan_done_ = false;
455 
456  ESP_LOGD(TAG, "Found networks:");
457  if (this->scan_result_.empty()) {
458  ESP_LOGD(TAG, " No network found!");
459  this->retry_connect();
460  return;
461  }
462 
463  for (auto &res : this->scan_result_) {
464  for (auto &ap : this->sta_) {
465  if (res.matches(ap)) {
466  res.set_matches(true);
467  if (!this->has_sta_priority(res.get_bssid())) {
468  this->set_sta_priority(res.get_bssid(), ap.get_priority());
469  }
470  res.set_priority(this->get_sta_priority(res.get_bssid()));
471  break;
472  }
473  }
474  }
475 
476  std::stable_sort(this->scan_result_.begin(), this->scan_result_.end(),
477  [](const WiFiScanResult &a, const WiFiScanResult &b) {
478  // return true if a is better than b
479  if (a.get_matches() && !b.get_matches())
480  return true;
481  if (!a.get_matches() && b.get_matches())
482  return false;
483 
484  if (a.get_matches() && b.get_matches()) {
485  // if both match, check priority
486  if (a.get_priority() != b.get_priority())
487  return a.get_priority() > b.get_priority();
488  }
489 
490  return a.get_rssi() > b.get_rssi();
491  });
492 
493  for (auto &res : this->scan_result_) {
494  char bssid_s[18];
495  auto bssid = res.get_bssid();
496  sprintf(bssid_s, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
497 
498  if (res.get_matches()) {
499  ESP_LOGI(TAG, "- '%s' %s" LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(),
500  res.get_is_hidden() ? "(HIDDEN) " : "", bssid_s, LOG_STR_ARG(get_signal_bars(res.get_rssi())));
501  ESP_LOGD(TAG, " Channel: %u", res.get_channel());
502  ESP_LOGD(TAG, " RSSI: %d dB", res.get_rssi());
503  } else {
504  ESP_LOGD(TAG, "- " LOG_SECRET("'%s'") " " LOG_SECRET("(%s) ") "%s", res.get_ssid().c_str(), bssid_s,
505  LOG_STR_ARG(get_signal_bars(res.get_rssi())));
506  }
507  }
508 
509  if (!this->scan_result_[0].get_matches()) {
510  ESP_LOGW(TAG, "No matching network found!");
511  this->retry_connect();
512  return;
513  }
514 
515  WiFiAP connect_params;
516  WiFiScanResult scan_res = this->scan_result_[0];
517  for (auto &config : this->sta_) {
518  // search for matching STA config, at least one will match (from checks before)
519  if (!scan_res.matches(config)) {
520  continue;
521  }
522 
523  if (config.get_hidden()) {
524  // selected network is hidden, we use the data from the config
525  connect_params.set_hidden(true);
526  connect_params.set_ssid(config.get_ssid());
527  // don't set BSSID and channel, there might be multiple hidden networks
528  // but we can't know which one is the correct one. Rely on probe-req with just SSID.
529  } else {
530  // selected network is visible, we use the data from the scan
531  // limit the connect params to only connect to exactly this network
532  // (network selection is done during scan phase).
533  connect_params.set_hidden(false);
534  connect_params.set_ssid(scan_res.get_ssid());
535  connect_params.set_channel(scan_res.get_channel());
536  connect_params.set_bssid(scan_res.get_bssid());
537  }
538  // copy manual IP (if set)
539  connect_params.set_manual_ip(config.get_manual_ip());
540 
541 #ifdef USE_WIFI_WPA2_EAP
542  // copy EAP parameters (if set)
543  connect_params.set_eap(config.get_eap());
544 #endif
545 
546  // copy password (if set)
547  connect_params.set_password(config.get_password());
548 
549  break;
550  }
551 
552  yield();
553 
554  this->selected_ap_ = connect_params;
555  this->start_connecting(connect_params, false);
556 }
557 
559  ESP_LOGCONFIG(TAG, "WiFi:");
560  this->print_connect_params_();
561 }
562 
564  auto status = this->wifi_sta_connect_status_();
565 
567  if (wifi_ssid().empty()) {
568  ESP_LOGW(TAG, "Incomplete connection.");
569  this->retry_connect();
570  return;
571  }
572 
573  ESP_LOGI(TAG, "WiFi Connected!");
574  this->print_connect_params_();
575 
576  if (this->has_ap()) {
577 #ifdef USE_CAPTIVE_PORTAL
578  if (this->is_captive_portal_active_()) {
580  }
581 #endif
582  ESP_LOGD(TAG, "Disabling AP...");
583  this->wifi_mode_({}, false);
584  }
585 #ifdef USE_IMPROV
586  if (this->is_esp32_improv_active_()) {
588  }
589 #endif
590 
592  this->num_retried_ = 0;
593  return;
594  }
595 
596  uint32_t now = millis();
597  if (now - this->action_started_ > 30000) {
598  ESP_LOGW(TAG, "Timeout while connecting to WiFi.");
599  this->retry_connect();
600  return;
601  }
602 
603  if (this->error_from_callback_) {
604  ESP_LOGW(TAG, "Error while connecting to network.");
605  this->retry_connect();
606  return;
607  }
608 
610  return;
611  }
612 
614  ESP_LOGW(TAG, "WiFi network can not be found anymore.");
615  this->retry_connect();
616  return;
617  }
618 
620  ESP_LOGW(TAG, "Connecting to WiFi network failed. Are the credentials wrong?");
621  this->retry_connect();
622  return;
623  }
624 
625  ESP_LOGW(TAG, "WiFi Unknown connection status %d", (int) status);
626  this->retry_connect();
627 }
628 
630  if (this->selected_ap_.get_bssid()) {
631  auto bssid = *this->selected_ap_.get_bssid();
632  float priority = this->get_sta_priority(bssid);
633  this->set_sta_priority(bssid, priority - 1.0f);
634  }
635 
636  delay(10);
637  if (!this->is_captive_portal_active_() && !this->is_esp32_improv_active_() &&
638  (this->num_retried_ > 5 || this->error_from_callback_)) {
639  // If retry failed for more than 5 times, let's restart STA
640  ESP_LOGW(TAG, "Restarting WiFi adapter...");
641  this->wifi_mode_(false, {});
642  delay(100); // NOLINT
643  this->num_retried_ = 0;
644  } else {
645  this->num_retried_++;
646  }
647  this->error_from_callback_ = false;
649  yield();
651  this->start_connecting(this->selected_ap_, true);
652  return;
653  }
654 
656  this->action_started_ = millis();
657 }
658 
660  if (!this->has_sta() || this->state_ == WIFI_COMPONENT_STATE_DISABLED) {
661  return true;
662  }
663  return this->is_connected();
664 }
665 void WiFiComponent::set_reboot_timeout(uint32_t reboot_timeout) { this->reboot_timeout_ = reboot_timeout; }
667  return this->state_ == WIFI_COMPONENT_STATE_STA_CONNECTED &&
669 }
670 void WiFiComponent::set_power_save_mode(WiFiPowerSaveMode power_save) { this->power_save_ = power_save; }
671 
672 void WiFiComponent::set_passive_scan(bool passive) { this->passive_scan_ = passive; }
673 
674 std::string WiFiComponent::format_mac_addr(const uint8_t *mac) {
675  char buf[20];
676  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
677  return buf;
678 }
680 #ifdef USE_CAPTIVE_PORTAL
682 #else
683  return false;
684 #endif
685 }
687 #ifdef USE_IMPROV
689 #else
690  return false;
691 #endif
692 }
693 
694 void WiFiAP::set_ssid(const std::string &ssid) { this->ssid_ = ssid; }
695 void WiFiAP::set_bssid(bssid_t bssid) { this->bssid_ = bssid; }
696 void WiFiAP::set_bssid(optional<bssid_t> bssid) { this->bssid_ = bssid; }
697 void WiFiAP::set_password(const std::string &password) { this->password_ = password; }
698 #ifdef USE_WIFI_WPA2_EAP
699 void WiFiAP::set_eap(optional<EAPAuth> eap_auth) { this->eap_ = std::move(eap_auth); }
700 #endif
701 void WiFiAP::set_channel(optional<uint8_t> channel) { this->channel_ = channel; }
702 void WiFiAP::set_manual_ip(optional<ManualIP> manual_ip) { this->manual_ip_ = manual_ip; }
703 void WiFiAP::set_hidden(bool hidden) { this->hidden_ = hidden; }
704 const std::string &WiFiAP::get_ssid() const { return this->ssid_; }
705 const optional<bssid_t> &WiFiAP::get_bssid() const { return this->bssid_; }
706 const std::string &WiFiAP::get_password() const { return this->password_; }
707 #ifdef USE_WIFI_WPA2_EAP
708 const optional<EAPAuth> &WiFiAP::get_eap() const { return this->eap_; }
709 #endif
710 const optional<uint8_t> &WiFiAP::get_channel() const { return this->channel_; }
711 const optional<ManualIP> &WiFiAP::get_manual_ip() const { return this->manual_ip_; }
712 bool WiFiAP::get_hidden() const { return this->hidden_; }
713 
714 WiFiScanResult::WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth,
715  bool is_hidden)
716  : bssid_(bssid),
717  ssid_(std::move(ssid)),
718  channel_(channel),
719  rssi_(rssi),
720  with_auth_(with_auth),
721  is_hidden_(is_hidden) {}
722 bool WiFiScanResult::matches(const WiFiAP &config) {
723  if (config.get_hidden()) {
724  // User configured a hidden network, only match actually hidden networks
725  // don't match SSID
726  if (!this->is_hidden_)
727  return false;
728  } else if (!config.get_ssid().empty()) {
729  // check if SSID matches
730  if (config.get_ssid() != this->ssid_)
731  return false;
732  } else {
733  // network is configured without SSID - match other settings
734  }
735  // If BSSID configured, only match for correct BSSIDs
736  if (config.get_bssid().has_value() && *config.get_bssid() != this->bssid_)
737  return false;
738 
739 #ifdef USE_WIFI_WPA2_EAP
740  // BSSID requires auth but no PSK or EAP credentials given
741  if (this->with_auth_ && (config.get_password().empty() && !config.get_eap().has_value()))
742  return false;
743 
744  // BSSID does not require auth, but PSK or EAP credentials given
745  if (!this->with_auth_ && (!config.get_password().empty() || config.get_eap().has_value()))
746  return false;
747 #else
748  // If PSK given, only match for networks with auth (and vice versa)
749  if (config.get_password().empty() == this->with_auth_)
750  return false;
751 #endif
752 
753  // If channel configured, only match networks on that channel.
754  if (config.get_channel().has_value() && *config.get_channel() != this->channel_) {
755  return false;
756  }
757  return true;
758 }
759 bool WiFiScanResult::get_matches() const { return this->matches_; }
761 const bssid_t &WiFiScanResult::get_bssid() const { return this->bssid_; }
762 const std::string &WiFiScanResult::get_ssid() const { return this->ssid_; }
763 uint8_t WiFiScanResult::get_channel() const { return this->channel_; }
764 int8_t WiFiScanResult::get_rssi() const { return this->rssi_; }
765 bool WiFiScanResult::get_with_auth() const { return this->with_auth_; }
766 bool WiFiScanResult::get_is_hidden() const { return this->is_hidden_; }
767 
768 bool WiFiScanResult::operator==(const WiFiScanResult &rhs) const { return this->bssid_ == rhs.bssid_; }
769 
770 WiFiComponent *global_wifi_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
771 
772 } // namespace wifi
773 } // namespace esphome
Nothing has been initialized yet.
const char * name
Definition: stm32flash.h:78
This component is responsible for managing the ESP WiFi interface.
const std::string & get_ssid() const
std::array< uint8_t, 6 > bssid_t
const optional< EAPAuth > & get_eap() const
static std::string format_mac_addr(const uint8_t mac[6])
const std::string & get_password() const
WiFiPowerSaveMode power_save_
void save_wifi_sta(const std::string &ssid, const std::string &password)
network::IPAddress wifi_dns_ip_(int num)
void set_sta_priority(const bssid_t bssid, float priority)
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
const optional< bssid_t > & get_bssid() const
std::string str() const
Definition: ip_address.h:97
bool wifi_apply_output_power_(float output_power)
void add_sta(const WiFiAP &ap)
WiFi is in STA(+AP) mode and currently connecting to an AP a second time.
STL namespace.
WiFi is in STA(+AP) mode and successfully connected.
bool has_value() const
Definition: optional.h:87
network::IPAddress static_ip
void set_ap(const WiFiAP &ap)
Setup an Access Point that should be created if no connection to a station can be made...
network::IPAddress get_ip_address()
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
const bssid_t & get_bssid() const
void set_channel(optional< uint8_t > channel)
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
bool save(const T *src)
Definition: preferences.h:21
network::IPAddress gateway
void start_connecting(const WiFiAP &ap, bool two)
WiFiComponent()
Construct a WiFiComponent.
CaptivePortal * global_captive_portal
void set_passive_scan(bool passive)
std::vector< WiFiScanResult > scan_result_
WiFi is in STA-only mode and currently scanning for APs.
uint8_t m
Definition: bl0939.h:20
Struct for setting static IPs in WiFiComponent.
void set_power_save_mode(WiFiPowerSaveMode power_save)
network::IPAddress dns1
The first DNS server. 0.0.0.0 for default.
WiFi is in STA(+AP) mode and currently connecting to an AP.
bool has_sta_priority(const bssid_t &bssid)
const optional< ManualIP > & get_manual_ip() const
ESPPreferences * global_preferences
const optional< uint8_t > & get_channel() const
void status_clear_warning()
Definition: component.cpp:153
WiFi is in cooldown mode because something went wrong, scanning will begin after a short period of ti...
void set_ssid(const std::string &ssid)
Application App
Global storage of Application pointer - only one Application can exist.
bool matches(const WiFiAP &config)
WiFiComponent * global_wifi_component
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Definition: application.h:160
void status_set_warning()
Definition: component.cpp:145
network::IPAddress get_dns_address(int num)
void set_reboot_timeout(uint32_t reboot_timeout)
bool is_name_add_mac_suffix_enabled() const
Definition: application.h:171
ESP32ImprovComponent * global_improv_component
void loop() override
Reconnect WiFi if required.
uint8_t priority
bool operator==(const WiFiScanResult &rhs) const
uint8_t status
Definition: bl0942.h:23
ESPPreferenceObject pref_
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
const char * client_cert
void IRAM_ATTR HOT yield()
Definition: core.cpp:24
void set_fast_connect(bool fast_connect)
void set_manual_ip(optional< ManualIP > manual_ip)
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
Definition: helpers.cpp:176
std::vector< WiFiAP > sta_
optional< float > output_power_
network::IPAddress subnet
void set_sta(const WiFiAP &ap)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_bssid(bssid_t bssid)
const std::string & get_ssid() const
void set_eap(optional< EAPAuth > eap_auth)
void set_hidden(bool hidden)
float get_loop_priority() const override
void set_use_address(const std::string &use_address)
WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden)
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition: helpers.cpp:576
virtual bool sync()=0
Commit pending writes to flash.
std::string get_use_address() const
std::string get_compilation_time() const
Definition: application.h:173
float get_sta_priority(const bssid_t bssid)
void set_password(const std::string &password)
void setup() override
Setup WiFi interface.
float get_setup_priority() const override
WIFI setup_priority.
WiFi is in AP-only mode and internal AP is already enabled.
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26
const LogString * get_signal_bars(int8_t rssi)