Skip to content

Commit 1ae539f

Browse files
authored
Fix slow connections by using multicast mDNS queries for cache sharing (#1394)
1 parent c69efb1 commit 1ae539f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

aioesphomeapi/host_resolver.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import socket
1212
from typing import TYPE_CHECKING, Any, cast
1313

14-
from zeroconf import IPVersion
14+
from zeroconf import DNSQuestionType, IPVersion
1515
from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf
1616

1717
from .core import ResolveAPIError, ResolveTimeoutAPIError
@@ -66,7 +66,16 @@ async def _async_zeroconf_get_service_info(
6666
) -> AsyncServiceInfo:
6767
info = _make_service_info_for_short_host(short_host)
6868
try:
69-
await info.async_request(aiozc.zeroconf, int(timeout * 1000))
69+
# Use QM (multicast) questions to ensure multicast responses that all
70+
# zeroconf instances on the network can observe and cache. RFC 6762 Section
71+
# 5.4 specifies that responders SHOULD send multicast responses to QU
72+
# (unicast) questions if they haven't multicast recently (within 1/4 TTL),
73+
# but ESP32 mDNS doesn't implement this - it always sends unicast to QU.
74+
# By using QM, we ensure the dashboard observes responses when the CLI does
75+
# resolution, preventing the dashboard's cache from going stale.
76+
await info.async_request(
77+
aiozc.zeroconf, int(timeout * 1000), question_type=DNSQuestionType.QM
78+
)
7079
except Exception as exc:
7180
raise ResolveAPIError(
7281
f"Error resolving mDNS {short_host} via mDNS: {exc}"

0 commit comments

Comments
 (0)