Add DNS to Text info (#4821)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Christian 2023-05-17 05:29:21 +01:00 committed by GitHub
parent 90598d2405
commit 0de47e2a4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 44 additions and 0 deletions

View file

@ -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";

View file

@ -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);

View file

@ -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)

View file

@ -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

View file

@ -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 {

View file

@ -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"

View file

@ -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