mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
Add ethernet DNS text sensor and simplify DNS display format (#6450)
This commit is contained in:
parent
7eb524f920
commit
76daefe21c
8 changed files with 47 additions and 9 deletions
|
@ -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;
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ import esphome.config_validation as cv
|
|||
from esphome.components import text_sensor
|
||||
from esphome.const import (
|
||||
CONF_IP_ADDRESS,
|
||||
CONF_DNS_ADDRESS,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
)
|
||||
|
||||
|
@ -10,14 +11,18 @@ 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(
|
||||
|
@ -27,7 +32,10 @@ CONFIG_SCHEMA = cv.Schema(
|
|||
)
|
||||
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}"):
|
||||
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])
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -15,3 +15,5 @@ text_sensor:
|
|||
- platform: ethernet_info
|
||||
ip_address:
|
||||
name: IP Address
|
||||
dns_address:
|
||||
name: DNS Address
|
||||
|
|
|
@ -15,3 +15,5 @@ text_sensor:
|
|||
- platform: ethernet_info
|
||||
ip_address:
|
||||
name: IP Address
|
||||
dns_address:
|
||||
name: DNS Address
|
||||
|
|
Loading…
Reference in a new issue