DNSIP: Add literal to querytype (#146367)

This commit is contained in:
Michael Arthur 2025-06-09 19:36:17 +12:00 committed by GitHub
parent a8aebbce9a
commit 5d58cdd98e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,6 +5,7 @@ from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from ipaddress import IPv4Address, IPv6Address from ipaddress import IPv4Address, IPv6Address
import logging import logging
from typing import Literal
import aiodns import aiodns
from aiodns.error import DNSError from aiodns.error import DNSError
@ -34,7 +35,7 @@ _LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=120) SCAN_INTERVAL = timedelta(seconds=120)
def sort_ips(ips: list, querytype: str) -> list: def sort_ips(ips: list, querytype: Literal["A", "AAAA"]) -> list:
"""Join IPs into a single string.""" """Join IPs into a single string."""
if querytype == "AAAA": if querytype == "AAAA":
@ -89,7 +90,7 @@ class WanIpSensor(SensorEntity):
self.hostname = hostname self.hostname = hostname
self.resolver = aiodns.DNSResolver(tcp_port=port, udp_port=port) self.resolver = aiodns.DNSResolver(tcp_port=port, udp_port=port)
self.resolver.nameservers = [resolver] self.resolver.nameservers = [resolver]
self.querytype = "AAAA" if ipv6 else "A" self.querytype: Literal["A", "AAAA"] = "AAAA" if ipv6 else "A"
self._retries = DEFAULT_RETRIES self._retries = DEFAULT_RETRIES
self._attr_extra_state_attributes = { self._attr_extra_state_attributes = {
"resolver": resolver, "resolver": resolver,
@ -106,7 +107,7 @@ class WanIpSensor(SensorEntity):
async def async_update(self) -> None: async def async_update(self) -> None:
"""Get the current DNS IP address for hostname.""" """Get the current DNS IP address for hostname."""
try: try:
response = await self.resolver.query(self.hostname, self.querytype) # type: ignore[call-overload] response = await self.resolver.query(self.hostname, self.querytype)
except DNSError as err: except DNSError as err:
_LOGGER.warning("Exception while resolving host: %s", err) _LOGGER.warning("Exception while resolving host: %s", err)
response = None response = None