Skip to content

Commit

Permalink
Add ethernet DNS text sensor and simplify DNS display format (esphome…
Browse files Browse the repository at this point in the history
  • Loading branch information
HeMan authored Apr 12, 2024
1 parent 7eb524f commit 76daefe
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 9 deletions.
5 changes: 5 additions & 0 deletions esphome/components/ethernet/ethernet_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ network::IPAddresses EthernetComponent::get_ip_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) {
const char *event_name;

Expand Down
1 change: 1 addition & 0 deletions esphome/components/ethernet/ethernet_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class EthernetComponent : public Component {
void set_manual_ip(const ManualIP &manual_ip);

network::IPAddresses get_ip_addresses();
network::IPAddress get_dns_address(uint8_t num);
std::string get_use_address() const;
void set_use_address(const std::string &use_address);
bool powerdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace ethernet_info {
static const char *const TAG = "ethernet_info";

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 esphome
Expand Down
21 changes: 21 additions & 0 deletions esphome/components/ethernet_info/ethernet_info_text_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ class IPAddressEthernetInfo : public PollingComponent, public text_sensor::TextS
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 esphome

Expand Down
17 changes: 14 additions & 3 deletions esphome/components/ethernet_info/text_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
from esphome.components import text_sensor
from esphome.const import (
CONF_IP_ADDRESS,
CONF_DNS_ADDRESS,
ENTITY_CATEGORY_DIAGNOSTIC,
)

DEPENDENCIES = ["ethernet"]

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
)

DNSAddressEthernetInfo = ethernet_info_ns.class_(
"DNSAddressEthernetInfo", text_sensor.TextSensor, cg.PollingComponent
)

CONFIG_SCHEMA = cv.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(
Expand All @@ -27,7 +32,10 @@
)
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")),
}
)

Expand All @@ -40,3 +48,6 @@ async def to_code(config):
if sensor_conf := conf.get(f"address_{x}"):
sens = await text_sensor.new_text_sensor(sensor_conf)
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])
7 changes: 1 addition & 6 deletions esphome/components/wifi_info/wifi_info_text_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ class IPAddressWiFiInfo : public PollingComponent, public text_sensor::TextSenso
class DNSAddressWifiInfo : public PollingComponent, public text_sensor::TextSensor {
public:
void update() override {
std::string dns_results;

auto dns_one = wifi::global_wifi_component->get_dns_address(0);
auto dns_two = wifi::global_wifi_component->get_dns_address(1);

dns_results += "DNS1: ";
dns_results += dns_one.str();
dns_results += " DNS2: ";
dns_results += dns_two.str();
std::string dns_results = dns_one.str() + " " + dns_two.str();

if (dns_results != this->last_results_) {
this->last_results_ = dns_results;
Expand Down
2 changes: 2 additions & 0 deletions tests/components/ethernet_info/test.esp32-idf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ text_sensor:
- platform: ethernet_info
ip_address:
name: IP Address
dns_address:
name: DNS Address
2 changes: 2 additions & 0 deletions tests/components/ethernet_info/test.esp32.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ text_sensor:
- platform: ethernet_info
ip_address:
name: IP Address
dns_address:
name: DNS Address

0 comments on commit 76daefe

Please sign in to comment.