Skip to content

Commit 752e320

Browse files
authored
fix: mypy complaints (#137)
* fix: change some typing * fix: include poetry lock * fix: linting * fix: linting * fix: add typing * fix: linting * fix: bugs * fix: linting * fix: none typing * Update api.py
1 parent d2d53a0 commit 752e320

File tree

6 files changed

+65
-20
lines changed

6 files changed

+65
-20
lines changed

roborock/api.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def _async_response(
349349
def _get_payload(
350350
self,
351351
method: RoborockCommand | str,
352-
params: list | dict | None = None,
352+
params: list | dict | int | None = None,
353353
secured=False,
354354
):
355355
timestamp = math.floor(time.time())
@@ -381,15 +381,15 @@ async def send_message(self, roborock_message: RoborockMessage):
381381
async def _send_command(
382382
self,
383383
method: RoborockCommand | str,
384-
params: list | dict | None = None,
384+
params: list | dict | int | None = None,
385385
):
386386
raise NotImplementedError
387387

388388
@final
389389
async def send_command(
390390
self,
391391
method: RoborockCommand | str,
392-
params: list | dict | None = None,
392+
params: list | dict | int | None = None,
393393
return_type: type[RT] | None = None,
394394
) -> RT:
395395
cacheable_attribute_result = find_cacheable_attribute(method)
@@ -412,8 +412,11 @@ async def send_command(
412412
return return_type.from_dict(response)
413413
return response
414414

415-
async def get_status(self) -> Status | None:
416-
return self._status_type.from_dict(await self.cache[CacheableAttribute.status].async_value())
415+
async def get_status(self) -> Status:
416+
data = self._status_type.from_dict(await self.cache[CacheableAttribute.status].async_value())
417+
if data is None:
418+
return self._status_type()
419+
return data
417420

418421
async def get_dnd_timer(self) -> DnDTimer | None:
419422
return DnDTimer.from_dict(await self.cache[CacheableAttribute.dnd_timer].async_value())
@@ -451,8 +454,11 @@ async def get_clean_record(self, record_id: int) -> CleanRecord | None:
451454
_LOGGER.warning("Clean record was of a new type, please submit an issue request: %s", record)
452455
return None
453456

454-
async def get_consumable(self) -> Consumable | None:
455-
return Consumable.from_dict(await self.cache[CacheableAttribute.consumable].async_value())
457+
async def get_consumable(self) -> Consumable:
458+
data = Consumable.from_dict(await self.cache[CacheableAttribute.consumable].async_value())
459+
if data is None:
460+
return Consumable()
461+
return data
456462

457463
async def get_wash_towel_mode(self) -> WashTowelMode | None:
458464
return WashTowelMode.from_dict(await self.cache[CacheableAttribute.wash_towel_mode].async_value())
@@ -463,7 +469,7 @@ async def get_dust_collection_mode(self) -> DustCollectionMode | None:
463469
async def get_smart_wash_params(self) -> SmartWashParams | None:
464470
return SmartWashParams.from_dict(await self.cache[CacheableAttribute.smart_wash_params].async_value())
465471

466-
async def get_dock_summary(self, dock_type: RoborockDockTypeCode) -> DockSummary | None:
472+
async def get_dock_summary(self, dock_type: RoborockDockTypeCode) -> DockSummary:
467473
"""Gets the status summary from the dock with the methods available for a given dock.
468474
469475
:param dock_type: RoborockDockTypeCode"""
@@ -525,11 +531,11 @@ async def get_room_mapping(self) -> list[RoomMapping] | None:
525531
]
526532
return None
527533

528-
async def get_child_lock_status(self) -> ChildLockStatus | None:
534+
async def get_child_lock_status(self) -> ChildLockStatus:
529535
"""Gets current child lock status."""
530536
return ChildLockStatus.from_dict(await self.cache[CacheableAttribute.child_lock_status].async_value())
531537

532-
async def get_flow_led_status(self) -> FlowLedStatus | None:
538+
async def get_flow_led_status(self) -> FlowLedStatus:
533539
"""Gets current flow led status."""
534540
return FlowLedStatus.from_dict(await self.cache[CacheableAttribute.flow_led_status].async_value())
535541

@@ -549,6 +555,12 @@ async def get_server_timer(self) -> list[ServerTimer]:
549555
def add_listener(self, listener: Callable):
550556
self._listeners.append(listener)
551557

558+
async def get_from_cache(self, key: CacheableAttribute) -> AttributeCache | None:
559+
val = self.cache.get(key)
560+
if val is not None:
561+
return await val.async_value()
562+
return None
563+
552564

553565
class RoborockApiClient:
554566
def __init__(self, username: str, base_url=None) -> None:

roborock/cloud_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ async def send_message(self, roborock_message: RoborockMessage):
199199
async def _send_command(
200200
self,
201201
method: RoborockCommand | str,
202-
params: list | dict | None = None,
202+
params: list | dict | int | None = None,
203203
):
204204
request_id, timestamp, payload = super()._get_payload(method, params, True)
205205
request_protocol = RoborockMessageProtocol.RPC_REQUEST

roborock/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
ROBOROCK_G10S_PRO = "roborock.vacuum.a26"
2727
ROBOROCK_G10S = "roborock.vacuum.a46"
2828
ROBOROCK_G10 = "roborock.vacuum.a29"
29-
ROCKROBO_G10_SG = "roborock.vacuum.a30" # Variant of the G10, has similar features as S7
29+
ROCKROBO_G10_SG = "roborock.vacuum.a30" # Variant of the G10, has similar features as S7
3030
ROBOROCK_S7 = "roborock.vacuum.a15"
3131
ROBOROCK_S6_MAXV = "roborock.vacuum.a10"
3232
ROBOROCK_E2 = "roborock.vacuum.e2"

roborock/containers.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
SENSOR_DIRTY_REPLACE_TIME,
5151
SIDE_BRUSH_REPLACE_TIME,
5252
)
53+
from .exceptions import RoborockException
5354

5455
_LOGGER = logging.getLogger(__name__)
5556

@@ -185,12 +186,12 @@ class HomeDataDevice(RoborockBase):
185186
name: str
186187
local_key: str
187188
fv: str
189+
product_id: str
188190
attribute: Any | None = None
189191
active_time: int | None = None
190192
runtime_env: Any | None = None
191193
time_zone_id: str | None = None
192194
icon_url: str | None = None
193-
product_id: str | None = None
194195
lon: Any | None = None
195196
lat: Any | None = None
196197
share: Any | None = None
@@ -297,9 +298,41 @@ class Status(RoborockBase):
297298
dss: int | None = None
298299
common_status: int | None = None
299300
corner_clean_mode: int | None = None
301+
error_code_name: str | None = None
302+
state_name: str | None = None
303+
water_box_mode_name: str | None = None
304+
fan_power_options: list[str] = field(default_factory=list)
305+
fan_power_name: str | None = None
306+
mop_mode_name: str | None = None
300307

301308
def __post_init__(self) -> None:
302309
self.square_meter_clean_area = round(self.clean_area / 1000000, 1) if self.clean_area is not None else None
310+
if self.error_code is not None:
311+
self.error_code_name = self.error_code.name
312+
if self.state is not None:
313+
self.state_name = self.state.name
314+
if self.water_box_mode is not None:
315+
self.water_box_mode_name = self.water_box_mode.name
316+
if self.fan_power is not None:
317+
self.fan_power_options = self.fan_power.keys()
318+
self.fan_power_name = self.fan_power.name
319+
if self.mop_mode is not None:
320+
self.mop_mode_name = self.mop_mode.name
321+
322+
def get_fan_speed_code(self, fan_speed: str) -> int:
323+
if self.fan_power is None:
324+
raise RoborockException("Attempted to get fan speed before status has been updated.")
325+
return self.fan_power.as_dict().get(fan_speed)
326+
327+
def get_mop_intensity_code(self, mop_intensity: str) -> int:
328+
if self.water_box_mode is None:
329+
raise RoborockException("Attempted to get mop_intensity before status has been updated.")
330+
return self.water_box_mode.as_dict().get(mop_intensity)
331+
332+
def get_mop_mode_code(self, mop_mode: str) -> int:
333+
if self.mop_mode is None:
334+
raise RoborockException("Attempted to get mop_mode before status has been updated.")
335+
return self.mop_mode.as_dict().get(mop_mode)
303336

304337

305338
@dataclass

roborock/local_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ async def async_disconnect(self) -> None:
8383
self.sync_disconnect()
8484

8585
def build_roborock_message(
86-
self, method: RoborockCommand | str, params: list | dict | None = None
86+
self, method: RoborockCommand | str, params: list | dict | int | None = None
8787
) -> RoborockMessage:
8888
secured = True if method in COMMANDS_SECURED else False
8989
request_id, timestamp, payload = self._get_payload(method, params, secured)
@@ -123,7 +123,7 @@ async def ping(self):
123123
async def _send_command(
124124
self,
125125
method: RoborockCommand | str,
126-
params: list | dict | None = None,
126+
params: list | dict | int | None = None,
127127
):
128128
roborock_message = self.build_roborock_message(method, params)
129129
return await self.send_message(roborock_message)

roborock/roborock_typing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from dataclasses import dataclass
3+
from dataclasses import dataclass, field
44
from enum import Enum
55

66
from .containers import (
@@ -132,7 +132,7 @@ class RoborockCommand(str, Enum):
132132

133133
@dataclass
134134
class CommandInfo:
135-
params: list | dict | None = None
135+
params: list | dict | int | None = None
136136

137137

138138
CommandInfoMap: dict[RoborockCommand | None, CommandInfo] = {
@@ -315,9 +315,9 @@ class DockSummary(RoborockBase):
315315

316316
@dataclass
317317
class DeviceProp(RoborockBase):
318-
status: Status | None = None
319-
clean_summary: CleanSummary | None = None
320-
consumable: Consumable | None = None
318+
status: Status = field(default_factory=Status)
319+
clean_summary: CleanSummary = field(default_factory=CleanSummary)
320+
consumable: Consumable = field(default_factory=Consumable)
321321
last_clean_record: CleanRecord | None = None
322322
dock_summary: DockSummary | None = None
323323

0 commit comments

Comments
 (0)