1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-13 23:33:48 +01:00

dashboard: Implement automatic ping fallback (#8263)

This commit is contained in:
J. Nick Koston
2025-02-27 15:17:07 +00:00
committed by GitHub
parent 63a7234767
commit 3048f303c5
10 changed files with 254 additions and 106 deletions

View File

@@ -1,6 +1,8 @@
from __future__ import annotations
import asyncio
from contextlib import suppress
from ipaddress import ip_address
import sys
from icmplib import NameLookupError, async_resolve
@@ -10,11 +12,15 @@ if sys.version_info >= (3, 11):
else:
from async_timeout import timeout as async_timeout
RESOLVE_TIMEOUT = 3.0
async def _async_resolve_wrapper(hostname: str) -> list[str] | Exception:
"""Wrap the icmplib async_resolve function."""
with suppress(ValueError):
return [str(ip_address(hostname))]
try:
async with async_timeout(2):
async with async_timeout(RESOLVE_TIMEOUT):
return await async_resolve(hostname)
except (asyncio.TimeoutError, NameLookupError, UnicodeError) as ex:
return ex