Skip to content

Commit dcad915

Browse files
fix: move add_status_listener from cloud_api to base_api
1 parent bf8c8d5 commit dcad915

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

roborock/api.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,25 @@ def __init__(self, endpoint: str, device_localkey: dict[str, str]) -> None:
103103
self._waiting_queue: dict[int, RoborockQueue] = {}
104104
self._status_listeners: list[Callable[[str, str], None]] = []
105105

106+
def add_status_listener(self, callback: Callable[[str, str], None]):
107+
self._status_listeners.append(callback)
108+
106109
def _decode_msg(self, msg: bytes, local_key: str) -> dict[str, Any]:
107110
if msg[4:7] == "1.0".encode():
108111
msg = msg[4:]
109112
elif msg[0:3] != "1.0".encode():
110113
raise RoborockException(f"Unknown protocol version {msg[0:3]}")
111114
if len(msg) == 17:
112-
[version, _seq, _random, timestamp, protocol] = struct.unpack(
115+
[version, request_id, _random, timestamp, protocol] = struct.unpack(
113116
"!3sIIIH", msg[0:17]
114117
)
115118
return {
116119
"version": version,
120+
"request_id": request_id,
117121
"timestamp": timestamp,
118122
"protocol": protocol,
119123
}
120-
[version, _seq, _random, timestamp, protocol, payload_len] = struct.unpack(
124+
[version, request_id, _random, timestamp, protocol, payload_len] = struct.unpack(
121125
"!3sIIIHH", msg[0:19]
122126
)
123127
extra_len = len(msg) - 23 - payload_len
@@ -132,20 +136,21 @@ def _decode_msg(self, msg: bytes, local_key: str) -> dict[str, Any]:
132136
decrypted_payload = unpad(decipher.decrypt(payload), AES.block_size) if payload else extra
133137
return {
134138
"version": version,
139+
"request_id": request_id,
135140
"timestamp": timestamp,
136141
"protocol": protocol,
137142
"payload": decrypted_payload
138143
}
139144

140-
def _encode_msg(self, device_id, protocol, timestamp, payload, prefix=None) -> bytes:
145+
def _encode_msg(self, device_id, request_id, protocol, timestamp, payload, prefix=None) -> bytes:
141146
local_key = self.device_localkey[device_id]
142147
aes_key = md5bin(encode_timestamp(timestamp) + local_key + self._salt)
143148
cipher = AES.new(aes_key, AES.MODE_ECB)
144149
encrypted = cipher.encrypt(pad(payload, AES.block_size))
145150
encrypted_len = len(encrypted)
146151
values = [
147152
"1.0".encode(),
148-
self._seq,
153+
request_id,
149154
self._random,
150155
timestamp,
151156
protocol,

roborock/cloud_api.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ async def _async_check_keepalive(self) -> None:
118118
if now - self._last_disconnection > self._keepalive ** 2 and now - self._last_device_msg_in > self._keepalive:
119119
self._ping_t = self._last_device_msg_in
120120

121-
def add_status_listener(self, callback: Callable[[str, str], None]):
122-
self._status_listeners.append(callback)
123-
124121
def _check_keepalive(self) -> None:
125122
self._async_check_keepalive()
126123
super()._check_keepalive()

roborock/local_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async def send_command(
4646
_LOGGER.debug(f"id={request_id} Requesting method {method} with {params}")
4747
prefix = secured_prefix if method in SPECIAL_COMMANDS else get_prefix
4848
protocol = 4
49-
msg = self._encode_msg(device_id, protocol, timestamp, payload, prefix)
49+
msg = self._encode_msg(device_id, request_id, protocol, timestamp, payload, prefix)
5050
# Send the command to the Roborock device
5151
listener = self.device_listener.get(device_id)
5252
await listener.send_message(msg)

0 commit comments

Comments
 (0)