1 """Get your own public IP address or that of any host."""
3 from __future__
import annotations
5 from datetime
import timedelta
6 from ipaddress
import IPv4Address, IPv6Address
10 from aiodns.error
import DNSError
32 _LOGGER = logging.getLogger(__name__)
37 def sort_ips(ips: list, querytype: str) -> list:
38 """Join IPs into a single string."""
40 if querytype ==
"AAAA":
41 ips = [IPv6Address(ip)
for ip
in ips]
43 ips = [IPv4Address(ip)
for ip
in ips]
44 return [
str(ip)
for ip
in sorted(ips)][:MAX_RESULTS]
48 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
50 """Set up the dnsip sensor entry."""
52 hostname = entry.data[CONF_HOSTNAME]
53 name = entry.data[CONF_NAME]
55 resolver_ipv4 = entry.options[CONF_RESOLVER]
56 resolver_ipv6 = entry.options[CONF_RESOLVER_IPV6]
57 port_ipv4 = entry.options[CONF_PORT]
58 port_ipv6 = entry.options[CONF_PORT_IPV6]
61 if entry.data[CONF_IPV4]:
62 entities.append(
WanIpSensor(name, hostname, resolver_ipv4,
False, port_ipv4))
63 if entry.data[CONF_IPV6]:
64 entities.append(
WanIpSensor(name, hostname, resolver_ipv6,
True, port_ipv6))
70 """Implementation of a DNS IP sensor."""
72 _attr_has_entity_name =
True
73 _attr_translation_key =
"dnsip"
74 _unrecorded_attributes = frozenset({
"resolver",
"querytype",
"ip_addresses"})
84 """Initialize the DNS IP sensor."""
88 self.
resolverresolver = aiodns.DNSResolver(tcp_port=port, udp_port=port)
89 self.
resolverresolver.nameservers = [resolver]
97 entry_type=DeviceEntryType.SERVICE,
98 identifiers={(DOMAIN, hostname)},
100 model=aiodns.__version__,
105 """Get the current DNS IP address for hostname."""
108 except DNSError
as err:
109 _LOGGER.warning(
"Exception while resolving host: %s", err)
114 [res.host
for res
in response], querytype=self.
querytypequerytype
119 self.
_retries_retries = DEFAULT_RETRIES
None __init__(self, str name, str hostname, str resolver, bool ipv6, int port)
_attr_extra_state_attributes
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
list sort_ips(list ips, str querytype)