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