Skip to content

Commit 65e6722

Browse files
committed
client api coverage
1 parent 2d5d949 commit 65e6722

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

tests/websocket/test_websocket_server_client.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from zha.application.gateway import WebSocketClientGateway, WebSocketServerGateway
99
from zha.application.helpers import ZHAData
1010
from zha.websocket.client.client import Client
11+
from zha.websocket.client.helpers import ClientHelper
1112

1213

1314
async def test_server_client_connect_disconnect(
@@ -44,6 +45,36 @@ async def test_server_client_connect_disconnect(
4445
assert gateway._ws_server is None
4546

4647

48+
async def test_client_helper_disconnect(
49+
zha_data: ZHAData,
50+
) -> None:
51+
"""Tests client helper disconnect logic."""
52+
53+
async with WebSocketServerGateway(zha_data) as gateway:
54+
assert gateway.is_serving
55+
assert gateway._ws_server is not None
56+
57+
client = Client(f"ws://localhost:{zha_data.ws_server_config.port}")
58+
client_helper = ClientHelper(client)
59+
60+
await client.connect()
61+
assert client.connected
62+
assert "connected" in repr(client)
63+
64+
# The client does not begin listening immediately
65+
assert client._listen_task is None
66+
await client_helper.listen()
67+
assert client._listen_task is not None
68+
69+
await client_helper.disconnect()
70+
assert client._listen_task is None
71+
assert "not connected" in repr(client)
72+
assert not client.connected
73+
74+
assert not gateway.is_serving
75+
assert gateway._ws_server is None
76+
77+
4778
@pytest.mark.parametrize(
4879
"zha_gateway",
4980
[

zha/application/gateway.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,15 +969,14 @@ async def connect(self) -> None:
969969
_LOGGER.exception("Unable to connect to the ZHA wss", exc_info=err)
970970
raise err
971971

972-
await self._client.listen()
973-
974972
async def disconnect(self) -> None:
975973
"""Disconnect from the websocket server."""
976974
await self._client.disconnect()
977975

978976
async def __aenter__(self) -> WebSocketClientGateway:
979977
"""Connect to the websocket server."""
980978
await self.connect()
979+
await self.clients.listen()
981980
return self
982981

983982
async def __aexit__(

zha/websocket/client/helpers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,17 +804,19 @@ def __init__(self, client: Client):
804804
async def listen(self) -> WebSocketCommandResponse:
805805
"""Listen for incoming messages."""
806806
command = ClientListenCommand()
807+
await self._client.listen()
807808
return await self._client.async_send_command(command)
808809

809810
async def listen_raw_zcl(self) -> WebSocketCommandResponse:
810811
"""Listen for incoming raw ZCL messages."""
811812
command = ClientListenRawZCLCommand()
812813
return await self._client.async_send_command(command)
813814

814-
async def disconnect(self) -> WebSocketCommandResponse:
815+
async def disconnect(self) -> None:
815816
"""Disconnect this client from the server."""
816817
command = ClientDisconnectCommand()
817-
return await self._client.async_send_command(command)
818+
await self._client.async_send_command_no_wait(command)
819+
await self._client.disconnect()
818820

819821

820822
class GroupHelper:

0 commit comments

Comments
 (0)