Skip to content

Commit f7b9cfc

Browse files
authored
Merge pull request #12 from DiUS/fix/workaround_ha_discovery_issue
fix: workaround HA discovery cache miss
2 parents 9c56b02 + 0fe1a49 commit f7b9cfc

3 files changed

Lines changed: 232 additions & 55 deletions

File tree

‎src/powersensor_local/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
'Message',
7070
'CommonEventsDocs',
7171
]
72-
__version__ = "2.4.0rc3"
72+
__version__ = "2.4.0rc4"
7373
from .devices import (
7474
AsyncEventCallback,
7575
PowersensorDevices,

‎src/powersensor_local/zeroconf_devices.py‎

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@
5252

5353
_DEBOUNCE_DEFAULT_S = 60.0
5454

55+
# How long to wait for a plug to answer when its records are not cached.
56+
# 5 seconds is enough to cover network latency and plug response time.
57+
_REQUEST_TIMEOUT_MS = 5000
58+
5559

5660
try:
5761
import zeroconf as _zc
58-
from zeroconf.asyncio import AsyncServiceBrowser
62+
from zeroconf.asyncio import AsyncServiceBrowser, AsyncServiceInfo
5963

6064
class PowersensorZeroconfDevices(_PowersensorDevicesBase):
6165
"""Discovers and manages Powersensor plugs via continuous mDNS browsing.
@@ -237,42 +241,23 @@ def cleanup(task: Task[None]) -> None:
237241

238242

239243
class _Listener(_zc.ServiceListener):
240-
"""Zeroconf ServiceListener that forwards events to PowersensorZeroconfDevices."""
244+
"""Zeroconf ServiceListener that forwards events to PowersensorZeroconfDevices.
245+
246+
Its callbacks must never raise as Zeroconf does not catch exceptions from
247+
browser callbacks. In Home Assistant, the instance is shared with other
248+
integration, so this would disrupt their discovery too. Anything that can
249+
fail is therefore either handled here or run in a task.
250+
"""
241251

242252
def __init__(self, owner: PowersensorZeroconfDevices) -> None:
243253
self._owner = owner
244254
self._name_to_mac: dict[str, str] = {}
245255

246-
def _extract(self, zc: Any, type_: str, name: str) -> tuple[str, str, int] | None:
247-
"""Return (mac, ip, port) from the zeroconf cache, or None.
248-
249-
Uses ServiceInfo.load_from_cache() rather than
250-
Zeroconf.get_service_info() for two reasons:
251-
252-
1. On zeroconf >= 0.32 (including HA's 0.149.x), ServiceBrowser
253-
callbacks run inside the asyncio event loop. Calling
254-
get_service_info() from there blocks waiting for a DNS reply
255-
that can never be processed because the event loop is occupied —
256-
it deadlocks, times out after 3 s, and silently returns None,
257-
causing the device to be dropped.
258-
259-
2. load_from_cache() is synchronous, non-blocking, and explicitly
260-
documented as threadsafe. The ServiceBrowser guarantees the
261-
cache is populated before firing add_service / update_service,
262-
so the record is always present when this method is called.
263-
"""
264-
info = _zc.ServiceInfo(type_, name)
265-
if not info.load_from_cache(zc):
266-
self._owner._maybe_log(
267-
_LogLevel.WARNING,
268-
"No cache entry for %s — device will appear on next mDNS announcement",
269-
name,
270-
)
271-
return None
272-
256+
def _parse(self, info: AsyncServiceInfo, name: str) -> tuple[str, str, int] | None:
257+
"""Return (mac, ip, port) from a resolved service record, or None."""
273258
addresses = info.parsed_addresses()
274259
if not addresses:
275-
self._owner._maybe_log(_LogLevel.WARNING, "No addresses in zeroconf cache record for %s", name)
260+
self._owner._maybe_log(_LogLevel.WARNING, "No addresses in zeroconf record for %s", name)
276261
return None
277262

278263
try:
@@ -289,35 +274,69 @@ def _extract(self, zc: Any, type_: str, name: str) -> tuple[str, str, int] | Non
289274
self._owner._maybe_log(_LogLevel.ERROR, "No port in zeroconf record for %s", name)
290275
return None
291276

292-
return raw_id.decode('utf-8'), addresses[0], info.port
277+
# This runs inside the browser callback, so a bad id from any
278+
# device advertising this service type must not raise.
279+
try:
280+
mac = raw_id.decode('utf-8')
281+
except UnicodeDecodeError:
282+
self._owner._maybe_log(_LogLevel.ERROR, "'id' property in zeroconf record for %s is not valid UTF-8", name)
283+
return None
293284

294-
def add_service(self, zc: Any, type_: str, name: str) -> None:
295-
result = self._extract(zc, type_, name)
296-
if result is None:
297-
self._owner._maybe_log(
298-
_LogLevel.WARNING,
299-
"add_service: no info available for %s — will retry on next announcement",
300-
name,
301-
)
302-
return
303-
mac, ip, port = result
304285
self._name_to_mac[name] = mac
305-
self._owner._internal_callback(self._owner._on_zc_add(mac, ip, port))
286+
return mac, addresses[0], info.port
306287

307-
def update_service(self, zc: Any, type_: str, name: str) -> None:
308-
result = self._extract(zc, type_, name)
309-
if result is None:
310-
self._owner._maybe_log(
311-
_LogLevel.WARNING,
312-
"update_service: no info available for %s — will retry on next announcement",
313-
name,
314-
)
288+
def _resolve(
289+
self,
290+
zc: _zc.Zeroconf,
291+
type_: str,
292+
name: str,
293+
on_resolved: Callable[[str, str, int], _InternalCallback],
294+
) -> None:
295+
"""Resolve a plug's address and port, then schedule on_resolved.
296+
297+
The records are usually already in the zeroconf cache but they are
298+
not guaranteed to be. A browser started on a shared zeroconf
299+
instance (as in Home Assistant when a config entry is reloaded) is
300+
seeded from cached PTR records, and the plug's SRV and A records
301+
have much shorter lifetimes, so they may have expired. Waiting for
302+
the plug to announce itself again can take a long time, so ask it
303+
directly instead, as Home Assistant's own zeroconf discovery does.
304+
"""
305+
# If a device advertises an invalid DNS name, the constructor will
306+
# reject it. This runs inside the browser callback, so ignore
307+
# invalid names rather than raise.
308+
try:
309+
info = AsyncServiceInfo(type_, name)
310+
except _zc.BadTypeInNameException:
311+
self._owner._maybe_log(_LogLevel.DEBUG, "Ignoring %s: not a valid service name", name)
315312
return
316-
mac, ip, port = result
317-
self._name_to_mac[name] = mac
318-
self._owner._internal_callback(self._owner._on_zc_update(mac, ip, port))
319313

320-
def remove_service(self, zc: Any, type_: str, name: str) -> None:
314+
if info.load_from_cache(zc):
315+
if (result := self._parse(info, name)) is not None:
316+
self._owner._internal_callback(on_resolved(*result))
317+
return
318+
319+
async def request() -> None:
320+
if not await info.async_request(zc, _REQUEST_TIMEOUT_MS):
321+
self._owner._maybe_log(
322+
_LogLevel.WARNING,
323+
"No response from %s — device will appear on next mDNS announcement",
324+
name,
325+
)
326+
return
327+
if (result := self._parse(info, name)) is not None:
328+
await on_resolved(*result)
329+
330+
self._owner._maybe_log(_LogLevel.DEBUG, "No cache entry for %s, requesting it", name)
331+
self._owner._internal_callback(request())
332+
333+
def add_service(self, zc: _zc.Zeroconf, type_: str, name: str) -> None:
334+
self._resolve(zc, type_, name, self._owner._on_zc_add)
335+
336+
def update_service(self, zc: _zc.Zeroconf, type_: str, name: str) -> None:
337+
self._resolve(zc, type_, name, self._owner._on_zc_update)
338+
339+
def remove_service(self, zc: _zc.Zeroconf, type_: str, name: str) -> None:
321340
mac = self._name_to_mac.pop(name, None)
322341
if mac is None:
323342
self._owner._maybe_log(

‎tests/test_zeroconf_devices.py‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import asyncio
2+
import logging
3+
from typing import Any, NoReturn
4+
from unittest.mock import AsyncMock, MagicMock, patch
5+
6+
import pytest
7+
import zeroconf
8+
from zeroconf.asyncio import AsyncServiceInfo
9+
10+
from powersensor_local import zeroconf_devices as zd
11+
from powersensor_local.zeroconf_devices import PowersensorZeroconfDevices
12+
13+
TYPE = '_powersensor._udp.local.'
14+
NAME = f'Powersensor-gateway-aabbccddeeff-civet.{TYPE}'
15+
16+
# The zeroconf instance is a MagicMock rather than a real Zeroconf. _Listener
17+
# only hands it to AsyncServiceInfo, which the tests replace, and the tests call
18+
# the listener directly instead of starting a browser, so nothing reads it. A
19+
# real instance would also open multicast sockets on port 5353 and send queries
20+
# onto the local network, making the tests slow and dependent on the host's
21+
# network setup.
22+
23+
24+
class FakeServiceInfo(AsyncServiceInfo):
25+
"""An AsyncServiceInfo with a controllable cache and plug response.
26+
27+
Keyword arguments override the plug's record, as passed to ServiceInfo.
28+
"""
29+
30+
def __init__(self, cached: bool, answers: bool, **record: Any) -> None:
31+
super().__init__(TYPE, NAME, **{
32+
'port': 49476,
33+
'properties': {b'id': b'aabbccddeeff'},
34+
'parsed_addresses': ['192.0.2.10'],
35+
**record,
36+
})
37+
self.cached = cached
38+
self.answers = answers
39+
self.requested = False
40+
41+
def load_from_cache(self, zc: zeroconf.Zeroconf, now: float | None = None) -> bool:
42+
return self.cached
43+
44+
async def async_request(
45+
self, zc: zeroconf.Zeroconf, timeout: float, *args: Any, **kwargs: Any,
46+
) -> bool:
47+
self.requested = True
48+
return self.answers
49+
50+
51+
async def settle(devices: PowersensorZeroconfDevices) -> None:
52+
"""Wait for the listener's internal tasks, including any they schedule."""
53+
for _ in range(10):
54+
await asyncio.sleep(0)
55+
if not devices._internal_callbacks:
56+
return
57+
await asyncio.gather(*list(devices._internal_callbacks))
58+
59+
60+
@pytest.mark.asyncio
61+
async def test_add_service_uses_cached_records() -> None:
62+
zc = MagicMock()
63+
devices = PowersensorZeroconfDevices(zeroconf_instance=zc)
64+
info = FakeServiceInfo(cached=True, answers=False)
65+
with patch.object(zd, 'AsyncServiceInfo', return_value=info), \
66+
patch.object(devices, '_on_zc_add', AsyncMock()) as on_add:
67+
zd._Listener(devices).add_service(zc, TYPE, NAME)
68+
await settle(devices)
69+
on_add.assert_awaited_once_with('aabbccddeeff', '192.0.2.10', 49476)
70+
assert not info.requested
71+
72+
73+
@pytest.mark.asyncio
74+
@pytest.mark.parametrize(
75+
('method', 'handler'),
76+
[('add_service', '_on_zc_add'), ('update_service', '_on_zc_update')],
77+
)
78+
async def test_records_missing_from_cache_are_requested(method: str, handler: str) -> None:
79+
# A browser started on a shared zeroconf instance is seeded from cached PTR
80+
# records, whose SRV and A records may already have expired.
81+
zc = MagicMock()
82+
devices = PowersensorZeroconfDevices(zeroconf_instance=zc)
83+
info = FakeServiceInfo(cached=False, answers=True)
84+
with patch.object(zd, 'AsyncServiceInfo', return_value=info), \
85+
patch.object(devices, handler, AsyncMock()) as on_resolved:
86+
getattr(zd._Listener(devices), method)(zc, TYPE, NAME)
87+
await settle(devices)
88+
assert info.requested
89+
on_resolved.assert_awaited_once_with('aabbccddeeff', '192.0.2.10', 49476)
90+
91+
92+
@pytest.mark.asyncio
93+
async def test_unanswered_request_is_logged(caplog: pytest.LogCaptureFixture) -> None:
94+
zc = MagicMock()
95+
devices = PowersensorZeroconfDevices(
96+
zeroconf_instance=zc, logger=logging.getLogger('test'),
97+
)
98+
info = FakeServiceInfo(cached=False, answers=False)
99+
with caplog.at_level(logging.WARNING), \
100+
patch.object(zd, 'AsyncServiceInfo', return_value=info), \
101+
patch.object(devices, '_on_zc_add', AsyncMock()) as on_add:
102+
zd._Listener(devices).add_service(zc, TYPE, NAME)
103+
await settle(devices)
104+
on_add.assert_not_awaited()
105+
assert f'No response from {NAME}' in caplog.text
106+
107+
108+
@pytest.mark.asyncio
109+
@pytest.mark.parametrize('cached', [True, False], ids=['cached', 'requested'])
110+
@pytest.mark.parametrize(
111+
('record', 'message'),
112+
[
113+
({'parsed_addresses': []}, 'No addresses in zeroconf record'),
114+
({'properties': {}}, "Missing 'id' property"),
115+
({'properties': {b'id': None}}, 'has no value'),
116+
({'port': None}, 'No port in zeroconf record'),
117+
({'properties': {b'id': b'\xff\xfe'}}, 'is not valid UTF-8'),
118+
],
119+
ids=['no-addresses', 'no-id', 'id-without-value', 'no-port', 'non-utf8-id'],
120+
)
121+
async def test_invalid_record_is_ignored(
122+
cached: bool, record: dict[str, Any], message: str,
123+
caplog: pytest.LogCaptureFixture,
124+
) -> None:
125+
zc = MagicMock()
126+
devices = PowersensorZeroconfDevices(
127+
zeroconf_instance=zc, logger=logging.getLogger('test'),
128+
)
129+
info = FakeServiceInfo(cached=cached, answers=True, **record)
130+
with caplog.at_level(logging.WARNING), \
131+
patch.object(zd, 'AsyncServiceInfo', return_value=info), \
132+
patch.object(devices, '_on_zc_add', AsyncMock()) as on_add:
133+
zd._Listener(devices).add_service(zc, TYPE, NAME)
134+
await settle(devices)
135+
on_add.assert_not_awaited()
136+
assert message in caplog.text
137+
138+
139+
# Browser callbacks must never raise (see _Listener): zeroconf does not catch
140+
# their exceptions, which would disrupt other listeners on a shared instance.
141+
142+
@pytest.mark.asyncio
143+
async def test_malformed_service_name_is_ignored() -> None:
144+
# Stands in for AsyncServiceInfo rejecting a name. The real constructor is
145+
# not used because, under coverage, zeroconf's compiled modules can raise a
146+
# copy of the exception class that is not zeroconf.BadTypeInNameException.
147+
def reject(type_: str, name: str) -> NoReturn:
148+
raise zeroconf.BadTypeInNameException
149+
150+
zc = MagicMock()
151+
devices = PowersensorZeroconfDevices(zeroconf_instance=zc)
152+
with patch.object(zd, 'AsyncServiceInfo', reject), \
153+
patch.object(devices, '_on_zc_add', AsyncMock()) as on_add:
154+
zd._Listener(devices).add_service(zc, TYPE, NAME)
155+
await settle(devices)
156+
on_add.assert_not_awaited()
157+
assert not devices._internal_callbacks
158+

0 commit comments

Comments
 (0)