Skip to content

Commit 8af494c

Browse files
committed
chore: some potential clean up
1 parent 3362aac commit 8af494c

File tree

3 files changed

+15
-50
lines changed

3 files changed

+15
-50
lines changed

roborock/protocols/v1_protocol.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,14 @@ class RequestMessage:
6565
request_id: int = field(default_factory=lambda: get_next_int(10000, 32767))
6666

6767
def encode_message(
68-
self,
69-
protocol: RoborockMessageProtocol,
70-
security_data: SecurityData | None = None,
68+
self, protocol: RoborockMessageProtocol, security_data: SecurityData | None = None, version: str = "1.0"
7169
) -> RoborockMessage:
7270
"""Convert the request message to a RoborockMessage."""
7371
return RoborockMessage(
7472
timestamp=self.timestamp,
7573
protocol=protocol,
7674
payload=self._as_payload(security_data=security_data),
75+
version=version.encode(),
7776
)
7877

7978
def _as_payload(self, security_data: SecurityData | None) -> bytes:

roborock/version_1_apis/roborock_client_v1.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,10 @@ def on_message_received(self, messages: list[RoborockMessage]) -> None:
447447
self._logger.debug(
448448
"Received unsolicited map response for request_id %s", map_response.request_id
449449
)
450+
elif data.protocol == RoborockMessageProtocol.GENERAL_RESPONSE and data.payload is None:
451+
# Api will often send blank messages with matching sequences, we can ignore these.
452+
continue
450453
else:
451-
if data.protocol == RoborockMessageProtocol.GENERAL_RESPONSE and data.payload is None:
452-
# Api will often send blank messages with matching sequences, we can ignore these.
453-
continue
454454
queue = self._waiting_queue.get(data.seq)
455455
if queue:
456456
if data.protocol == RoborockMessageProtocol.HELLO_RESPONSE:

roborock/version_1_apis/roborock_local_client_v1.py

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import asyncio
2-
import json
32
import logging
4-
import math
5-
import time
63
from asyncio import Lock, TimerHandle, Transport, get_running_loop
74
from collections.abc import Callable
85
from dataclasses import dataclass
@@ -12,7 +9,7 @@
129
from .. import CommandVacuumError, DeviceData, RoborockCommand
1310
from ..api import RoborockClient
1411
from ..exceptions import RoborockConnectionException, RoborockException, VacuumError
15-
from ..protocol import Decoder, Encoder, create_local_decoder, create_local_encoder
12+
from ..protocol import create_local_decoder, create_local_encoder
1613
from ..protocols.v1_protocol import RequestMessage
1714
from ..roborock_message import RoborockMessage, RoborockMessageProtocol
1815
from ..util import RoborockLoggerAdapter, get_next_int
@@ -54,13 +51,9 @@ def __init__(self, device_data: DeviceData, queue_timeout: int = 4, version: str
5451
RoborockClient.__init__(self, device_data)
5552
self._local_protocol = _LocalProtocol(self._data_received, self._connection_lost)
5653
self._version = version
57-
self._connect_nonce: int | None = None
54+
self._connect_nonce = get_next_int(10000, 32767)
5855
self._ack_nonce: int | None = None
59-
if version == "L01":
60-
self._set_l01_encoder_decoder()
61-
else:
62-
self._encoder: Encoder = create_local_encoder(device_data.device.local_key)
63-
self._decoder: Decoder = create_local_decoder(device_data.device.local_key)
56+
self._set_encoder_decoder()
6457
self.queue_timeout = queue_timeout
6558
self._logger = RoborockLoggerAdapter(device_data.device.name, _LOGGER)
6659

@@ -117,15 +110,14 @@ async def async_disconnect(self) -> None:
117110
async with self._mutex:
118111
self._sync_disconnect()
119112

120-
def _set_l01_encoder_decoder(self):
121-
"""Tell the system to use the L01 encoder/decoder."""
113+
def _set_encoder_decoder(self):
114+
"""Updates the encoder decoder. For L01 these are updated with nonces after the first hello."""
122115
self._encoder = create_local_encoder(self.device_info.device.local_key, self._connect_nonce, self._ack_nonce)
123116
self._decoder = create_local_decoder(self.device_info.device.local_key, self._connect_nonce, self._ack_nonce)
124117

125118
async def _do_hello(self, version: str) -> bool:
126119
"""Perform the initial handshaking."""
127120
self._logger.debug(f"Attempting to use the {version} protocol for client {self.device_info.device.duid}...")
128-
self._connect_nonce = get_next_int(10000, 32767)
129121
request = RoborockMessage(
130122
protocol=RoborockMessageProtocol.HELLO_REQUEST,
131123
version=version.encode(),
@@ -138,9 +130,8 @@ async def _do_hello(self, version: str) -> bool:
138130
request_id=request.seq,
139131
response_protocol=RoborockMessageProtocol.HELLO_RESPONSE,
140132
)
141-
if response.version.decode() == "L01":
142-
self._ack_nonce = response.random
143-
self._set_l01_encoder_decoder()
133+
self._ack_nonce = response.random
134+
self._set_encoder_decoder()
144135
self._version = version
145136
self._logger.debug(f"Client {self.device_info.device.duid} speaks the {version} protocol.")
146137
return True
@@ -187,35 +178,10 @@ async def _send_command(
187178
):
188179
if method in CLOUD_REQUIRED:
189180
raise RoborockException(f"Method {method} is not supported over local connection")
190-
if self._version == "L01":
191-
request_id = get_next_int(10000, 999999)
192-
dps_payload = {
193-
"id": request_id,
194-
"method": method,
195-
"params": params,
196-
}
197-
ts = math.floor(time.time())
198-
payload = {
199-
"dps": {str(RoborockMessageProtocol.RPC_REQUEST.value): json.dumps(dps_payload, separators=(",", ":"))},
200-
"t": ts,
201-
}
202-
roborock_message = RoborockMessage(
203-
protocol=RoborockMessageProtocol.GENERAL_REQUEST,
204-
payload=json.dumps(payload, separators=(",", ":")).encode("utf-8"),
205-
version=self._version.encode(),
206-
timestamp=ts,
207-
)
208-
self._logger.debug("Building message id %s for method %s", request_id, method)
209-
return await self._send_message(
210-
roborock_message,
211-
request_id=request_id,
212-
response_protocol=RoborockMessageProtocol.GENERAL_REQUEST,
213-
method=method,
214-
params=params,
215-
)
216-
217181
request_message = RequestMessage(method=method, params=params)
218-
roborock_message = request_message.encode_message(RoborockMessageProtocol.GENERAL_REQUEST)
182+
roborock_message = request_message.encode_message(
183+
RoborockMessageProtocol.GENERAL_REQUEST, version=self._version if self._version is not None else "1.0"
184+
)
219185
self._logger.debug("Building message id %s for method %s", request_message.request_id, method)
220186
return await self._send_message(
221187
roborock_message,

0 commit comments

Comments
 (0)