ESPHome 2025.11.0b4
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
13static constexpr size_t MAX_STATE_LENGTH = 255;
14
16 public:
17 void update() override {
19 if (ips != this->last_ips_) {
20 this->last_ips_ = ips;
21 this->publish_state(ips[0].str());
22 uint8_t sensor = 0;
23 for (auto &ip : ips) {
24 if (ip.is_set()) {
25 if (this->ip_sensors_[sensor] != nullptr) {
26 this->ip_sensors_[sensor]->publish_state(ip.str());
27 }
28 sensor++;
29 }
30 }
31 }
32 }
33 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
34 void dump_config() override;
35 void add_ip_sensors(uint8_t index, text_sensor::TextSensor *s) { this->ip_sensors_[index] = s; }
36
37 protected:
39 std::array<text_sensor::TextSensor *, 5> ip_sensors_;
40};
41
43 public:
44 void update() override {
47
48 std::string dns_results = dns_one.str() + " " + dns_two.str();
49
50 if (dns_results != this->last_results_) {
51 this->last_results_ = dns_results;
52 this->publish_state(dns_results);
53 }
54 }
55 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
56 void dump_config() override;
57
58 protected:
59 std::string last_results_;
60};
61
63 public:
64 void update() override {
65 std::string scan_results;
66 for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
67 if (scan.get_is_hidden())
68 continue;
69
70 scan_results += scan.get_ssid();
71 scan_results += ": ";
72 scan_results += esphome::to_string(scan.get_rssi());
73 scan_results += "dB\n";
74 }
75
76 // There's a limit of 255 characters per state.
77 // Longer states just don't get sent so we truncate it.
78 if (scan_results.length() > MAX_STATE_LENGTH) {
79 scan_results.resize(MAX_STATE_LENGTH);
80 }
81 if (this->last_scan_results_ != scan_results) {
82 this->last_scan_results_ = scan_results;
83 this->publish_state(scan_results);
84 }
85 }
86 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
87 void dump_config() override;
88
89 protected:
90 std::string last_scan_results_;
91};
92
94 public:
95 void update() override {
96 std::string ssid = wifi::global_wifi_component->wifi_ssid();
97 if (this->last_ssid_ != ssid) {
98 this->last_ssid_ = ssid;
99 this->publish_state(this->last_ssid_);
100 }
101 }
102 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
103 void dump_config() override;
104
105 protected:
106 std::string last_ssid_;
107};
108
110 public:
111 void update() override {
113 if (memcmp(bssid.data(), last_bssid_.data(), 6) != 0) {
114 std::copy(bssid.begin(), bssid.end(), last_bssid_.begin());
115 char buf[18];
116 format_mac_addr_upper(bssid.data(), buf);
117 this->publish_state(buf);
118 }
119 }
120 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
121 void dump_config() override;
122
123 protected:
125};
126
128 public:
129 void setup() override { this->publish_state(get_mac_address_pretty()); }
130 void dump_config() override;
131};
132
133} // namespace wifi_info
134} // namespace esphome
135#endif
This class simplifies creating components that periodically check a state.
Definition component.h:437
void publish_state(const std::string &state)
network::IPAddress get_dns_address(int num)
network::IPAddresses wifi_sta_ip_addresses()
std::array< text_sensor::TextSensor *, 5 > ip_sensors_
void add_ip_sensors(uint8_t index, text_sensor::TextSensor *s)
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:66
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:604
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition helpers.cpp:640
std::string str() const
Definition ip_address.h:52