From 846130eb9c8150d8465d4647766756a1fb5de26d Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Mon, 20 Nov 2023 17:40:43 +0300 Subject: [PATCH] Don't fail on energy scan with legacy modules (#166) * Don't fail on energy scan with legacy modules * Use new InvalidCommand exception --- tests/test_application.py | 14 ++++++++++++++ zigpy_xbee/zigbee/application.py | 7 ++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/test_application.py b/tests/test_application.py index 4ecb643..d70373d 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -731,6 +731,20 @@ async def test_energy_scan(app): } +async def test_energy_scan_legacy_module(app): + """Test channel energy scan.""" + app._api._at_command = mock.AsyncMock( + spec=XBee._at_command, side_effect=InvalidCommand + ) + time_s = 3 + count = 3 + energy = await app.energy_scan( + channels=list(range(11, 27)), duration_exp=time_s, count=count + ) + app._api._at_command.assert_called_once_with("ED", bytes([time_s])) + assert energy == {c: 0 for c in range(11, 27)} + + def test_neighbors_updated(app, device): """Test LQI from neighbour scan.""" router = device(ieee=b"\x01\x02\x03\x04\x05\x06\x07\x08") diff --git a/zigpy_xbee/zigbee/application.py b/zigpy_xbee/zigbee/application.py index 18969ac..dad1ae0 100644 --- a/zigpy_xbee/zigbee/application.py +++ b/zigpy_xbee/zigbee/application.py @@ -197,7 +197,12 @@ async def energy_scan( all_results = {} for _ in range(count): - results = await self._api._at_command("ED", bytes([duration_exp])) + try: + results = await self._api._at_command("ED", bytes([duration_exp])) + except InvalidCommand: + LOGGER.warning("Coordinator does not support energy scanning") + return {c: 0 for c in channels} + results = { channel: -int(rssi) for channel, rssi in zip(range(11, 27), results) }