Skip to content

Commit 2b338f6

Browse files
committed
fix: always stop the event loop when disconnecting
1 parent 590f347 commit 2b338f6

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

roborock/cloud_api.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ def __init__(self, user_data: UserData, device_info: DeviceData, queue_timeout:
7474
self._waiting_queue: dict[int, RoborockFuture] = {}
7575
self._mutex = Lock()
7676

77-
async def async_release(self) -> None:
78-
"""Release the MQTT client."""
79-
await super().async_release()
80-
await self.event_loop.run_in_executor(None, self._mqtt_client.loop_stop)
81-
8277
def _mqtt_on_connect(self, *args, **kwargs):
8378
_, __, ___, rc, ____ = args
8479
connection_queue = self._waiting_queue.get(CONNECT_REQUEST_ID)
@@ -161,10 +156,9 @@ def sync_connect(self) -> Any:
161156
async def async_disconnect(self) -> None:
162157
async with self._mutex:
163158
if disconnected_future := self.sync_disconnect():
164-
try:
165-
await disconnected_future
166-
except VacuumError as err:
167-
raise RoborockException(err) from err
159+
# There are no errors set on this future
160+
await disconnected_future
161+
await self.event_loop.run_in_executor(None, self._mqtt_client.loop_stop)
168162

169163
async def async_connect(self) -> None:
170164
async with self._mutex:

tests/test_api.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import json
3+
import logging
34
from collections.abc import AsyncGenerator
45
from queue import Queue
56
from typing import Any
@@ -168,7 +169,7 @@ async def test_async_connect(received_requests: Queue, connected_mqtt_client: Ro
168169
assert received_requests.qsize() >= 2 # Connect and Subscribe
169170

170171

171-
async def test_connect_failure(
172+
async def test_connect_failure_response(
172173
received_requests: Queue, response_queue: Queue, mqtt_client: RoborockMqttClientV1
173174
) -> None:
174175
"""Test the broker responding with a connect failure."""
@@ -203,6 +204,24 @@ async def test_disconnect_failure(connected_mqtt_client: RoborockMqttClientV1) -
203204
await connected_mqtt_client.async_disconnect()
204205

205206

207+
async def test_disconnect_failure_response(
208+
received_requests: Queue,
209+
response_queue: Queue,
210+
connected_mqtt_client: RoborockMqttClientV1,
211+
caplog: pytest.LogCaptureFixture,
212+
) -> None:
213+
"""Test the broker responding with a connect failure."""
214+
215+
# Enqueue a failed message -- however, the client does not process any
216+
# further messages and there is no parsing error, and no failed log messages.
217+
response_queue.put(mqtt_packet.gen_disconnect(reason_code=1))
218+
assert connected_mqtt_client.is_connected()
219+
with caplog.at_level(logging.ERROR, logger="homeassistant.components.nest"):
220+
await connected_mqtt_client.async_disconnect()
221+
assert not connected_mqtt_client.is_connected()
222+
assert not caplog.records
223+
224+
206225
async def test_async_release(connected_mqtt_client: RoborockMqttClientV1) -> None:
207226
"""Test the async_release API will disconnect the client."""
208227
await connected_mqtt_client.async_release()

0 commit comments

Comments
 (0)