Skip to content

Commit aaf0c31

Browse files
committed
Allow deserialization failures with some commands.
See home-assistant/core#50005
1 parent a571726 commit aaf0c31

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

tests/api/test_request.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,36 @@ async def test_znp_unknown_frame(connected_znp, caplog):
285285

286286
# Unknown frames are logged in their entirety but an error is not thrown
287287
assert repr(frame) in caplog.text
288+
289+
290+
async def test_handling_known_bad_command_parsing(connected_znp, caplog):
291+
znp, _ = connected_znp
292+
293+
bad_frame = GeneralFrame(
294+
header=t.CommandHeader(
295+
id=0x9F, subsystem=t.Subsystem.ZDO, type=t.CommandType.AREQ
296+
),
297+
data=b"\x13\xDB\x84\x01\x21",
298+
)
299+
300+
caplog.set_level(logging.WARNING)
301+
znp.frame_received(bad_frame)
302+
303+
# The frame is expected to fail to parse so will be logged as only a warning
304+
assert len(caplog.records) == 1
305+
assert caplog.records[0].levelname == "WARNING"
306+
assert repr(bad_frame) in caplog.messages[0]
307+
308+
309+
async def test_handling_unknown_bad_command_parsing(connected_znp):
310+
znp, _ = connected_znp
311+
312+
bad_frame = GeneralFrame(
313+
header=t.CommandHeader(
314+
id=0xCB, subsystem=t.Subsystem.ZDO, type=t.CommandType.AREQ
315+
),
316+
data=b"\x13\xDB\x84\x01\x21",
317+
)
318+
319+
with pytest.raises(ValueError):
320+
znp.frame_received(bad_frame)

zigpy_znp/api.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,17 @@ def frame_received(self, frame: GeneralFrame) -> bool:
358358
return
359359

360360
command_cls = c.COMMANDS_BY_ID[frame.header]
361-
command = command_cls.from_frame(frame, align=self.nvram.align_structs)
361+
362+
try:
363+
command = command_cls.from_frame(frame, align=self.nvram.align_structs)
364+
except ValueError:
365+
# Some commands can be received corrupted. They are not useful:
366+
# https://github.com/home-assistant/core/issues/50005
367+
if command_cls == c.ZDO.ParentAnnceRsp.Callback:
368+
LOGGER.warning("Failed to parse broken %s as %s", frame, command_cls)
369+
return
370+
371+
raise
362372

363373
LOGGER.debug("Received command: %s", command)
364374

0 commit comments

Comments
 (0)