ESPHome  2024.2.1
wifi_info_text_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace wifi_info {
9 
11  public:
12  void update() override {
14  if (ip != this->last_ip_) {
15  this->last_ip_ = ip;
16  this->publish_state(ip.str());
17  }
18  }
19  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
20  std::string unique_id() override { return get_mac_address() + "-wifiinfo-ip"; }
21  void dump_config() override;
22 
23  protected:
25 };
26 
28  public:
29  void update() override {
30  std::string dns_results;
31 
34 
35  dns_results += "DNS1: ";
36  dns_results += dns_one.str();
37  dns_results += " DNS2: ";
38  dns_results += dns_two.str();
39 
40  if (dns_results != this->last_results_) {
41  this->last_results_ = dns_results;
42  this->publish_state(dns_results);
43  }
44  }
45  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
46  std::string unique_id() override { return get_mac_address() + "-wifiinfo-dns"; }
47  void dump_config() override;
48 
49  protected:
50  std::string last_results_;
51 };
52 
54  public:
55  void update() override {
56  std::string scan_results;
57  for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
58  if (scan.get_is_hidden())
59  continue;
60 
61  scan_results += scan.get_ssid();
62  scan_results += ": ";
63  scan_results += esphome::to_string(scan.get_rssi());
64  scan_results += "dB\n";
65  }
66 
67  if (this->last_scan_results_ != scan_results) {
68  this->last_scan_results_ = scan_results;
69  // There's a limit of 255 characters per state.
70  // Longer states just don't get sent so we truncate it.
71  this->publish_state(scan_results.substr(0, 255));
72  }
73  }
74  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
75  std::string unique_id() override { return get_mac_address() + "-wifiinfo-scanresults"; }
76  void dump_config() override;
77 
78  protected:
79  std::string last_scan_results_;
80 };
81 
83  public:
84  void update() override {
85  std::string ssid = wifi::global_wifi_component->wifi_ssid();
86  if (this->last_ssid_ != ssid) {
87  this->last_ssid_ = ssid;
88  this->publish_state(this->last_ssid_);
89  }
90  }
91  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
92  std::string unique_id() override { return get_mac_address() + "-wifiinfo-ssid"; }
93  void dump_config() override;
94 
95  protected:
96  std::string last_ssid_;
97 };
98 
100  public:
101  void update() override {
103  if (memcmp(bssid.data(), last_bssid_.data(), 6) != 0) {
104  std::copy(bssid.begin(), bssid.end(), last_bssid_.begin());
105  char buf[30];
106  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
107  this->publish_state(buf);
108  }
109  }
110  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
111  std::string unique_id() override { return get_mac_address() + "-wifiinfo-bssid"; }
112  void dump_config() override;
113 
114  protected:
116 };
117 
119  public:
120  void setup() override { this->publish_state(get_mac_address_pretty()); }
121  std::string unique_id() override { return get_mac_address() + "-wifiinfo-macadr"; }
122  void dump_config() override;
123 };
124 
125 } // namespace wifi_info
126 } // namespace esphome
std::array< uint8_t, 6 > bssid_t
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:25
std::string str() const
Definition: ip_address.h:112
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition: helpers.cpp:585
WiFiComponent * global_wifi_component
network::IPAddress get_dns_address(int num)
float get_setup_priority() const override
std::string to_string(int value)
Definition: helpers.cpp:80
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition: helpers.cpp:590