Add ethernet DNS text sensor and simplify DNS display format (#6450)

This commit is contained in:
Jimmy Hedman 2024-04-12 06:03:08 +02:00 committed by GitHub
parent 7eb524f920
commit 76daefe21c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 47 additions and 9 deletions

View file

@ -340,6 +340,11 @@ network::IPAddresses EthernetComponent::get_ip_addresses() {
return addresses; return addresses;
} }
network::IPAddress EthernetComponent::get_dns_address(uint8_t num) {
const ip_addr_t *dns_ip = dns_getserver(num);
return dns_ip;
}
void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event, void *event_data) { void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event, void *event_data) {
const char *event_name; const char *event_name;

View file

@ -70,6 +70,7 @@ class EthernetComponent : public Component {
void set_manual_ip(const ManualIP &manual_ip); void set_manual_ip(const ManualIP &manual_ip);
network::IPAddresses get_ip_addresses(); network::IPAddresses get_ip_addresses();
network::IPAddress get_dns_address(uint8_t num);
std::string get_use_address() const; std::string get_use_address() const;
void set_use_address(const std::string &use_address); void set_use_address(const std::string &use_address);
bool powerdown(); bool powerdown();

View file

@ -9,6 +9,7 @@ namespace ethernet_info {
static const char *const TAG = "ethernet_info"; static const char *const TAG = "ethernet_info";
void IPAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo IPAddress", this); } void IPAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo IPAddress", this); }
void DNSAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo DNS Address", this); }
} // namespace ethernet_info } // namespace ethernet_info
} // namespace esphome } // namespace esphome

View file

@ -38,6 +38,27 @@ class IPAddressEthernetInfo : public PollingComponent, public text_sensor::TextS
std::array<text_sensor::TextSensor *, 5> ip_sensors_; std::array<text_sensor::TextSensor *, 5> ip_sensors_;
}; };
class DNSAddressEthernetInfo : public PollingComponent, public text_sensor::TextSensor {
public:
void update() override {
auto dns_one = ethernet::global_eth_component->get_dns_address(0);
auto dns_two = ethernet::global_eth_component->get_dns_address(1);
std::string dns_results = dns_one.str() + " " + dns_two.str();
if (dns_results != this->last_results_) {
this->last_results_ = dns_results;
this->publish_state(dns_results);
}
}
float get_setup_priority() const override { return setup_priority::ETHERNET; }
std::string unique_id() override { return get_mac_address() + "-ethernetinfo-dns"; }
void dump_config() override;
protected:
std::string last_results_;
};
} // namespace ethernet_info } // namespace ethernet_info
} // namespace esphome } // namespace esphome

View file

@ -3,6 +3,7 @@ import esphome.config_validation as cv
from esphome.components import text_sensor from esphome.components import text_sensor
from esphome.const import ( from esphome.const import (
CONF_IP_ADDRESS, CONF_IP_ADDRESS,
CONF_DNS_ADDRESS,
ENTITY_CATEGORY_DIAGNOSTIC, ENTITY_CATEGORY_DIAGNOSTIC,
) )
@ -10,14 +11,18 @@ DEPENDENCIES = ["ethernet"]
ethernet_info_ns = cg.esphome_ns.namespace("ethernet_info") ethernet_info_ns = cg.esphome_ns.namespace("ethernet_info")
IPAddressEsthernetInfo = ethernet_info_ns.class_( IPAddressEthernetInfo = ethernet_info_ns.class_(
"IPAddressEthernetInfo", text_sensor.TextSensor, cg.PollingComponent "IPAddressEthernetInfo", text_sensor.TextSensor, cg.PollingComponent
) )
DNSAddressEthernetInfo = ethernet_info_ns.class_(
"DNSAddressEthernetInfo", text_sensor.TextSensor, cg.PollingComponent
)
CONFIG_SCHEMA = cv.Schema( CONFIG_SCHEMA = cv.Schema(
{ {
cv.Optional(CONF_IP_ADDRESS): text_sensor.text_sensor_schema( cv.Optional(CONF_IP_ADDRESS): text_sensor.text_sensor_schema(
IPAddressEsthernetInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC IPAddressEthernetInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC
) )
.extend(cv.polling_component_schema("1s")) .extend(cv.polling_component_schema("1s"))
.extend( .extend(
@ -27,7 +32,10 @@ CONFIG_SCHEMA = cv.Schema(
) )
for x in range(5) for x in range(5)
} }
) ),
cv.Optional(CONF_DNS_ADDRESS): text_sensor.text_sensor_schema(
DNSAddressEthernetInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC
).extend(cv.polling_component_schema("1s")),
} }
) )
@ -40,3 +48,6 @@ async def to_code(config):
if sensor_conf := conf.get(f"address_{x}"): if sensor_conf := conf.get(f"address_{x}"):
sens = await text_sensor.new_text_sensor(sensor_conf) sens = await text_sensor.new_text_sensor(sensor_conf)
cg.add(ip_info.add_ip_sensors(x, sens)) cg.add(ip_info.add_ip_sensors(x, sens))
if conf := config.get(CONF_DNS_ADDRESS):
dns_info = await text_sensor.new_text_sensor(config[CONF_DNS_ADDRESS])
await cg.register_component(dns_info, config[CONF_DNS_ADDRESS])

View file

@ -39,15 +39,10 @@ class IPAddressWiFiInfo : public PollingComponent, public text_sensor::TextSenso
class DNSAddressWifiInfo : public PollingComponent, public text_sensor::TextSensor { class DNSAddressWifiInfo : public PollingComponent, public text_sensor::TextSensor {
public: public:
void update() override { void update() override {
std::string dns_results;
auto dns_one = wifi::global_wifi_component->get_dns_address(0); auto dns_one = wifi::global_wifi_component->get_dns_address(0);
auto dns_two = wifi::global_wifi_component->get_dns_address(1); auto dns_two = wifi::global_wifi_component->get_dns_address(1);
dns_results += "DNS1: "; std::string dns_results = dns_one.str() + " " + dns_two.str();
dns_results += dns_one.str();
dns_results += " DNS2: ";
dns_results += dns_two.str();
if (dns_results != this->last_results_) { if (dns_results != this->last_results_) {
this->last_results_ = dns_results; this->last_results_ = dns_results;

View file

@ -15,3 +15,5 @@ text_sensor:
- platform: ethernet_info - platform: ethernet_info
ip_address: ip_address:
name: IP Address name: IP Address
dns_address:
name: DNS Address

View file

@ -15,3 +15,5 @@ text_sensor:
- platform: ethernet_info - platform: ethernet_info
ip_address: ip_address:
name: IP Address name: IP Address
dns_address:
name: DNS Address