ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
ethernet_info_text_sensor.h
Go to the documentation of this file.
1#pragma once
2
6
7#ifdef USE_ESP32
8
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
30 float get_setup_priority() const override { return setup_priority::ETHERNET; }
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 if (dns1 != this->last_dns1_ || dns2 != this->last_dns2_) {
46 this->last_dns1_ = dns1;
47 this->last_dns2_ = dns2;
48 // IP_ADDRESS_BUFFER_SIZE (40) = max IP (39) + null; space reuses first null's slot
49 char buf[network::IP_ADDRESS_BUFFER_SIZE * 2];
50 dns1.str_to(buf);
51 size_t len1 = strlen(buf);
52 buf[len1] = ' ';
53 dns2.str_to(buf + len1 + 1);
54 this->publish_state(buf);
55 }
56 }
57 float get_setup_priority() const override { return setup_priority::ETHERNET; }
58 void dump_config() override;
59
60 protected:
63};
64
66 public:
67 void setup() override { this->publish_state(ethernet::global_eth_component->get_eth_mac_address_pretty()); }
68 float get_setup_priority() const override { return setup_priority::ETHERNET; }
69 void dump_config() override;
70};
71
72} // namespace esphome::ethernet_info
73
74#endif // USE_ESP32
This class simplifies creating components that periodically check a state.
Definition component.h:474
network::IPAddress get_dns_address(uint8_t num)
std::array< text_sensor::TextSensor *, 5 > ip_sensors_
void add_ip_sensors(uint8_t index, text_sensor::TextSensor *s)
void publish_state(const std::string &state)
EthernetComponent * global_eth_component
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:158
char * str_to(char *buf) const
Write IP address to buffer. Buffer must be at least IP_ADDRESS_BUFFER_SIZE bytes.
Definition ip_address.h:57