|
4 | 4 |
|
5 | 5 | import socket |
6 | 6 | from typing import Any |
7 | | -from zeroconf import IPVersion |
8 | | -from zeroconf.asyncio import AsyncZeroconf, AsyncServiceInfo |
| 7 | +from zeroconf.asyncio import AsyncZeroconf |
| 8 | +from zeroconf import ( |
| 9 | + AddressResolver, |
| 10 | + AddressResolverIPv4, |
| 11 | + AddressResolverIPv6, |
| 12 | + IPVersion, |
| 13 | +) |
9 | 14 | from aiohttp.resolver import AsyncResolver, ResolveResult |
10 | 15 | from ipaddress import IPv4Address, IPv6Address |
11 | 16 |
|
12 | | - |
13 | | -class IPv6orIPv4HostResolver(AsyncServiceInfo): |
14 | | - """Resolve a host name to an IP address.""" |
15 | | - |
16 | | - @property |
17 | | - def _is_complete(self) -> bool: |
18 | | - """The ServiceInfo has all expected properties.""" |
19 | | - return bool(self._ipv4_addresses) or bool(self._ipv6_addresses) |
20 | | - |
21 | | - |
22 | | -class IPv6HostResolver(AsyncServiceInfo): |
23 | | - """Resolve a host name to an IP address.""" |
24 | | - |
25 | | - @property |
26 | | - def _is_complete(self) -> bool: |
27 | | - """The ServiceInfo has all expected properties.""" |
28 | | - return bool(self._ipv6_addresses) |
29 | | - |
30 | | - |
31 | | -class IPv4HostResolver(AsyncServiceInfo): |
32 | | - """Resolve a host name to an IP address.""" |
33 | | - |
34 | | - @property |
35 | | - def _is_complete(self) -> bool: |
36 | | - """The ServiceInfo has all expected properties.""" |
37 | | - return bool(self._ipv4_addresses) |
38 | | - |
39 | | - |
40 | 17 | DEFAULT_TIMEOUT = 5.0 |
41 | 18 |
|
42 | | -_FAMILY_TO_RESOLVER_CLASS = { |
43 | | - socket.AF_INET: IPv4HostResolver, |
44 | | - socket.AF_INET6: IPv6HostResolver, |
45 | | - socket.AF_UNSPEC: IPv6orIPv4HostResolver, |
| 19 | +_FAMILY_TO_RESOLVER_CLASS: dict[ |
| 20 | + socket.AddressFamily, |
| 21 | + type[AddressResolver] | type[AddressResolverIPv4] | type[AddressResolverIPv6], |
| 22 | +] = { |
| 23 | + socket.AF_INET: AddressResolverIPv4, |
| 24 | + socket.AF_INET6: AddressResolverIPv6, |
| 25 | + socket.AF_UNSPEC: AddressResolver, |
46 | 26 | } |
47 | 27 | _FAMILY_TO_IP_VERSION = { |
48 | 28 | socket.AF_INET: IPVersion.V4Only, |
@@ -98,11 +78,11 @@ async def _resolve_mdns( |
98 | 78 | self, host: str, port: int, family: socket.AddressFamily |
99 | 79 | ) -> list[ResolveResult]: |
100 | 80 | """Resolve a host name to an IP address using mDNS.""" |
101 | | - resolver_class: type[AsyncServiceInfo] = _FAMILY_TO_RESOLVER_CLASS[family] |
| 81 | + resolver_class = _FAMILY_TO_RESOLVER_CLASS[family] |
102 | 82 | ip_version: IPVersion = _FAMILY_TO_IP_VERSION[family] |
103 | 83 | if host[-1] != ".": |
104 | 84 | host += "." |
105 | | - info = resolver_class(".local.", host, server=host) |
| 85 | + info = resolver_class(host) |
106 | 86 | if ( |
107 | 87 | info.load_from_cache(self._aiozc.zeroconf) |
108 | 88 | or ( |
|
0 commit comments