ESPHome  2024.7.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 {
44 
45  std::string dns_results = dns_one.str() + " " + dns_two.str();
46 
47  if (dns_results != this->last_results_) {
48  this->last_results_ = dns_results;
49  this->publish_state(dns_results);
50  }
51  }
52  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
53  std::string unique_id() override { return get_mac_address() + "-wifiinfo-dns"; }
54  void dump_config() override;
55 
56  protected:
57  std::string last_results_;
58 };
59 
61  public:
62  void update() override {
63  std::string scan_results;
64  for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
65  if (scan.get_is_hidden())
66  continue;
67 
68  scan_results += scan.get_ssid();
69  scan_results += ": ";
70  scan_results += esphome::to_string(scan.get_rssi());
71  scan_results += "dB\n";
72  }
73 
74  if (this->last_scan_results_ != scan_results) {
75  this->last_scan_results_ = scan_results;
76  // There's a limit of 255 characters per state.
77  // Longer states just don't get sent so we truncate it.
78  this->publish_state(scan_results.substr(0, 255));
79  }
80  }
81  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
82  std::string unique_id() override { return get_mac_address() + "-wifiinfo-scanresults"; }
83  void dump_config() override;
84 
85  protected:
86  std::string last_scan_results_;
87 };
88 
90  public:
91  void update() override {
92  std::string ssid = wifi::global_wifi_component->wifi_ssid();
93  if (this->last_ssid_ != ssid) {
94  this->last_ssid_ = ssid;
95  this->publish_state(this->last_ssid_);
96  }
97  }
98  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
99  std::string unique_id() override { return get_mac_address() + "-wifiinfo-ssid"; }
100  void dump_config() override;
101 
102  protected:
103  std::string last_ssid_;
104 };
105 
107  public:
108  void update() override {
110  if (memcmp(bssid.data(), last_bssid_.data(), 6) != 0) {
111  std::copy(bssid.begin(), bssid.end(), last_bssid_.begin());
112  char buf[30];
113  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
114  this->publish_state(buf);
115  }
116  }
117  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
118  std::string unique_id() override { return get_mac_address() + "-wifiinfo-bssid"; }
119  void dump_config() override;
120 
121  protected:
123 };
124 
126  public:
127  void setup() override { this->publish_state(get_mac_address_pretty()); }
128  std::string unique_id() override { return get_mac_address() + "-wifiinfo-macadr"; }
129  void dump_config() override;
130 };
131 
132 } // namespace wifi_info
133 } // 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:120
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:688
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:139
float get_setup_priority() const override
std::string to_string(int value)
Definition: helpers.cpp:82
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:693
void add_ip_sensors(uint8_t index, text_sensor::TextSensor *s)