Skip to content

Commit

Permalink
Handle invalid commands without throwing an exception (#1336)
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Feb 6, 2024
1 parent 43b3649 commit 7877af5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
15 changes: 14 additions & 1 deletion tests/test_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ async def test_listener_callback_no_matches():
assert listener.callback.mock_calls == []


async def test_listener_callback_invalid(caplog):
async def test_listener_callback_invalid_matcher(caplog):
listener = listeners.CallbackListener(
matchers=[object()],
callback=mock.Mock(),
Expand All @@ -164,6 +164,19 @@ async def test_listener_callback_invalid(caplog):
assert f"Matcher {listener.matchers[0]!r} and command" in caplog.text


async def test_listener_callback_invalid_call(caplog):
listener = listeners.CallbackListener(
matchers=[on()],
callback=mock.Mock(),
)

with caplog.at_level(logging.WARNING):
assert not listener.resolve(make_hdr(on()), b"data")

assert listener.callback.mock_calls == []
assert f"Matcher {listener.matchers[0]!r} and command" in caplog.text


async def test_listener_callback_zdo(caplog):
listener = listeners.CallbackListener(
matchers=[
Expand Down
15 changes: 8 additions & 7 deletions zigpy/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ def resolve(
"""

for matcher in self.matchers:
if isinstance(matcher, foundation.CommandSchema):
if isinstance(hdr, zdo_t.ZDOHeader):
# FIXME: ZDO does not use command schemas and cannot be matched
continue
elif isinstance(command, foundation.CommandSchema):
match = command.matches(matcher)
match = None
is_matcher_cmd = isinstance(matcher, foundation.CommandSchema)

if is_matcher_cmd and isinstance(command, foundation.CommandSchema):
match = command.matches(matcher)
elif is_matcher_cmd and isinstance(hdr, zdo_t.ZDOHeader):
# FIXME: ZDO does not use command schemas and cannot be matched
pass
elif callable(matcher):
match = matcher(hdr, command)
else:
match = None
LOGGER.warning(
"Matcher %r and command %r %r are incompatible",
matcher,
Expand Down

0 comments on commit 7877af5

Please sign in to comment.