From a5bfa4b2eed5cd8c9ce63022db0c5b17b16d81a9 Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Thu, 16 Jan 2025 12:03:11 -0500 Subject: [PATCH] Add a unit test --- bellows/zigbee/application.py | 3 + tests/test_application.py | 101 ++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/bellows/zigbee/application.py b/bellows/zigbee/application.py index 03ad9ad9..068034a6 100644 --- a/bellows/zigbee/application.py +++ b/bellows/zigbee/application.py @@ -730,6 +730,9 @@ async def _network_scan( duration=duration_exp, ) + if t.sl_Status.from_ember_status(status) != t.sl_Status.OK: + raise ControllerError(f"Failed to start scan: {status!r}") + while True: command, response = await queue.get() diff --git a/tests/test_application.py b/tests/test_application.py index c3931c3e..873f6921 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -1943,3 +1943,104 @@ async def test_write_network_info( ) ) ] + + +async def test_network_scan(app: ControllerApplication) -> None: + app._ezsp._protocol.startScan.return_value = [t.sl_Status.OK] + + def run_scan() -> None: + app._ezsp._protocol._handle_callback( + "networkFoundHandler", + list( + { + "networkFound": t.EmberZigbeeNetwork( + channel=11, + panId=zigpy_t.PanId(0x1D13), + extendedPanId=t.EUI64.convert("00:07:81:00:00:9a:8f:3b"), + allowingJoin=False, + stackProfile=2, + nwkUpdateId=0, + ), + "lastHopLqi": 152, + "lastHopRssi": -62, + }.values() + ), + ) + app._ezsp._protocol._handle_callback( + "networkFoundHandler", + list( + { + "networkFound": t.EmberZigbeeNetwork( + channel=11, + panId=zigpy_t.PanId(0x2857), + extendedPanId=t.EUI64.convert("00:07:81:00:00:9a:34:1b"), + allowingJoin=False, + stackProfile=2, + nwkUpdateId=0, + ), + "lastHopLqi": 136, + "lastHopRssi": -66, + }.values() + ), + ) + app._ezsp._protocol._handle_callback( + "scanCompleteHandler", + list( + { + "channel": 26, + "status": t.sl_Status.OK, + }.values() + ), + ) + + asyncio.get_running_loop().call_soon(run_scan) + + results = [ + beacon + async for beacon in app.network_scan( + channels=t.Channels.from_channel_list([11, 15, 26]), duration_exp=4 + ) + ] + + assert results == [ + zigpy_t.NetworkBeacon( + pan_id=0x1D13, + extended_pan_id=t.EUI64.convert("00:07:81:00:00:9a:8f:3b"), + channel=11, + permit_joining=False, + stack_profile=2, + nwk_update_id=0, + lqi=152, + src=None, + rssi=-62, + depth=None, + router_capacity=None, + device_capacity=None, + protocol_version=None, + ), + zigpy_t.NetworkBeacon( + pan_id=0x2857, + extended_pan_id=t.EUI64.convert("00:07:81:00:00:9a:34:1b"), + channel=11, + permit_joining=False, + stack_profile=2, + nwk_update_id=0, + lqi=136, + src=None, + rssi=-66, + depth=None, + router_capacity=None, + device_capacity=None, + protocol_version=None, + ), + ] + + +async def test_network_scan_failure(app: ControllerApplication) -> None: + app._ezsp._protocol.startScan.return_value = [t.sl_Status.FAIL] + + with pytest.raises(zigpy.exceptions.ControllerException): + async for beacon in app.network_scan( + channels=t.Channels.from_channel_list([11, 15, 26]), duration_exp=4 + ): + pass