Skip to content

Commit 9dce57e

Browse files
committed
Increase diff test coverage
1 parent b545d07 commit 9dce57e

File tree

4 files changed

+97
-15
lines changed

4 files changed

+97
-15
lines changed

tests/api/test_network_state.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44

5+
import zigpy_znp.types as t
56
from zigpy_znp.types.nvids import ExNvIds, OsalNvIds
67

78
from ..conftest import (
@@ -64,3 +65,30 @@ async def test_broken_cc2531_load_state(device, make_connected_znp, caplog):
6465
assert "inconsistent" in caplog.text
6566

6667
znp.close()
68+
69+
70+
@pytest.mark.parametrize("device", [FormedZStack3CC2531])
71+
async def test_state_write_tclk_zstack3(device, make_connected_znp, caplog):
72+
formed_znp, _ = await make_connected_znp(server_cls=device)
73+
74+
await formed_znp.load_network_info()
75+
formed_znp.close()
76+
77+
empty_znp, _ = await make_connected_znp(server_cls=device)
78+
79+
caplog.set_level(logging.WARNING)
80+
await empty_znp.write_network_info(
81+
network_info=formed_znp.network_info.replace(
82+
tc_link_key=formed_znp.network_info.tc_link_key.replace(
83+
# Non-standard TCLK
84+
key=t.KeyData.convert("AA:BB:CC:DD:AA:BB:CC:DD:AA:BB:CC:DD:AA:BB:CC:DD")
85+
)
86+
),
87+
node_info=formed_znp.node_info,
88+
)
89+
assert "TC link key is configured at build time in Z-Stack 3" in caplog.text
90+
91+
await empty_znp.load_network_info()
92+
93+
# TCLK was not changed
94+
assert formed_znp.network_info == empty_znp.network_info

tests/application/test_connect.py

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async def test_probe_unsuccessful():
5858

5959

6060
@pytest.mark.parametrize("device", FORMED_DEVICES)
61-
async def test_probe_unsuccessful_slow(device, make_znp_server, mocker):
61+
async def test_probe_unsuccessful_slow1(device, make_znp_server, mocker):
6262
znp_server = make_znp_server(server_cls=device, shorten_delays=False)
6363

6464
# Don't respond to anything
@@ -75,6 +75,24 @@ async def test_probe_unsuccessful_slow(device, make_znp_server, mocker):
7575
assert not any([t._is_connected for t in znp_server._transports])
7676

7777

78+
@pytest.mark.parametrize("device", FORMED_DEVICES)
79+
async def test_probe_unsuccessful_slow2(device, make_znp_server, mocker):
80+
znp_server = make_znp_server(server_cls=device, shorten_delays=False)
81+
82+
# Don't respond to anything
83+
znp_server._listeners.clear()
84+
85+
mocker.patch("zigpy_znp.zigbee.application.PROBE_TIMEOUT", new=0.1)
86+
87+
assert not (
88+
await ControllerApplication.probe(
89+
conf.SCHEMA_DEVICE({conf.CONF_DEVICE_PATH: znp_server.serial_port})
90+
)
91+
)
92+
93+
assert not any([t._is_connected for t in znp_server._transports])
94+
95+
7896
@pytest.mark.parametrize("device", FORMED_DEVICES)
7997
async def test_probe_successful(device, make_znp_server):
8098
znp_server = make_znp_server(server_cls=device, shorten_delays=False)
@@ -101,7 +119,7 @@ async def test_probe_multiple(device, make_znp_server):
101119

102120

103121
@pytest.mark.parametrize("device", FORMED_DEVICES)
104-
async def test_reconnect(device, event_loop, make_application):
122+
async def test_reconnect(device, make_application):
105123
app, znp_server = await make_application(
106124
server_cls=device,
107125
client_config={
@@ -180,7 +198,7 @@ async def test_multiple_shutdown(make_application):
180198

181199

182200
@pytest.mark.parametrize("device", FORMED_DEVICES)
183-
async def test_reconnect_lockup(device, event_loop, make_application, mocker):
201+
async def test_reconnect_lockup(device, make_application, mocker):
184202
mocker.patch("zigpy_znp.zigbee.application.WATCHDOG_PERIOD", 0.1)
185203

186204
app, znp_server = await make_application(
@@ -221,7 +239,7 @@ async def test_reconnect_lockup(device, event_loop, make_application, mocker):
221239

222240

223241
@pytest.mark.parametrize("device", [FormedLaunchpadCC26X2R1])
224-
async def test_reconnect_lockup_pyserial(device, event_loop, make_application, mocker):
242+
async def test_reconnect_lockup_pyserial(device, make_application, mocker):
225243
mocker.patch("zigpy_znp.zigbee.application.WATCHDOG_PERIOD", 0.1)
226244

227245
app, znp_server = await make_application(
@@ -271,3 +289,49 @@ async def patched_start_network(old_start_network=app.start_network, **kwargs):
271289
assert app._znp and app._znp._uart
272290

273291
await app.shutdown()
292+
293+
294+
@pytest.mark.parametrize("device", [FormedLaunchpadCC26X2R1])
295+
async def test_disconnect(device, make_application):
296+
app, znp_server = await make_application(
297+
server_cls=device,
298+
client_config={
299+
conf.CONF_ZNP_CONFIG: {
300+
conf.CONF_SREQ_TIMEOUT: 0.1,
301+
}
302+
},
303+
)
304+
305+
assert app._znp is None
306+
await app.connect()
307+
308+
assert app._znp is not None
309+
310+
await app.disconnect()
311+
assert app._znp is None
312+
313+
await app.disconnect()
314+
await app.disconnect()
315+
316+
317+
@pytest.mark.parametrize("device", [FormedLaunchpadCC26X2R1])
318+
async def test_disconnect_failure(device, make_application):
319+
app, znp_server = await make_application(
320+
server_cls=device,
321+
client_config={
322+
conf.CONF_ZNP_CONFIG: {
323+
conf.CONF_SREQ_TIMEOUT: 0.1,
324+
}
325+
},
326+
)
327+
328+
assert app._znp is None
329+
await app.connect()
330+
331+
assert app._znp is not None
332+
333+
with patch.object(app._znp, "reset", side_effect=RuntimeError("An error")):
334+
# Runs without error
335+
await app.disconnect()
336+
337+
assert app._znp is None

zigpy_znp/types/commands.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -471,15 +471,7 @@ def as_dict(self) -> dict[str, typing.Any]:
471471
Converts the command into a dictionary.
472472
"""
473473

474-
result = {}
475-
476-
for param, value in self._bound_params.values():
477-
if issubclass(param.type, t.CStruct):
478-
result[param.name] = value.as_dict()
479-
else:
480-
result[param.name] = value
481-
482-
return result
474+
return {p.name: v for p, v in self._bound_params.values()}
483475

484476
def __eq__(self, other):
485477
return type(self) is type(other) and self._bound_params == other._bound_params

zigpy_znp/zigbee/application.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ async def disconnect(self):
123123
if self._znp is not None:
124124
try:
125125
await self._znp.reset(wait_for_reset=False)
126-
except asyncio.CancelledError:
127-
raise
128126
except Exception as e:
129127
LOGGER.warning("Failed to reset before disconnect: %s", e)
130128
finally:

0 commit comments

Comments
 (0)