ESPHome
2026.11.0b6
Loading...
Searching...
No Matches
esphome
components
ethernet_info
ethernet_info_text_sensor.cpp
Go to the documentation of this file.
1
#include "
ethernet_info_text_sensor.h
"
2
#include "
esphome/core/log.h
"
3
4
#ifdef USE_ETHERNET
5
6
namespace
esphome::ethernet_info
{
7
8
ESPHOME_LOG_TAG
(TAG,
"ethernet_info"
);
9
10
#ifdef USE_ETHERNET_IP_STATE_LISTENERS
11
void
IPAddressEthernetInfo::setup
() {
ethernet::global_eth_component
->
add_ip_state_listener
(
this
); }
12
13
void
IPAddressEthernetInfo::dump_config
() { LOG_TEXT_SENSOR(
""
,
"EthernetInfo IPAddress"
,
this
); }
14
15
void
IPAddressEthernetInfo::on_ip_state
(
const
network::IPAddresses
&ips,
const
network::IPAddress
&dns1,
16
const
network::IPAddress
&dns2) {
17
char
buf[network::IP_ADDRESS_BUFFER_SIZE];
18
ips[0].str_to(buf);
19
this->
publish_state
(buf);
20
#ifdef ETHERNET_INFO_IP_SENSOR_COUNT
21
uint8_t sensor = 0;
22
for
(
const
auto
&ip : ips) {
23
if
(!ip.is_set())
24
continue
;
25
if
(sensor == ETHERNET_INFO_IP_SENSOR_COUNT)
26
break
;
27
if
(this->
ip_sensors_
[sensor] !=
nullptr
) {
28
ip.str_to(buf);
29
this->
ip_sensors_
[sensor]->publish_state(buf);
30
}
31
sensor++;
32
}
33
#endif
34
}
35
36
void
DNSAddressEthernetInfo::setup
() {
ethernet::global_eth_component
->
add_ip_state_listener
(
this
); }
37
38
void
DNSAddressEthernetInfo::dump_config
() { LOG_TEXT_SENSOR(
""
,
"EthernetInfo DNS Address"
,
this
); }
39
40
void
DNSAddressEthernetInfo::on_ip_state
(
const
network::IPAddresses
&ips,
const
network::IPAddress
&dns1,
41
const
network::IPAddress
&dns2) {
42
// IP_ADDRESS_BUFFER_SIZE (40) = max IP (39) + null; space reuses first null's slot
43
char
buf[network::IP_ADDRESS_BUFFER_SIZE * 2];
44
dns1.
str_to
(buf);
45
size_t
len1 = strlen(buf);
46
buf[len1] =
' '
;
47
dns2.
str_to
(buf + len1 + 1);
48
this->
publish_state
(buf);
49
}
50
#endif
// USE_ETHERNET_IP_STATE_LISTENERS
51
52
void
MACAddressEthernetInfo::dump_config
() { LOG_TEXT_SENSOR(
""
,
"EthernetInfo MAC Address"
,
this
); }
53
54
}
// namespace esphome::ethernet_info
55
56
#endif
// USE_ETHERNET
esphome::ethernet::EthernetComponent::add_ip_state_listener
void add_ip_state_listener(EthernetIPStateListener *listener)
Definition
ethernet_component.h:219
esphome::ethernet_info::DNSAddressEthernetInfo::on_ip_state
void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1, const network::IPAddress &dns2) override
Definition
ethernet_info_text_sensor.cpp:40
esphome::ethernet_info::DNSAddressEthernetInfo::setup
void setup() override
Definition
ethernet_info_text_sensor.cpp:36
esphome::ethernet_info::DNSAddressEthernetInfo::dump_config
void dump_config() override
Definition
ethernet_info_text_sensor.cpp:38
esphome::ethernet_info::IPAddressEthernetInfo::dump_config
void dump_config() override
Definition
ethernet_info_text_sensor.cpp:13
esphome::ethernet_info::IPAddressEthernetInfo::on_ip_state
void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1, const network::IPAddress &dns2) override
Definition
ethernet_info_text_sensor.cpp:15
esphome::ethernet_info::IPAddressEthernetInfo::setup
void setup() override
Definition
ethernet_info_text_sensor.cpp:11
esphome::ethernet_info::IPAddressEthernetInfo::ip_sensors_
std::array< text_sensor::TextSensor *, ETHERNET_INFO_IP_SENSOR_COUNT > ip_sensors_
Definition
ethernet_info_text_sensor.h:32
esphome::ethernet_info::MACAddressEthernetInfo::dump_config
void dump_config() override
Definition
ethernet_info_text_sensor.cpp:52
esphome::text_sensor::TextSensor::publish_state
void publish_state(const std::string &state)
Definition
text_sensor.h:42
ethernet_info_text_sensor.h
log.h
esphome::ethernet_info
Definition
ethernet_info_text_sensor.cpp:6
esphome::ethernet_info::ESPHOME_LOG_TAG
ESPHOME_LOG_TAG(TAG, "ethernet_info")
esphome::ethernet::global_eth_component
EthernetComponent * global_eth_component
Definition
ethernet_component.cpp:9
esphome::network::IPAddresses
std::array< IPAddress, 5 > IPAddresses
Definition
ip_address.h:368
esphome::network::IPAddress
Definition
ip_address.h:108
esphome::network::IPAddress::str_to
char * str_to(char *buf) const
Definition
ip_address.h:152
Generated by
1.12.0