Skip to content

Commit 3dc64de

Browse files
committed
chore: move extract api
1 parent c18baff commit 3dc64de

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

roborock/devices/traits/v1/common.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import ClassVar, Self
1010

1111
from roborock.data import RoborockBase
12+
from roborock.exceptions import RoborockException
1213
from roborock.protocols.v1_protocol import V1RpcChannel
1314
from roborock.roborock_typing import RoborockCommand
1415

@@ -17,6 +18,20 @@
1718
V1ResponseData = dict | list | int | str
1819

1920

21+
def extract_v1_api_error_code(err: RoborockException) -> int | None:
22+
"""Extract a V1 RPC API error code from a RoborockException, if present.
23+
24+
V1 RPC error responses typically look like: {"code": -10007, "message": "..."}.
25+
"""
26+
if not err.args:
27+
return None
28+
payload = err.args[0]
29+
if isinstance(payload, dict):
30+
code = payload.get("code")
31+
return code if isinstance(code, int) else None
32+
return None
33+
34+
2035
@dataclass
2136
class V1TraitMixin(ABC):
2237
"""Base model that supports v1 traits.

roborock/devices/traits/v1/home.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,6 @@
3737
MAP_SLEEP = 3
3838

3939

40-
def _extract_api_error_code(err: RoborockException) -> int | None:
41-
"""Extract an API error code from a RoborockException, if present.
42-
43-
V1 RPC error responses typically look like: {"code": -10007, "message": "..."}.
44-
"""
45-
if not err.args:
46-
return None
47-
payload = err.args[0]
48-
if isinstance(payload, dict):
49-
code = payload.get("code")
50-
return code if isinstance(code, int) else None
51-
return None
52-
53-
5440
class HomeTrait(RoborockBase, common.V1TraitMixin):
5541
"""Trait that represents a full view of the home layout."""
5642

@@ -170,7 +156,7 @@ async def _build_home_map_info(self) -> tuple[dict[int, CombinedMapInfo], dict[i
170156
# Some firmware revisions return -10007 ("invalid status"/action locked) when attempting
171157
# to switch maps while the device is in a state that forbids it. Treat this as a
172158
# "busy" condition so callers can fall back to refreshing the current map only.
173-
if _extract_api_error_code(ex) == -10007:
159+
if common.extract_v1_api_error_code(ex) == -10007:
174160
raise RoborockDeviceBusy("Cannot switch maps right now (device action locked)") from ex
175161
raise
176162
await asyncio.sleep(MAP_SLEEP)

roborock/protocols/v1_protocol.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ def decode_rpc_response(message: RoborockMessage) -> ResponseMessage:
165165
api_error = RoborockException(
166166
f"Invalid V1 message format: missing 'result' in data point for {message.payload!r}"
167167
)
168+
result = {}
168169
else:
169170
_LOGGER.debug("Decoded V1 message result: %s", result)
170171
if isinstance(result, str):

0 commit comments

Comments
 (0)