Skip to content

Commit fca4593

Browse files
committed
additional coverage
1 parent ba14fd2 commit fca4593

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

tests/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,11 @@ def __init__(
344344
self.zha_data = zha_data
345345
self.ws_gateway: WebSocketServerGateway = ws_gateway
346346
self.client_gateway: WebSocketClientGateway = client_gateway
347-
self.application_controller: ControllerApplication = (
348-
self.ws_gateway.application_controller
349-
)
347+
348+
@property
349+
def application_controller(self) -> ControllerApplication:
350+
"""Return the Zigpy application controller."""
351+
return self.ws_gateway.application_controller
350352

351353
@property
352354
def config(self) -> ZHAData:

tests/websocket/test_client_controller.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ async def test_ws_client_gateway_devices(
183183
await zha_gateway.async_block_till_done()
184184
assert len(ws_client_gateway.devices) == 1
185185

186+
# lets kill the network and then start it back up to make sure everything is still in working order
187+
await ws_client_gateway.network.stop_network()
188+
189+
assert zha_gateway.application_controller is None
190+
191+
await ws_client_gateway.network.start_network()
192+
193+
assert zha_gateway.application_controller is not None
194+
186195
# let's add it back
187196
zha_device = await join_zigpy_device(zha_gateway, zigpy_device)
188197
await zha_gateway.async_block_till_done()
@@ -313,6 +322,17 @@ async def test_ws_client_gateway_devices(
313322
)
314323
)
315324

325+
# test topology scan
326+
zha_gateway.application_controller.topology.scan = AsyncMock()
327+
await ws_client_gateway.network.update_topology()
328+
assert zha_gateway.application_controller.topology.scan.await_count == 1
329+
330+
# test permit join
331+
zha_gateway.application_controller.permit = AsyncMock()
332+
await ws_client_gateway.network.permit_joining(60)
333+
assert zha_gateway.application_controller.permit.await_count == 1
334+
assert zha_gateway.application_controller.permit.await_args == call(60, None)
335+
316336

317337
@pytest.mark.parametrize(
318338
"zha_gateway",

zha/application/gateway.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ async def async_remove_zigpy_group(self, group_id: int) -> None:
757757
await asyncio.gather(*tasks)
758758
self.application_controller.groups.pop(group_id)
759759

760-
async def shutdown(self) -> None:
760+
async def shutdown(self, call_super=True) -> None:
761761
"""Stop ZHA Controller Application."""
762762
if self.shutting_down:
763763
_LOGGER.debug("Ignoring duplicate shutdown event")
@@ -780,7 +780,8 @@ async def shutdown(self) -> None:
780780
self.application_controller = None
781781
await asyncio.sleep(0.1) # give bellows thread callback a chance to run
782782

783-
await super().shutdown()
783+
if call_super:
784+
await super().shutdown()
784785

785786
self._devices.clear()
786787
self._groups.clear()
@@ -859,6 +860,15 @@ async def stop_server(self) -> None:
859860

860861
self._stopped_event.set()
861862

863+
async def start_network(self) -> None:
864+
"""Start the Zigbee network."""
865+
await super().async_initialize() # we do this to avoid 2x event registration
866+
await self.async_initialize_devices_and_entities()
867+
868+
async def stop_network(self) -> None:
869+
"""Stop the Zigbee network."""
870+
await self.shutdown(call_super=False)
871+
862872
async def wait_closed(self) -> None:
863873
"""Wait until the server is not running."""
864874
await self._stopped_event.wait()

0 commit comments

Comments
 (0)