mirror of
https://github.com/esphome/esphome.git
synced 2026-09-03 19:46:02 +00:00
54 lines
2.0 KiB
Python
54 lines
2.0 KiB
Python
"""DNS resolver for ESPHome using aioesphomeapi."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from aioesphomeapi.core import ResolveAPIError, ResolveTimeoutAPIError
|
|
import aioesphomeapi.host_resolver as hr
|
|
|
|
from esphome.async_thread import AsyncThreadRunner
|
|
from esphome.core import EsphomeError
|
|
|
|
RESOLVE_TIMEOUT = 10.0 # seconds
|
|
|
|
|
|
class AsyncResolver:
|
|
"""Resolver using aioesphomeapi that runs in a thread for faster results.
|
|
|
|
This resolver uses aioesphomeapi's async_resolve_host to handle DNS
|
|
resolution, including proper .local domain fallback. Running in a thread
|
|
(via :class:`AsyncThreadRunner`) allows us to get the result immediately
|
|
without waiting for ``asyncio.run()`` to complete its cleanup cycle, which
|
|
can take significant time.
|
|
"""
|
|
|
|
def __init__(self, hosts: list[str], port: int) -> None:
|
|
"""Initialize the resolver."""
|
|
self.hosts = hosts
|
|
self.port = port
|
|
|
|
async def _resolve(self) -> list[hr.AddrInfo]:
|
|
"""Resolve hostnames to IP addresses."""
|
|
return await hr.async_resolve_host(
|
|
self.hosts, self.port, timeout=RESOLVE_TIMEOUT
|
|
)
|
|
|
|
def resolve(self) -> list[hr.AddrInfo]:
|
|
"""Start the thread and wait for the result."""
|
|
runner: AsyncThreadRunner[list[hr.AddrInfo]] = AsyncThreadRunner(self._resolve)
|
|
runner.start()
|
|
|
|
if not runner.event.wait(
|
|
timeout=RESOLVE_TIMEOUT + 1.0
|
|
): # Give it 1 second more than the resolver timeout
|
|
raise EsphomeError("Timeout resolving IP address")
|
|
|
|
if exc := runner.exception:
|
|
if isinstance(exc, ResolveTimeoutAPIError):
|
|
raise EsphomeError(f"Timeout resolving IP address: {exc}") from exc
|
|
if isinstance(exc, ResolveAPIError):
|
|
raise EsphomeError(f"Error resolving IP address: {exc}") from exc
|
|
raise exc
|
|
|
|
assert runner.result is not None # guaranteed when event set and no exception
|
|
return runner.result
|