From 1a39f16652d45090d5d1742423f7841327469364 Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Mon, 10 Oct 2022 15:33:36 -0400 Subject: [PATCH] Fix group request exception (#138) --- tests/test_application.py | 21 ++++++++++++++++----- zigpy_zigate/api.py | 5 ++--- zigpy_zigate/zigbee/application.py | 4 ++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/tests/test_application.py b/tests/test_application.py index 025981d..c965217 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -2,11 +2,12 @@ import pytest import logging -import zigpy.types as zigpy_types +import zigpy.types as zigpy_t import zigpy.exceptions -import zigpy_zigate.config as config +import zigpy_zigate.api import zigpy_zigate.types as t +import zigpy_zigate.config as config import zigpy_zigate.zigbee.application APP_CONFIG = zigpy_zigate.zigbee.application.ControllerApplication.SCHEMA( @@ -22,7 +23,7 @@ def app(): a = zigpy_zigate.zigbee.application.ControllerApplication(APP_CONFIG) a.version = FAKE_FIRMWARE_VERSION - a._api = MagicMock() + a._api = MagicMock(spec_set=zigpy_zigate.api.ZiGate) return a @@ -32,7 +33,7 @@ def test_zigpy_ieee(app): data = b"\x01\x02\x03\x04\x05\x06\x07\x08" zigate_ieee, _ = t.EUI64.deserialize(data) - app.state.node_info.ieee = zigpy_types.EUI64(zigate_ieee) + app.state.node_info.ieee = zigpy_t.EUI64(zigate_ieee) dst_addr = app.get_dst_address(cluster) assert dst_addr.serialize() == b"\x03" + data[::-1] + b"\x01" @@ -148,4 +149,14 @@ async def test_startup_connect(zigate_new, app, version_rsp, expected_version): await app.connect() - assert app.version == expected_version \ No newline at end of file + assert app.version == expected_version + + +@pytest.mark.asyncio +async def test_send_group_request(app): + packet = zigpy_t.ZigbeePacket(src=None, src_ep=1, dst=zigpy_t.AddrModeAddress(addr_mode=zigpy_t.AddrMode.Group, address=0x0002), dst_ep=None, source_route=None, extended_timeout=False, tsn=21, profile_id=260, cluster_id=6, data=zigpy_t.SerializableBytes(b'\x01\x15\x00'), tx_options=zigpy_t.TransmitOptions.NONE, radius=0, non_member_radius=3, lqi=None, rssi=None) + + app._api.raw_aps_data_request.return_value = ([t.Status.Success, 0, 1328, b'\x01\xea\x00\x00'], 0) + await app.send_packet(packet) + + app._api.raw_aps_data_request.assert_called_once() diff --git a/zigpy_zigate/api.py b/zigpy_zigate/api.py index 3f0aacc..aff1ace 100644 --- a/zigpy_zigate/api.py +++ b/zigpy_zigate/api.py @@ -290,13 +290,12 @@ def set_application(self, app): self._app = app def data_received(self, cmd, data, lqi): - LOGGER.debug("data received %s %s LQI:%s", hex(cmd), - binascii.hexlify(data), lqi) if cmd not in RESPONSES: - LOGGER.warning('Received unhandled response 0x%04x', cmd) + LOGGER.warning('Received unhandled response 0x%04x: %r', cmd, binascii.hexlify(data)) return cmd = ResponseId(cmd) data, rest = t.deserialize(data, RESPONSES[cmd]) + LOGGER.debug("Response received: %s %s %s (LQI:%s)", cmd, data, rest, lqi) if cmd == ResponseId.STATUS: if data[2] in self._status_awaiting: fut = self._status_awaiting.pop(data[2]) diff --git a/zigpy_zigate/zigbee/application.py b/zigpy_zigate/zigbee/application.py index 0686909..8dedfbd 100644 --- a/zigpy_zigate/zigbee/application.py +++ b/zigpy_zigate/zigbee/application.py @@ -243,8 +243,8 @@ async def send_packet(self, packet): try: (status, tsn, packet_type, _), _ = await self._api.raw_aps_data_request( addr=packet.dst.address, - src_ep=(1 if packet.dst_ep > 0 else 0), # ZiGate only support endpoint 1 - dst_ep=packet.dst_ep, + src_ep=(1 if packet.dst_ep is None or packet.dst_ep > 0 else 0), # ZiGate only support endpoint 1 + dst_ep=packet.dst_ep or 0, profile=packet.profile_id, cluster=packet.cluster_id, payload=packet.data.serialize(),