Skip to content

Commit a010304

Browse files
fix: adding local_api disconnection
1 parent dcad915 commit a010304

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

roborock/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def __init__(self, endpoint: str, device_localkey: dict[str, str]) -> None:
106106
def add_status_listener(self, callback: Callable[[str, str], None]):
107107
self._status_listeners.append(callback)
108108

109+
async def async_disconnect(self) -> Any:
110+
raise NotImplementedError
111+
109112
def _decode_msg(self, msg: bytes, local_key: str) -> dict[str, Any]:
110113
if msg[4:7] == "1.0".encode():
111114
msg = msg[4:]

roborock/local_api.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import socket
66
from asyncio import Lock
7-
from typing import Callable, Coroutine
7+
from typing import Callable, Coroutine, Any
88

99
import async_timeout
1010

@@ -37,6 +37,9 @@ async def async_connect(self):
3737
for listener in self.device_listener.values()
3838
])
3939

40+
async def async_disconnect(self) -> Any:
41+
await asyncio.gather(*[listener.disconnect() for listener in self.device_listener.values()])
42+
4043
async def send_command(
4144
self, device_id: str, method: RoborockCommand, params: list = None
4245
):
@@ -87,17 +90,23 @@ async def _main_coro(self):
8790
except Exception as e:
8891
_LOGGER.exception(e)
8992
except BrokenPipeError:
90-
self.socket.close()
93+
await self.disconnect()
9194

9295
async def connect(self):
9396
async with self._mutex:
9497
if not self.is_connected or self.socket.is_closed:
98+
self.socket = RoborockSocket(socket.AF_INET, socket.SOCK_STREAM)
99+
self.socket.setblocking(False)
95100
async with async_timeout.timeout(self.timeout):
96101
_LOGGER.info(f"Connecting to {self.ip}")
97102
await self.loop.sock_connect(self.socket, (self.ip, 58867))
98103
self.is_connected = True
99104
self.loop.create_task(self._main_coro())
100105

106+
async def disconnect(self):
107+
self.socket.close()
108+
self.is_connected = False
109+
101110
async def send_message(self, data: bytes):
102111
response = {}
103112
await self.connect()
@@ -110,5 +119,5 @@ async def send_message(self, data: bytes):
110119
f"Timeout after {self.timeout} seconds waiting for response"
111120
) from None
112121
except BrokenPipeError:
113-
self.socket.close()
122+
await self.disconnect()
114123
return response

0 commit comments

Comments
 (0)