Skip to content

Commit

Permalink
Fail with descriptive errors when coordinator is disconnected (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Feb 13, 2023
1 parent 36c5d09 commit 9b959dc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/api/test_request.py
Expand Up @@ -318,3 +318,13 @@ async def test_handling_unknown_bad_command_parsing(connected_znp):

with pytest.raises(ValueError):
znp.frame_received(bad_frame)


async def test_send_failure_when_disconnected(connected_znp):
znp, _ = connected_znp
znp._uart = None

with pytest.raises(RuntimeError) as e:
await znp.request(c.SYS.Ping.Req())

assert "Coordinator is disconnected" in str(e.value)
26 changes: 26 additions & 0 deletions tests/application/test_requests.py
Expand Up @@ -1009,3 +1009,29 @@ async def test_send_packet_failure(device, make_application, mocker):
assert excinfo.value.status == t.Status.MAC_NO_ACK

await app.shutdown()


@pytest.mark.parametrize("device", FORMED_DEVICES)
async def test_send_packet_failure_disconnected(device, make_application, mocker):
app, znp_server = await make_application(server_cls=device)
await app.startup(auto_form=False)

app._znp = None

packet = zigpy_t.ZigbeePacket(
src=zigpy_t.AddrModeAddress(addr_mode=zigpy_t.AddrMode.NWK, address=0x0000),
src_ep=0x9A,
dst=zigpy_t.AddrModeAddress(addr_mode=zigpy_t.AddrMode.NWK, address=0xEEFF),
dst_ep=0xBC,
tsn=0xDE,
profile_id=0x1234,
cluster_id=0x0006,
data=zigpy_t.SerializableBytes(b"test data"),
)

with pytest.raises(zigpy.exceptions.DeliveryError) as excinfo:
await app.send_packet(packet)

assert "Coordinator is disconnected" in str(excinfo.value)

await app.shutdown()
3 changes: 3 additions & 0 deletions zigpy_znp/api.py
Expand Up @@ -969,6 +969,9 @@ async def request(

frame = request.to_frame(align=self.nvram.align_structs)

if self._uart is None:
raise RuntimeError("Coordinator is disconnected, cannot send request")

# We should only be sending one SREQ at a time, according to the spec
async with self._sync_request_lock:
LOGGER.debug("Sending request: %s", request)
Expand Down
3 changes: 3 additions & 0 deletions zigpy_znp/zigbee/application.py
Expand Up @@ -839,6 +839,9 @@ async def _send_request_raw(
Data=data,
)

if self._znp is None:
raise DeliveryError("Coordinator is disconnected, cannot send request")

# Z-Stack requires special treatment when sending ZDO requests
if dst_ep == ZDO_ENDPOINT:
# XXX: Joins *must* be sent via a ZDO command, even if they are directly
Expand Down

0 comments on commit 9b959dc

Please sign in to comment.