ESPHome
2026.1.5
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_ESP32
5
6
namespace
esphome::ethernet_info
{
7
8
static
const
char
*
const
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
uint8_t sensor = 0;
21
for
(
const
auto
&ip : ips) {
22
if
(ip.is_set()) {
23
if
(this->
ip_sensors_
[sensor] !=
nullptr
) {
24
ip.str_to(buf);
25
this->
ip_sensors_
[sensor]->publish_state(buf);
26
}
27
sensor++;
28
}
29
}
30
}
31
32
void
DNSAddressEthernetInfo::setup
() {
ethernet::global_eth_component
->
add_ip_state_listener
(
this
); }
33
34
void
DNSAddressEthernetInfo::dump_config
() { LOG_TEXT_SENSOR(
""
,
"EthernetInfo DNS Address"
,
this
); }
35
36
void
DNSAddressEthernetInfo::on_ip_state
(
const
network::IPAddresses
&ips,
const
network::IPAddress
&dns1,
37
const
network::IPAddress
&dns2) {
38
// IP_ADDRESS_BUFFER_SIZE (40) = max IP (39) + null; space reuses first null's slot
39
char
buf[network::IP_ADDRESS_BUFFER_SIZE * 2];
40
dns1.
str_to
(buf);
41
size_t
len1 = strlen(buf);
42
buf[len1] =
' '
;
43
dns2.
str_to
(buf + len1 + 1);
44
this->
publish_state
(buf);
45
}
46
#endif
// USE_ETHERNET_IP_STATE_LISTENERS
47
48
void
MACAddressEthernetInfo::dump_config
() { LOG_TEXT_SENSOR(
""
,
"EthernetInfo MAC Address"
,
this
); }
49
50
}
// namespace esphome::ethernet_info
51
52
#endif
// USE_ESP32
esphome::ethernet::EthernetComponent::add_ip_state_listener
void add_ip_state_listener(EthernetIPStateListener *listener)
Definition
ethernet_component.h:119
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:36
esphome::ethernet_info::DNSAddressEthernetInfo::setup
void setup() override
Definition
ethernet_info_text_sensor.cpp:32
esphome::ethernet_info::DNSAddressEthernetInfo::dump_config
void dump_config() override
Definition
ethernet_info_text_sensor.cpp:34
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::ip_sensors_
std::array< text_sensor::TextSensor *, 5 > ip_sensors_
Definition
ethernet_info_text_sensor.h:25
esphome::ethernet_info::IPAddressEthernetInfo::setup
void setup() override
Definition
ethernet_info_text_sensor.cpp:11
esphome::ethernet_info::MACAddressEthernetInfo::dump_config
void dump_config() override
Definition
ethernet_info_text_sensor.cpp:48
esphome::text_sensor::TextSensor::publish_state
void publish_state(const std::string &state)
Definition
text_sensor.cpp:28
ethernet_info_text_sensor.h
log.h
esphome::ethernet_info
Definition
ethernet_info_text_sensor.cpp:6
esphome::ethernet::global_eth_component
EthernetComponent * global_eth_component
Definition
ethernet_component.cpp:46
esphome::network::IPAddresses
std::array< IPAddress, 5 > IPAddresses
Definition
ip_address.h:180
esphome::network::IPAddress
Definition
ip_address.h:54
esphome::network::IPAddress::str_to
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:69
Generated by
1.12.0