22from queue import Queue
33from typing import Any
44from unittest .mock import patch
5+ from collections .abc import AsyncGenerator
56
67import paho .mqtt .client as mqtt
78import pytest
@@ -145,19 +146,28 @@ async def test_get_prop():
145146 assert props .dock_summary .dust_collection_mode is not None
146147
147148
149+ @pytest .fixture (name = "connected_mqtt_client" )
150+ async def connected_mqtt_client_fixture (response_queue : Queue , mqtt_client : RoborockMqttClientV1 ) -> AsyncGenerator [RoborockMqttClientV1 , None ]:
151+ response_queue .put (mqtt_packet .gen_connack (rc = 0 , flags = 2 ))
152+ response_queue .put (mqtt_packet .gen_suback (1 , 0 ))
153+ await mqtt_client .async_connect ()
154+ yield mqtt_client
155+ if mqtt_client .is_connected ():
156+ await mqtt_client .async_disconnect ()
157+
158+
148159async def test_async_connect (
149- received_requests : Queue , response_queue : Queue , mqtt_client : RoborockMqttClientV1
160+ received_requests : Queue , connected_mqtt_client : RoborockMqttClientV1
150161) -> None :
151162 """Test connecting to the MQTT broker."""
152163
153- response_queue .put (mqtt_packet .gen_connack (rc = 0 , flags = 2 ))
154- response_queue .put (mqtt_packet .gen_suback (1 , 0 ))
155-
156- await mqtt_client .async_connect ()
157- assert mqtt_client .is_connected ()
164+ assert connected_mqtt_client .is_connected ()
165+ # Connecting again is a no-op
166+ await connected_mqtt_client .async_connect ()
167+ assert connected_mqtt_client .is_connected ()
158168
159- await mqtt_client .async_disconnect ()
160- assert not mqtt_client .is_connected ()
169+ await connected_mqtt_client .async_disconnect ()
170+ assert not connected_mqtt_client .is_connected ()
161171
162172 # Broker received a connect and subscribe. Disconnect packet is not
163173 # guaranteed to be captured by the time the async_disconnect returns
@@ -198,15 +208,10 @@ def build_rpc_response(message: dict[str, Any]) -> bytes:
198208async def test_get_room_mapping (
199209 received_requests : Queue ,
200210 response_queue : Queue ,
201- mqtt_client : RoborockMqttClientV1 ,
211+ connected_mqtt_client : RoborockMqttClientV1 ,
202212) -> None :
203213 """Test sending an arbitrary MQTT message and parsing the response."""
204214
205- response_queue .put (mqtt_packet .gen_connack (rc = 0 , flags = 2 ))
206- response_queue .put (mqtt_packet .gen_suback (1 , 0 ))
207- await mqtt_client .async_connect ()
208- assert mqtt_client .is_connected ()
209-
210215 test_request_id = 5050
211216 message = build_rpc_response (
212217 {
@@ -217,11 +222,9 @@ async def test_get_room_mapping(
217222 response_queue .put (mqtt_packet .gen_publish (MQTT_PUBLISH_TOPIC , payload = message ))
218223
219224 with patch ("roborock.version_1_apis.roborock_client_v1.get_next_int" , return_value = test_request_id ):
220- room_mapping = await mqtt_client .get_room_mapping ()
225+ room_mapping = await connected_mqtt_client .get_room_mapping ()
221226
222227 assert room_mapping == [
223228 RoomMapping (segment_id = 16 , iot_id = "2362048" ),
224229 RoomMapping (segment_id = 17 , iot_id = "2362044" ),
225230 ]
226-
227- await mqtt_client .async_disconnect ()
0 commit comments