ESPHome  2024.3.2
wifi_info_text_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include <array>
7 
8 namespace esphome {
9 namespace wifi_info {
10 
12  public:
13  void update() override {
15  if (ips != this->last_ips_) {
16  this->last_ips_ = ips;
17  this->publish_state(ips[0].str());
18  uint8_t sensor = 0;
19  for (auto &ip : ips) {
20  if (ip.is_set()) {
21  if (this->ip_sensors_[sensor] != nullptr) {
22  this->ip_sensors_[sensor]->publish_state(ip.str());
23  }
24  sensor++;
25  }
26  }
27  }
28  }
29  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
30  std::string unique_id() override { return get_mac_address() + "-wifiinfo-ip"; }
31  void dump_config() override;
32  void add_ip_sensors(uint8_t index, text_sensor::TextSensor *s) { this->ip_sensors_[index] = s; }
33 
34  protected:
36  std::array<text_sensor::TextSensor *, 5> ip_sensors_;
37 };
38 
40  public:
41  void update() override {
42  std::string dns_results;
43 
46 
47  dns_results += "DNS1: ";
48  dns_results += dns_one.str();
49  dns_results += " DNS2: ";
50  dns_results += dns_two.str();
51 
52  if (dns_results != this->last_results_) {
53  this->last_results_ = dns_results;
54  this->publish_state(dns_results);
55  }
56  }
57  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
58  std::string unique_id() override { return get_mac_address() + "-wifiinfo-dns"; }
59  void dump_config() override;
60 
61  protected:
62  std::string last_results_;
63 };
64 
66  public:
67  void update() override {
68  std::string scan_results;
69  for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
70  if (scan.get_is_hidden())
71  continue;
72 
73  scan_results += scan.get_ssid();
74  scan_results += ": ";
75  scan_results += esphome::to_string(scan.get_rssi());
76  scan_results += "dB\n";
77  }
78 
79  if (this->last_scan_results_ != scan_results) {
80  this->last_scan_results_ = scan_results;
81  // There's a limit of 255 characters per state.
82  // Longer states just don't get sent so we truncate it.
83  this->publish_state(scan_results.substr(0, 255));
84  }
85  }
86  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
87  std::string unique_id() override { return get_mac_address() + "-wifiinfo-scanresults"; }
88  void dump_config() override;
89 
90  protected:
91  std::string last_scan_results_;
92 };
93 
95  public:
96  void update() override {
97  std::string ssid = wifi::global_wifi_component->wifi_ssid();
98  if (this->last_ssid_ != ssid) {
99  this->last_ssid_ = ssid;
100  this->publish_state(this->last_ssid_);
101  }
102  }
103  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
104  std::string unique_id() override { return get_mac_address() + "-wifiinfo-ssid"; }
105  void dump_config() override;
106 
107  protected:
108  std::string last_ssid_;
109 };
110 
112  public:
113  void update() override {
115  if (memcmp(bssid.data(), last_bssid_.data(), 6) != 0) {
116  std::copy(bssid.begin(), bssid.end(), last_bssid_.begin());
117  char buf[30];
118  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
119  this->publish_state(buf);
120  }
121  }
122  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
123  std::string unique_id() override { return get_mac_address() + "-wifiinfo-bssid"; }
124  void dump_config() override;
125 
126  protected:
128 };
129 
131  public:
132  void setup() override { this->publish_state(get_mac_address_pretty()); }
133  std::string unique_id() override { return get_mac_address() + "-wifiinfo-macadr"; }
134  void dump_config() override;
135 };
136 
137 } // namespace wifi_info
138 } // 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:26
std::string str() const
Definition: ip_address.h:119
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:587
std::array< text_sensor::TextSensor *, 5 > ip_sensors_
WiFiComponent * global_wifi_component
network::IPAddress get_dns_address(int num)
std::array< IPAddress, 5 > IPAddresses
Definition: ip_address.h:138
float get_setup_priority() const override
std::string to_string(int value)
Definition: helpers.cpp:82
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:592
void add_ip_sensors(uint8_t index, text_sensor::TextSensor *s)