Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions roborock/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions roborock/cloud_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions roborock/local_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions roborock/version_1_apis/roborock_client_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down
9 changes: 0 additions & 9 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]")
with patch("roborock.web_api.PreparedRequest.request") as mock_request:
Expand Down
Loading