ESPHome  2024.10.2
ethernet_info_text_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #ifdef USE_ESP32
8 
9 namespace esphome {
10 namespace ethernet_info {
11 
13  public:
14  void update() override {
16  if (ips != this->last_ips_) {
17  this->last_ips_ = ips;
18  this->publish_state(ips[0].str());
19  uint8_t sensor = 0;
20  for (auto &ip : ips) {
21  if (ip.is_set()) {
22  if (this->ip_sensors_[sensor] != nullptr) {
23  this->ip_sensors_[sensor]->publish_state(ip.str());
24  }
25  sensor++;
26  }
27  }
28  }
29  }
30 
31  float get_setup_priority() const override { return setup_priority::ETHERNET; }
32  std::string unique_id() override { return get_mac_address() + "-ethernetinfo"; }
33  void dump_config() override;
34  void add_ip_sensors(uint8_t index, text_sensor::TextSensor *s) { this->ip_sensors_[index] = s; }
35 
36  protected:
38  std::array<text_sensor::TextSensor *, 5> ip_sensors_;
39 };
40 
42  public:
43  void update() override {
46 
47  std::string dns_results = dns_one.str() + " " + dns_two.str();
48 
49  if (dns_results != this->last_results_) {
50  this->last_results_ = dns_results;
51  this->publish_state(dns_results);
52  }
53  }
54  float get_setup_priority() const override { return setup_priority::ETHERNET; }
55  std::string unique_id() override { return get_mac_address() + "-ethernetinfo-dns"; }
56  void dump_config() override;
57 
58  protected:
59  std::string last_results_;
60 };
61 
63  public:
64  void setup() override { this->publish_state(ethernet::global_eth_component->get_eth_mac_address_pretty()); }
65  std::string unique_id() override { return get_mac_address() + "-ethernetinfo-mac"; }
66  void dump_config() override;
67 };
68 
69 } // namespace ethernet_info
70 } // namespace esphome
71 
72 #endif // USE_ESP32
std::string str() const
Definition: ip_address.h:122
void add_ip_sensors(uint8_t index, text_sensor::TextSensor *s)
network::IPAddress get_dns_address(uint8_t num)
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
EthernetComponent * global_eth_component
std::array< text_sensor::TextSensor *, 5 > ip_sensors_
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition: helpers.cpp:691
std::array< IPAddress, 5 > IPAddresses
Definition: ip_address.h:141
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
esphome::sensor::Sensor * sensor
Definition: statsd.h:37