Skip to content

Commit 56e4469

Browse files
fix: improve device ping
1 parent f03c735 commit 56e4469

1 file changed

Lines changed: 41 additions & 30 deletions

File tree

roborock/local_api.py

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
from . import DeviceData
1111
from .api import COMMANDS_SECURED, QUEUE_TIMEOUT, RoborockClient
12-
from .exceptions import CommandVacuumError, RoborockConnectionException, RoborockException
12+
from .exceptions import (
13+
CommandVacuumError,
14+
RoborockConnectionException,
15+
RoborockException,
16+
)
1317
from .protocol import MessageParser
1418
from .roborock_message import RoborockMessage, RoborockMessageProtocol
1519
from .roborock_typing import CommandInfoMap, RoborockCommand
@@ -47,10 +51,14 @@ def is_connected(self):
4751
return self.transport and self.transport.is_reading()
4852

4953
async def keep_alive_func(self, _=None):
50-
await self.ping()
54+
try:
55+
await self.ping()
56+
except RoborockException:
57+
pass
5158
self.keep_alive_task = self.loop.call_later(10, lambda: asyncio.create_task(self.keep_alive_func()))
5259

5360
async def async_connect(self) -> None:
61+
should_ping = False
5462
async with self._mutex:
5563
try:
5664
if not self.is_connected():
@@ -61,10 +69,12 @@ async def async_connect(self) -> None:
6169
lambda: self, self.host, 58867
6270
)
6371
_LOGGER.info(f"Connected to {self.host}")
64-
await self.hello()
65-
await self.keep_alive_func()
66-
except Exception as e:
72+
should_ping = True
73+
except BaseException as e:
6774
raise RoborockConnectionException(f"Failed connecting to {self.host}") from e
75+
if should_ping:
76+
await self.hello()
77+
await self.keep_alive_func()
6878

6979
def sync_disconnect(self) -> None:
7080
if self.transport and self.loop.is_running():
@@ -95,28 +105,33 @@ def build_roborock_message(self, method: RoborockCommand, params: Optional[list
95105
)
96106

97107
async def hello(self):
98-
if self.is_connected():
99-
request_id = 1
100-
protocol = RoborockMessageProtocol.HELLO_REQUEST
101-
_LOGGER.debug(f"id={request_id} Requesting protocol {protocol.name}")
102-
try:
103-
return await self._send_message(
104-
RoborockMessage(protocol=protocol, payload=None, seq=request_id, version=b"1.0", random=22)
108+
request_id = 1
109+
protocol = RoborockMessageProtocol.HELLO_REQUEST
110+
try:
111+
return await self.send_message(
112+
RoborockMessage(
113+
protocol=protocol,
114+
payload=None,
115+
seq=request_id,
116+
version=b"1.0",
117+
random=22,
105118
)
106-
except Exception as e:
107-
_LOGGER.error(e)
119+
)
120+
except Exception as e:
121+
_LOGGER.error(e)
108122

109123
async def ping(self):
110-
if self.is_connected():
111-
request_id = 2
112-
protocol = RoborockMessageProtocol.PING_REQUEST
113-
_LOGGER.debug(f"id={request_id} Requesting protocol {protocol.name}")
114-
try:
115-
return await self._send_message(
116-
RoborockMessage(protocol=protocol, payload=None, seq=request_id, version=b"1.0", random=23)
117-
)
118-
except Exception as e:
119-
_LOGGER.error(e)
124+
request_id = 2
125+
protocol = RoborockMessageProtocol.PING_REQUEST
126+
return await self.send_message(
127+
RoborockMessage(
128+
protocol=protocol,
129+
payload=None,
130+
seq=request_id,
131+
version=b"1.0",
132+
random=23,
133+
)
134+
)
120135

121136
async def send_command(self, method: RoborockCommand, params: Optional[list | dict] = None):
122137
roborock_message = self.build_roborock_message(method, params)
@@ -136,12 +151,8 @@ async def async_local_response(self, roborock_message: RoborockMessage):
136151
(response, err) = await self._async_response(request_id, response_protocol)
137152
if err:
138153
raise CommandVacuumError("", err) from err
139-
method = (
140-
f"method {roborock_message.get_method()}"
141-
if roborock_message.protocol == 4
142-
else f"protocol {RoborockMessageProtocol(roborock_message.protocol).name}"
143-
)
144-
_LOGGER.debug(f"id={request_id} Response from {method}: {response}")
154+
if roborock_message.protocol == 4:
155+
_LOGGER.debug(f"id={request_id} Response from method {roborock_message.get_method()}: {response}")
145156
return response
146157

147158
def _send_msg_raw(self, data: bytes):

0 commit comments

Comments
 (0)