Skip to content

Commit 58b3322

Browse files
fix: type checks
1 parent f5db0f9 commit 58b3322

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed

roborock/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def on_message_received(self, messages: list[RoborockMessage]) -> None:
122122
self._last_device_msg_in = self.time_func()
123123
for data in messages:
124124
protocol = data.protocol
125-
if protocol == 102 or protocol == 4:
125+
if data.payload and (protocol == 102 or protocol == 4):
126126
payload = json.loads(data.payload.decode())
127127
for data_point_number, data_point in payload.get("dps").items():
128128
if data_point_number == "102":
@@ -146,7 +146,7 @@ def on_message_received(self, messages: list[RoborockMessage]) -> None:
146146
if isinstance(result, list) and len(result) == 1:
147147
result = result[0]
148148
queue.resolve((result, None))
149-
elif protocol == 301:
149+
elif data.payload and protocol == 301:
150150
payload = data.payload[0:24]
151151
[endpoint, _, request_id, _] = struct.unpack("<15sBH6s", payload)
152152
if endpoint.decode().startswith(self._endpoint):

roborock/local_api.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,9 @@ async def ping(self):
9898
request_id = 2
9999
_LOGGER.debug(f"id={request_id} Requesting method ping with None")
100100
try:
101-
return await self.send_message(RoborockMessage(
102-
protocol=2,
103-
payload=None,
104-
seq=request_id,
105-
version=b'1.0',
106-
random=23
107-
))
101+
return await self.send_message(
102+
RoborockMessage(protocol=2, payload=None, seq=request_id, version=b"1.0", random=23)
103+
)
108104
except Exception as e:
109105
_LOGGER.error(e)
110106

roborock/protocol.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ def __del__(self):
5353

5454
def datagram_received(self, data, _):
5555
[broadcast_message], _ = BroadcastParser.parse(data)
56-
parsed_message = BroadcastMessage.from_dict(json.loads(broadcast_message.payload))
57-
_LOGGER.debug(f"Received broadcast: {parsed_message}")
58-
self.devices_found.append(parsed_message)
56+
if broadcast_message.payload:
57+
parsed_message = BroadcastMessage.from_dict(json.loads(broadcast_message.payload))
58+
_LOGGER.debug(f"Received broadcast: {parsed_message}")
59+
self.devices_found.append(parsed_message)
5960

6061
async def discover(self):
6162
async with self._mutex:

roborock/roborock_message.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from dataclasses import dataclass
77
from random import randint
88

9-
from .roborock_typing import RoborockCommand
10-
119

1210
@dataclass
1311
class RoborockMessage:
@@ -20,17 +18,17 @@ class RoborockMessage:
2018

2119
def get_request_id(self) -> int | None:
2220
protocol = self.protocol
23-
if protocol in [4, 101, 102]:
21+
if self.payload and protocol in [4, 101, 102]:
2422
payload = json.loads(self.payload.decode())
2523
for data_point_number, data_point in payload.get("dps").items():
2624
if data_point_number in ["101", "102"]:
2725
data_point_response = json.loads(data_point)
2826
return data_point_response.get("id")
2927
return None
3028

31-
def get_method(self) -> RoborockCommand | None:
29+
def get_method(self) -> str | None:
3230
protocol = self.protocol
33-
if protocol in [4, 5, 101, 102]:
31+
if self.payload and protocol in [4, 5, 101, 102]:
3432
payload = json.loads(self.payload.decode())
3533
for data_point_number, data_point in payload.get("dps").items():
3634
if data_point_number in ["101", "102"]:
@@ -40,7 +38,7 @@ def get_method(self) -> RoborockCommand | None:
4038

4139
def get_params(self) -> list | dict | None:
4240
protocol = self.protocol
43-
if protocol in [4, 101, 102]:
41+
if self.payload and protocol in [4, 101, 102]:
4442
payload = json.loads(self.payload.decode())
4543
for data_point_number, data_point in payload.get("dps").items():
4644
if data_point_number in ["101", "102"]:

0 commit comments

Comments
 (0)