Skip to content

Commit 1bc5824

Browse files
committed
chore: Include atributes in repr computation
1 parent 7944ef8 commit 1bc5824

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

roborock/containers.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ def _decamelize(s: str):
107107
return re.sub("([A-Z]+)", "_\\1", s).lower()
108108

109109

110+
def _attr_repr(obj: Any, attrs: list[str]) -> str:
111+
"""Return a string representation of the object including specified attributes."""
112+
# Reproduce default repr behavior
113+
items = (f"{k}={v!r}" for k, v in obj.__dict__.items() if not k.startswith("_"))
114+
default_repr = "{}({})".format(type(obj).__name__, ", ".join(items))
115+
# Append additional attributes
116+
parts = [default_repr[:-1]]
117+
for attr in attrs:
118+
value = getattr(obj, attr, None)
119+
parts.append(f", {attr}={repr(value)}")
120+
parts.append(")")
121+
return "".join(parts)
122+
123+
110124
@dataclass
111125
class RoborockBase:
112126
@staticmethod
@@ -193,6 +207,9 @@ def end_time(self) -> datetime.time | None:
193207
else None
194208
)
195209

210+
def __repr__(self) -> str:
211+
return _attr_repr(self, ["start_time", "end_time"])
212+
196213

197214
@dataclass
198215
class Reference(RoborockBase):
@@ -444,6 +461,20 @@ def current_map(self) -> int | None:
444461
return (self.map_status - 3) // 4
445462
return None
446463

464+
def __repr__(self) -> str:
465+
return _attr_repr(
466+
self,
467+
[
468+
"square_meter_clean_area",
469+
"error_code_name",
470+
"state_name",
471+
"water_box_mode_name",
472+
"fan_power_name",
473+
"mop_mode_name",
474+
"current_map",
475+
],
476+
)
477+
447478

448479
@dataclass
449480
class S4MaxStatus(Status):
@@ -607,6 +638,9 @@ def square_meter_clean_area(self) -> float | None:
607638
return None
608639
return round(self.clean_area / 1000000, 1) if self.clean_area is not None else None
609640

641+
def __repr__(self):
642+
return _attr_repr(self, ["square_meter_clean_area"])
643+
610644

611645
@dataclass
612646
class CleanRecord(RoborockBase):
@@ -636,6 +670,9 @@ def begin_datetime(self) -> datetime.datetime | None:
636670
def end_datetime(self) -> datetime.datetime | None:
637671
return datetime.datetime.fromtimestamp(self.end).astimezone(timezone.utc) if self.end else None
638672

673+
def __repr__(self) -> str:
674+
return _attr_repr(self, ["square_meter_area", "begin_datetime", "end_datetime"])
675+
639676

640677
@dataclass
641678
class Consumable(RoborockBase):
@@ -689,6 +726,21 @@ def cleaning_brush_time_left(self) -> int | None:
689726
def mop_roller_time_left(self) -> int | None:
690727
return MOP_ROLLER_REPLACE_TIME - self.moproller_work_time if self.moproller_work_time is not None else None
691728

729+
def __repr__(self) -> str:
730+
return _attr_repr(
731+
self,
732+
[
733+
"main_brush_time_left",
734+
"side_brush_time_left",
735+
"filter_time_left",
736+
"sensor_time_left",
737+
"strainer_time_left",
738+
"dust_collection_time_left",
739+
"cleaning_brush_time_left",
740+
"mop_roller_time_left",
741+
],
742+
)
743+
692744

693745
@dataclass
694746
class MultiMapsListMapInfoBakMaps(RoborockBase):
@@ -777,6 +829,9 @@ class DeviceData(RoborockBase):
777829
def product_nickname(self) -> RoborockProductNickname:
778830
return SHORT_MODEL_TO_ENUM.get(self.model.split(".")[-1], RoborockProductNickname.PEARLPLUS)
779831

832+
def __repr__(self) -> str:
833+
return _attr_repr(self, ["product_nickname"])
834+
780835

781836
@dataclass
782837
class RoomMapping(RoborockBase):
@@ -867,6 +922,9 @@ def product_nickname(self) -> RoborockProductNickname | None:
867922
return RoborockProductSpec.from_dict(json.loads(self.cardspec).get("data"))
868923
return None
869924

