ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
wifi_info_text_sensor.h
Go to the documentation of this file.
1#pragma once
2
6#ifdef USE_WIFI
7#include <array>
8
9namespace esphome {
10namespace 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 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 void dump_config() override;
54
55 protected:
56 std::string last_results_;
57};
58
60 public:
61 void update() override {
62 std::string scan_results;
63 for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
64 if (scan.get_is_hidden())
65 continue;
66
67 scan_results += scan.get_ssid();
68 scan_results += ": ";
69 scan_results += esphome::to_string(scan.get_rssi());
70 scan_results += "dB\n";
71 }
72
73 if (this->last_scan_results_ != scan_results) {
74 this->last_scan_results_ = scan_results;
75 // There's a limit of 255 characters per state.
76 // Longer states just don't get sent so we truncate it.
77 this->publish_state(scan_results.substr(0, 255));
78 }
79 }
80 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
81 void dump_config() override;
82
83 protected:
84 std::string last_scan_results_;
85};
86
88 public:
89 void update() override {
90 std::string ssid = wifi::global_wifi_component->wifi_ssid();
91 if (this->last_ssid_ != ssid) {
92 this->last_ssid_ = ssid;
93 this->publish_state(this->last_ssid_);
94 }
95 }
96 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
97 void dump_config() override;
98
99 protected:
100 std::string last_ssid_;
101};
102
104 public:
105 void update() override {
107 if (memcmp(bssid.data(), last_bssid_.data(), 6) != 0) {
108 std::copy(bssid.begin(), bssid.end(), last_bssid_.begin());
109 char buf[30];
110 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
111 this->publish_state(buf);
112 }
113 }
114 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
115 void dump_config() override;
116
117 protected:
119};
120
122 public:
123 void setup() override { this->publish_state(get_mac_address_pretty()); }
124 void dump_config() override;
125};
126
127} // namespace wifi_info
128} // namespace esphome
129#endif
This class simplifies creating components that periodically check a state.
Definition component.h:425
void publish_state(const std::string &state)
network::IPAddress get_dns_address(int num)
std::array< text_sensor::TextSensor *, 5 > ip_sensors_
void add_ip_sensors(uint8_t index, text_sensor::TextSensor *s)
float get_setup_priority() const override
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:144
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:57
std::array< uint8_t, 6 > bssid_t
WiFiComponent * global_wifi_component
Providing packet encoding functions for exchanging data with a remote host.
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:584
std::string str() const
Definition ip_address.h:52