diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 9f047dd5ed..db19f9bcc0 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -183,6 +183,11 @@ network::IPAddress WiFiComponent::get_ip_address() { return this->wifi_soft_ap_ip(); return {}; } +network::IPAddress WiFiComponent::get_dns_address(int num) { + if (this->has_sta()) + return this->wifi_dns_ip_(num); + return {}; +} std::string WiFiComponent::get_use_address() const { if (this->use_address_.empty()) { return App.get_name() + ".local"; diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 3f81b94cce..d42be43b2d 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -240,6 +240,7 @@ class WiFiComponent : public Component { void set_rrm(bool rrm); #endif + network::IPAddress get_dns_address(int num); network::IPAddress get_ip_address(); std::string get_use_address() const; void set_use_address(const std::string &use_address); diff --git a/esphome/components/wifi_info/text_sensor.py b/esphome/components/wifi_info/text_sensor.py index 54993d48ee..659fd08db1 100644 --- a/esphome/components/wifi_info/text_sensor.py +++ b/esphome/components/wifi_info/text_sensor.py @@ -7,6 +7,7 @@ from esphome.const import ( CONF_SCAN_RESULTS, CONF_SSID, CONF_MAC_ADDRESS, + CONF_DNS_ADDRESS, ENTITY_CATEGORY_DIAGNOSTIC, ) @@ -28,6 +29,9 @@ BSSIDWiFiInfo = wifi_info_ns.class_( MacAddressWifiInfo = wifi_info_ns.class_( "MacAddressWifiInfo", text_sensor.TextSensor, cg.Component ) +DNSAddressWifiInfo = wifi_info_ns.class_( + "DNSAddressWifiInfo", text_sensor.TextSensor, cg.PollingComponent +) CONFIG_SCHEMA = cv.Schema( { @@ -46,6 +50,9 @@ CONFIG_SCHEMA = cv.Schema( cv.Optional(CONF_MAC_ADDRESS): text_sensor.text_sensor_schema( MacAddressWifiInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC ), + cv.Optional(CONF_DNS_ADDRESS): text_sensor.text_sensor_schema( + DNSAddressWifiInfo, entity_category=ENTITY_CATEGORY_DIAGNOSTIC + ).extend(cv.polling_component_schema("1s")), } ) @@ -63,3 +70,4 @@ async def to_code(config): await setup_conf(config, CONF_BSSID) await setup_conf(config, CONF_MAC_ADDRESS) await setup_conf(config, CONF_SCAN_RESULTS) + await setup_conf(config, CONF_DNS_ADDRESS) diff --git a/esphome/components/wifi_info/wifi_info_text_sensor.cpp b/esphome/components/wifi_info/wifi_info_text_sensor.cpp index 0b73de68de..eeb4985398 100644 --- a/esphome/components/wifi_info/wifi_info_text_sensor.cpp +++ b/esphome/components/wifi_info/wifi_info_text_sensor.cpp @@ -11,6 +11,7 @@ void ScanResultsWiFiInfo::dump_config() { LOG_TEXT_SENSOR("", "WifiInfo Scan Res void SSIDWiFiInfo::dump_config() { LOG_TEXT_SENSOR("", "WifiInfo SSID", this); } void BSSIDWiFiInfo::dump_config() { LOG_TEXT_SENSOR("", "WifiInfo BSSID", this); } void MacAddressWifiInfo::dump_config() { LOG_TEXT_SENSOR("", "WifiInfo Mac Address", this); } +void DNSAddressWifiInfo::dump_config() { LOG_TEXT_SENSOR("", "WifiInfo DNS Address", this); } } // namespace wifi_info } // namespace esphome diff --git a/esphome/components/wifi_info/wifi_info_text_sensor.h b/esphome/components/wifi_info/wifi_info_text_sensor.h index e5b0fa3223..35ce108c86 100644 --- a/esphome/components/wifi_info/wifi_info_text_sensor.h +++ b/esphome/components/wifi_info/wifi_info_text_sensor.h @@ -24,6 +24,32 @@ class IPAddressWiFiInfo : public PollingComponent, public text_sensor::TextSenso network::IPAddress last_ip_; }; +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(); + + 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::AFTER_WIFI; } + std::string unique_id() override { return get_mac_address() + "-wifiinfo-dns"; } + void dump_config() override; + + protected: + std::string last_results_; +}; + class ScanResultsWiFiInfo : public PollingComponent, public text_sensor::TextSensor { public: void update() override { diff --git a/esphome/const.py b/esphome/const.py index b215619d23..2b976a7079 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -190,6 +190,7 @@ CONF_DISCOVERY_UNIQUE_ID_GENERATOR = "discovery_unique_id_generator" CONF_DISTANCE = "distance" CONF_DITHER = "dither" CONF_DIV_RATIO = "div_ratio" +CONF_DNS_ADDRESS = "dns_address" CONF_DNS1 = "dns1" CONF_DNS2 = "dns2" CONF_DOMAIN = "domain" diff --git a/tests/test1.yaml b/tests/test1.yaml index 46c6bb80c6..0058c08c74 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -3077,6 +3077,8 @@ text_sensor: name: BSSID mac_address: name: Mac Address + dns_address: + name: DNS ADdress - platform: version name: ESPHome Version No Timestamp hide_timestamp: true