925+
def __repr__(self) -> str:
926+
return _attr_repr(self, ["product_nickname"])
927+
870928

871929
@dataclass
872930
class RoborockProductCategory(RoborockBase):

tests/devices/__snapshots__/test_v1_device.ambr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# serializer version: 1
22
# name: test_device_trait_command_parsing[payload0-<lambda>]
3-
StatusTrait(msg_ver=2, msg_seq=515, state=<RoborockStateCode.charging: 8>, battery=100, clean_time=5405, clean_area=91287500, error_code=<RoborockErrorCode.none: 0>, map_present=1, in_cleaning=<RoborockInCleaning.complete: 0>, in_returning=0, in_fresh_state=1, lab_status=1, water_box_status=0, back_type=None, wash_phase=None, wash_ready=None, fan_power=<RoborockFanSpeedS7MaxV.custom: 106>, dnd_enabled=1, map_status=3, is_locating=0, lock_status=0, water_box_mode=<RoborockMopIntensityS7.custom: 204>, water_box_carriage_status=0, mop_forbidden_enable=0, camera_status=None, is_exploring=None, home_sec_status=None, home_sec_enable_password=None, adbumper_status=None, water_shortage_status=None, dock_type=None, dust_collection_status=None, auto_dust_collection=None, avoid_count=None, mop_mode=None, debug_mode=None, collision_avoid_status=None, switch_map_mode=None, dock_error_status=None, charge_status=None, unsave_map_reason=4, unsave_map_flag=0, wash_status=None, distance_off=0, in_warmup=None, dry_status=None, rdt=None, clean_percent=None, rss=None, dss=None, common_status=None, corner_clean_mode=None)
3+
StatusTrait(msg_ver=2, msg_seq=515, state=<RoborockStateCode.charging: 8>, battery=100, clean_time=5405, clean_area=91287500, error_code=<RoborockErrorCode.none: 0>, map_present=1, in_cleaning=<RoborockInCleaning.complete: 0>, in_returning=0, in_fresh_state=1, lab_status=1, water_box_status=0, fan_power=<RoborockFanSpeedS7MaxV.custom: 106>, dnd_enabled=1, map_status=3, is_locating=0, lock_status=0, water_box_mode=<RoborockMopIntensityS7.custom: 204>, water_box_carriage_status=0, mop_forbidden_enable=0, unsave_map_reason=4, unsave_map_flag=0, distance_off=0, square_meter_clean_area=91.3, error_code_name=None, state_name='charging', water_box_mode_name='custom', fan_power_name='custom', mop_mode_name=None, current_map=0)
44
# ---
55
# name: test_device_trait_command_parsing[payload1-<lambda>]
66
DoNotDisturbTrait(start_hour=22, start_minute=0, end_hour=8, end_minute=0, enabled=1)
77
# ---
88
# name: test_device_trait_command_parsing[payload2-<lambda>]
9-
CleanSummaryTrait(clean_time=1442559, clean_area=24258125000, clean_count=296, dust_collection_count=None, records=[1756848207, 1754930385, 1753203976, 1752183435, 1747427370, 1746204046, 1745601543, 1744387080, 1743528522, 1742489154, 1741022299, 1740433682, 1739902516, 1738875106, 1738864366, 1738620067, 1736873889, 1736197544, 1736121269, 1734458038], last_clean_t=None)
9+
CleanSummaryTrait(clean_time=1442559, clean_area=24258125000, clean_count=296, dust_collection_count=None, records=[1756848207, 1754930385, 1753203976, 1752183435, 1747427370, 1746204046, 1745601543, 1744387080, 1743528522, 1742489154, 1741022299, 1740433682, 1739902516, 1738875106, 1738864366, 1738620067, 1736873889, 1736197544, 1736121269, 1734458038], last_clean_t=None, square_meter_clean_area=24258.1)
1010
# ---
1111
# name: test_device_trait_command_parsing[payload3-<lambda>]
1212
SoundVolumeTrait(volume=90)

0 commit comments

Comments
 (0)