diff --git a/roborock/api.py b/roborock/api.py index 764adff0..777ead6d 100644 --- a/roborock/api.py +++ b/roborock/api.py @@ -48,10 +48,8 @@ def __init__(self, device_info: DeviceData) -> None: self.is_available: bool = True def __del__(self) -> None: - self.release() - - def release(self) -> None: - self.sync_disconnect() + if self.is_connected(): + self._logger.debug("Roborock is connected while being released") async def async_release(self) -> None: await self.async_disconnect() @@ -65,12 +63,12 @@ async def async_connect(self): """Connect to the Roborock device.""" @abstractmethod - def sync_disconnect(self) -> Any: + async def async_disconnect(self) -> Any: """Disconnect from the Roborock device.""" @abstractmethod - async def async_disconnect(self) -> Any: - """Disconnect from the Roborock device.""" + def is_connected(self) -> bool: + """Return True if the client is connected to the device.""" @abstractmethod def on_message_received(self, messages: list[RoborockMessage]) -> None: diff --git a/roborock/cloud_api.py b/roborock/cloud_api.py index b608c89a..f2731371 100644 --- a/roborock/cloud_api.py +++ b/roborock/cloud_api.py @@ -121,7 +121,7 @@ def is_connected(self) -> bool: """Check if the mqtt client is connected.""" return self._mqtt_client.is_connected() - def sync_disconnect(self) -> Any: + def _sync_disconnect(self) -> Any: if not self.is_connected(): return None @@ -139,7 +139,7 @@ def sync_disconnect(self) -> Any: return disconnected_future - def sync_connect(self) -> Any: + def _sync_connect(self) -> Any: if self.is_connected(): self._mqtt_client.maybe_restart_loop() return None @@ -155,14 +155,14 @@ def sync_connect(self) -> Any: async def async_disconnect(self) -> None: async with self._mutex: - if disconnected_future := self.sync_disconnect(): + if disconnected_future := self._sync_disconnect(): # There are no errors set on this future await disconnected_future await self.event_loop.run_in_executor(None, self._mqtt_client.loop_stop) async def async_connect(self) -> None: async with self._mutex: - if connected_future := self.sync_connect(): + if connected_future := self._sync_connect(): try: await connected_future except VacuumError as err: diff --git a/roborock/local_api.py b/roborock/local_api.py index 31deba46..f0a2de96 100644 --- a/roborock/local_api.py +++ b/roborock/local_api.py @@ -61,7 +61,7 @@ def _data_received(self, message): def _connection_lost(self, exc: Exception | None): """Called when the transport connection is lost.""" - self.sync_disconnect() + self._sync_disconnect() self.on_connection_lost(exc) def is_connected(self): @@ -79,7 +79,7 @@ async def async_connect(self) -> None: async with self._mutex: try: if not self.is_connected(): - self.sync_disconnect() + self._sync_disconnect() async with async_timeout.timeout(self.queue_timeout): self._logger.debug(f"Connecting to {self.host}") self.transport, _ = await self.event_loop.create_connection( # type: ignore @@ -93,7 +93,7 @@ async def async_connect(self) -> None: await self.hello() await self.keep_alive_func() - def sync_disconnect(self) -> None: + def _sync_disconnect(self) -> None: if self.transport and self.event_loop.is_running(): self._logger.debug(f"Disconnecting from {self.host}") self.transport.close() @@ -102,7 +102,7 @@ def sync_disconnect(self) -> None: async def async_disconnect(self) -> None: async with self._mutex: - self.sync_disconnect() + self._sync_disconnect() async def hello(self): request_id = 1 diff --git a/roborock/version_1_apis/roborock_client_v1.py b/roborock/version_1_apis/roborock_client_v1.py index f817ff71..754b1af8 100644 --- a/roborock/version_1_apis/roborock_client_v1.py +++ b/roborock/version_1_apis/roborock_client_v1.py @@ -164,10 +164,6 @@ def __init__(self, device_info: DeviceData, endpoint: str): self.listener_model = self._listeners[device_info.device.duid] self._endpoint = endpoint - def release(self): - super().release() - [item.stop() for item in self.cache.values()] - async def async_release(self) -> None: await super().async_release() [item.stop() for item in self.cache.values()] diff --git a/tests/test_api.py b/tests/test_api.py index 82a29428..dba77a42 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -45,15 +45,6 @@ async def test_can_create_mqtt_roborock(): RoborockMqttClientV1(UserData.from_dict(USER_DATA), device_info) -async def test_sync_connect(mqtt_client): - with patch("paho.mqtt.client.Client.connect", return_value=mqtt.MQTT_ERR_SUCCESS): - with patch("paho.mqtt.client.Client.loop_start", return_value=mqtt.MQTT_ERR_SUCCESS): - connected_future = mqtt_client.sync_connect() - assert connected_future is not None - - connected_future.cancel() - - async def test_get_base_url_no_url(): rc = RoborockApiClient("sample@gmail.com") with patch("roborock.web_api.PreparedRequest.request") as mock_request: