Skip to content

Fail with descriptive errors when coordinator is disconnected #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/api/test_request.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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