ESPHome 2025.9.0
Loading...
Searching...
No Matches
wifi_info_text_sensor.h
Go to the documentation of this file.
1#pragma once
2
7#ifdef USE_WIFI
8#include <array>
9
10namespace esphome {
11namespace wifi_info {
12
14 public:
15 void update() override {
17 if (ips != this->last_ips_) {
18 this->last_ips_ = ips;
19 this->publish_state(ips[0].str());
20 uint8_t sensor = 0;
21 for (auto &ip : ips) {
22 if (ip.is_set()) {
23 if (this->ip_sensors_[sensor] != nullptr) {
24 this->ip_sensors_[sensor]->publish_state(ip.str());
25 }
26 sensor++;
27 }
28 }
29 }
30 }
31 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
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 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 void dump_config() override;
83
84 protected:
85 std::string last_scan_results_;
86};
87
89 public:
90 void update() override {
91 std::string ssid = wifi::global_wifi_component->wifi_ssid();
92 if (this->last_ssid_ != ssid) {
93 this->last_ssid_ = ssid;
94 this->publish_state(this->last_ssid_);
95 }
96 }
97 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
98 void dump_config() override;
99
100 protected:
101 std::string last_ssid_;
102};
103
105 public:
106 void update() override {
108 if (memcmp(bssid.data(), last_bssid_.data(), 6) != 0) {
109 std::copy(bssid.begin(), bssid.end(), last_bssid_.begin());
110 char buf[18];
111 format_mac_addr_upper(bssid.data(), buf);
112 this->publish_state(buf);
113 }
114 }
115 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
116 void dump_config() override;
117
118 protected:
120};
121
123 public:
124 void setup() override { this->publish_state(get_mac_address_pretty()); }
125 void dump_config() override;
126};
127
128} // namespace wifi_info
129} // namespace esphome
130#endif
This class simplifies creating components that periodically check a state.
Definition component.h:429
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:56
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
void format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase)
Definition helpers.h:391
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition helpers.cpp:598
std::string str() const
Definition ip_address.h:52