Skip to content

Commit

Permalink
Add sensor decorators for roborock
Browse files Browse the repository at this point in the history
  • Loading branch information
rytilahti committed Aug 14, 2022
1 parent d9672f2 commit 5a65086
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions miio/integrations/vacuum/roborock/vacuumcontainers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from croniter import croniter

from miio.device import DeviceStatus
from miio.devicestatus import sensor
from miio.utils import pretty_seconds, pretty_time


Expand Down Expand Up @@ -86,11 +87,13 @@ def __init__(self, data: Dict[str, Any]) -> None:
self.data = data

@property
@sensor("State Code")
def state_code(self) -> int:
"""State code as returned by the device."""
return int(self.data["state"])

@property
@sensor("State")
def state(self) -> str:
"""Human readable state description, see also :func:`state_code`."""
states = {
Expand Down Expand Up @@ -124,11 +127,13 @@ def state(self) -> str:
return "Definition missing for state %s" % self.state_code

@property
@sensor("Error Code", icon="mdi:alert")
def error_code(self) -> int:
"""Error code as returned by the device."""
return int(self.data["error_code"])

@property
@sensor("Error", icon="mdi:alert")
def error(self) -> str:
"""Human readable error description, see also :func:`error_code`."""
try:
Expand All @@ -137,6 +142,7 @@ def error(self) -> str:
return "Definition missing for error %s" % self.error_code

@property
@sensor("Battery", unit="%", device_class="battery")
def battery(self) -> int:
"""Remaining battery in percentage."""
return int(self.data["battery"])
Expand All @@ -147,11 +153,13 @@ def fanspeed(self) -> int:
return int(self.data["fan_power"])

@property
@sensor("Clean Duration", unit="s", icon="mdi:timer-sand")
def clean_time(self) -> timedelta:
"""Time used for cleaning (if finished, shows how long it took)."""
return pretty_seconds(self.data["clean_time"])

@property
@sensor("Cleaned Area", unit="m2", icon="mdi:texture-box")
def clean_area(self) -> float:
"""Cleaned area in m2."""
return pretty_area(self.data["clean_area"])
Expand Down Expand Up @@ -188,13 +196,15 @@ def is_on(self) -> bool:
)

@property
@sensor("Water Box Attached")
def is_water_box_attached(self) -> Optional[bool]:
"""Return True is water box is installed."""
if "water_box_status" in self.data:
return self.data["water_box_status"] == 1
return None

@property
@sensor("Mop Attached")
def is_water_box_carriage_attached(self) -> Optional[bool]:
"""Return True if water box carriage (mop) is installed, None if sensor not
present."""
Expand All @@ -203,13 +213,15 @@ def is_water_box_carriage_attached(self) -> Optional[bool]:
return None

@property
@sensor("Water Level Low", icon="mdi:alert")
def is_water_shortage(self) -> Optional[bool]:
"""Returns True if water is low in the tank, None if sensor not present."""
if "water_shortage_status" in self.data:
return self.data["water_shortage_status"] == 1
return None

@property
@sensor("Error", icon="mdi:alert")
def got_error(self) -> bool:
"""True if an error has occured."""
return self.error_code != 0
Expand Down Expand Up @@ -241,16 +253,19 @@ def __init__(self, data: Union[List[Any], Dict[str, Any]]) -> None:
self.data["records"] = []

@property
@sensor("Total Cleaning Time", icon="mdi:timer-sand")
def total_duration(self) -> timedelta:
"""Total cleaning duration."""
return pretty_seconds(self.data["clean_time"])

@property
@sensor("Total Cleaning Area", icon="mdi:texture-box")
def total_area(self) -> float:
"""Total cleaned area."""
return pretty_area(self.data["clean_area"])

@property
@sensor("Total Clean Count")
def count(self) -> int:
"""Number of cleaning runs."""
return int(self.data["clean_count"])
Expand All @@ -261,6 +276,7 @@ def ids(self) -> List[int]:
return list(self.data["records"])

@property
@sensor("Dust Collection Count")
def dust_collection_count(self) -> Optional[int]:
"""Total number of dust collections."""
if "dust_collection_count" in self.data:
Expand Down Expand Up @@ -351,11 +367,13 @@ def __init__(self, data: Dict[str, Any]) -> None:
self.sensor_dirty_total = timedelta(hours=30)

@property
@sensor("Main Brush Usage", unit="s")
def main_brush(self) -> timedelta:
"""Main brush usage time."""
return pretty_seconds(self.data["main_brush_work_time"])

@property
@sensor("Main Brush Remaining", unit="s")
def main_brush_left(self) -> timedelta:
"""How long until the main brush should be changed."""
return self.main_brush_total - self.main_brush
Expand Down Expand Up @@ -399,6 +417,7 @@ def __init__(self, data: Dict[str, Any]):
self.data = data

@property
@sensor("Do Not Disturb")
def enabled(self) -> bool:
"""True if DnD is enabled."""
return bool(self.data["enabled"])
Expand Down Expand Up @@ -558,6 +577,7 @@ def __init__(self, data):
self.data = data

@property
@sensor("Carpet Mode")
def enabled(self) -> bool:
"""True if carpet mode is enabled."""
return self.data["enable"] == 1
Expand Down

0 comments on commit 5a65086

Please sign in to comment.