Skip to content

Commit 9b959dc

Browse files
authored
Fail with descriptive errors when coordinator is disconnected (#203)
1 parent 36c5d09 commit 9b959dc

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

tests/api/test_request.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,13 @@ async def test_handling_unknown_bad_command_parsing(connected_znp):
318318

319319
with pytest.raises(ValueError):
320320
znp.frame_received(bad_frame)
321+
322+
323+
async def test_send_failure_when_disconnected(connected_znp):
324+
znp, _ = connected_znp
325+
znp._uart = None
326+
327+
with pytest.raises(RuntimeError) as e:
328+
await znp.request(c.SYS.Ping.Req())
329+
330+
assert "Coordinator is disconnected" in str(e.value)

tests/application/test_requests.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,3 +1009,29 @@ async def test_send_packet_failure(device, make_application, mocker):
10091009
assert excinfo.value.status == t.Status.MAC_NO_ACK
10101010

10111011
await app.shutdown()
1012+
1013+
1014+
@pytest.mark.parametrize("device", FORMED_DEVICES)
1015+
async def test_send_packet_failure_disconnected(device, make_application, mocker):
1016+
app, znp_server = await make_application(server_cls=device)
1017+
await app.startup(auto_form=False)
1018+
1019+
app._znp = None
1020+
1021+
packet = zigpy_t.ZigbeePacket(
1022+
src=zigpy_t.AddrModeAddress(addr_mode=zigpy_t.AddrMode.NWK, address=0x0000),
1023+
src_ep=0x9A,
1024+
dst=zigpy_t.AddrModeAddress(addr_mode=zigpy_t.AddrMode.NWK, address=0xEEFF),
1025+
dst_ep=0xBC,
1026+
tsn=0xDE,
1027+
profile_id=0x1234,
1028+
cluster_id=0x0006,
1029+
data=zigpy_t.SerializableBytes(b"test data"),
1030+
)
1031+
1032+
with pytest.raises(zigpy.exceptions.DeliveryError) as excinfo:
1033+
await app.send_packet(packet)
1034+
1035+
assert "Coordinator is disconnected" in str(excinfo.value)
1036+
1037+
await app.shutdown()

zigpy_znp/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,9 @@ async def request(
969969

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

972+
if self._uart is None:
973+
raise RuntimeError("Coordinator is disconnected, cannot send request")
974+
972975
# We should only be sending one SREQ at a time, according to the spec
973976
async with self._sync_request_lock:
974977
LOGGER.debug("Sending request: %s", request)

zigpy_znp/zigbee/application.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,9 @@ async def _send_request_raw(
839839
Data=data,
840840
)
841841

842+
if self._znp is None:
843+
raise DeliveryError("Coordinator is disconnected, cannot send request")
844+
842845
# Z-Stack requires special treatment when sending ZDO requests
843846
if dst_ep == ZDO_ENDPOINT:
844847
# XXX: Joins *must* be sent via a ZDO command, even if they are directly

0 commit comments

Comments
 (0)