mirror of
https://github.com/esphome/esphome.git
synced 2025-10-26 12:43:48 +00:00
Merge branch 'fix_ip_dns_mix' into integration
This commit is contained in:
@@ -224,36 +224,37 @@ def resolve_ip_address(
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
# Process hosts
|
# Process hosts
|
||||||
cached_addresses: list[str] = []
|
|
||||||
uncached_hosts: list[str] = []
|
uncached_hosts: list[str] = []
|
||||||
has_cache = address_cache is not None
|
|
||||||
|
|
||||||
for h in hosts:
|
for h in hosts:
|
||||||
if is_ip_address(h):
|
if is_ip_address(h):
|
||||||
if has_cache:
|
_add_ip_addresses_to_addrinfo([h], port, res)
|
||||||
# If we have a cache, treat IPs as cached
|
|
||||||
cached_addresses.append(h)
|
|
||||||
else:
|
|
||||||
# If no cache, pass IPs through to resolver with hostnames
|
|
||||||
uncached_hosts.append(h)
|
|
||||||
elif address_cache and (cached := address_cache.get_addresses(h)):
|
elif address_cache and (cached := address_cache.get_addresses(h)):
|
||||||
# Found in cache
|
_add_ip_addresses_to_addrinfo(cached, port, res)
|
||||||
cached_addresses.extend(cached)
|
|
||||||
else:
|
else:
|
||||||
# Not cached, need to resolve
|
# Not cached, need to resolve
|
||||||
if address_cache and address_cache.has_cache():
|
if address_cache and address_cache.has_cache():
|
||||||
_LOGGER.info("Host %s not in cache, will need to resolve", h)
|
_LOGGER.info("Host %s not in cache, will need to resolve", h)
|
||||||
uncached_hosts.append(h)
|
uncached_hosts.append(h)
|
||||||
|
|
||||||
# Process cached addresses (includes direct IPs and cached lookups)
|
|
||||||
_add_ip_addresses_to_addrinfo(cached_addresses, port, res)
|
|
||||||
|
|
||||||
# If we have uncached hosts (only non-IP hostnames), resolve them
|
# If we have uncached hosts (only non-IP hostnames), resolve them
|
||||||
if uncached_hosts:
|
if uncached_hosts:
|
||||||
|
from aioesphomeapi.host_resolver import AddrInfo as AioAddrInfo
|
||||||
|
|
||||||
|
from esphome.core import EsphomeError
|
||||||
from esphome.resolver import AsyncResolver
|
from esphome.resolver import AsyncResolver
|
||||||
|
|
||||||
resolver = AsyncResolver(uncached_hosts, port)
|
resolver = AsyncResolver(uncached_hosts, port)
|
||||||
|
addr_infos: list[AioAddrInfo] = []
|
||||||
|
try:
|
||||||
addr_infos = resolver.resolve()
|
addr_infos = resolver.resolve()
|
||||||
|
except EsphomeError as err:
|
||||||
|
if not res:
|
||||||
|
# No pre-resolved addresses available, DNS resolution is fatal
|
||||||
|
raise
|
||||||
|
_LOGGER.info("%s (using %d already resolved IP addresses)", err, len(res))
|
||||||
|
|
||||||
# Convert aioesphomeapi AddrInfo to our format
|
# Convert aioesphomeapi AddrInfo to our format
|
||||||
for addr_info in addr_infos:
|
for addr_info in addr_infos:
|
||||||
sockaddr = addr_info.sockaddr
|
sockaddr = addr_info.sockaddr
|
||||||
|
|||||||
@@ -454,9 +454,27 @@ def test_resolve_ip_address_mixed_list() -> None:
|
|||||||
# Mix of IP and hostname - should use async resolver
|
# Mix of IP and hostname - should use async resolver
|
||||||
result = helpers.resolve_ip_address(["192.168.1.100", "test.local"], 6053)
|
result = helpers.resolve_ip_address(["192.168.1.100", "test.local"], 6053)
|
||||||
|
|
||||||
|
assert len(result) == 2
|
||||||
|
assert result[0][4][0] == "192.168.1.100"
|
||||||
|
assert result[1][4][0] == "192.168.1.200"
|
||||||
|
MockResolver.assert_called_once_with(["test.local"], 6053)
|
||||||
|
mock_resolver.resolve.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
def test_resolve_ip_address_mixed_list_fail() -> None:
|
||||||
|
"""Test resolving a mix of IPs and hostnames with resolve failed."""
|
||||||
|
with patch("esphome.resolver.AsyncResolver") as MockResolver:
|
||||||
|
mock_resolver = MockResolver.return_value
|
||||||
|
mock_resolver.resolve.side_effect = EsphomeError(
|
||||||
|
"Error resolving IP address: [test.local]"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Mix of IP and hostname - should use async resolver
|
||||||
|
result = helpers.resolve_ip_address(["192.168.1.100", "test.local"], 6053)
|
||||||
|
|
||||||
assert len(result) == 1
|
assert len(result) == 1
|
||||||
assert result[0][4][0] == "192.168.1.200"
|
assert result[0][4][0] == "192.168.1.100"
|
||||||
MockResolver.assert_called_once_with(["192.168.1.100", "test.local"], 6053)
|
MockResolver.assert_called_once_with(["test.local"], 6053)
|
||||||
mock_resolver.resolve.assert_called_once()
|
mock_resolver.resolve.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user