Skip to content

Commit cc36704

Browse files
committed
fix: Update containers that __post_init__ to use properties
This is done since with the new v1 api we reuse attributes and mutate them on update, so tis allows the properties to reamin fresh.
1 parent 3589ff5 commit cc36704

File tree

2 files changed

+87
-72
lines changed

2 files changed

+87
-72
lines changed

roborock/containers.py

Lines changed: 85 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ class Status(RoborockBase):
345345
battery: int | None = None
346346
clean_time: int | None = None
347347
clean_area: int | None = None
348-
square_meter_clean_area: float | None = None
349348
error_code: RoborockErrorCode | None = None
350349
map_present: int | None = None
351350
in_cleaning: RoborockInCleaning | None = None
@@ -392,26 +391,36 @@ class Status(RoborockBase):
392391
dss: int | None = None
393392
common_status: int | None = None
394393
corner_clean_mode: int | None = None
395-
error_code_name: str | None = None
396-
state_name: str | None = None
397-
water_box_mode_name: str | None = None
398-
fan_power_options: list[str] = field(default_factory=list)
399-
fan_power_name: str | None = None
400-
mop_mode_name: str | None = None
401-
402-
def __post_init__(self) -> None:
403-
self.square_meter_clean_area = round(self.clean_area / 1000000, 1) if self.clean_area is not None else None
404-
if self.error_code is not None:
405-
self.error_code_name = self.error_code.name
406-
if self.state is not None:
407-
self.state_name = self.state.name
408-
if self.water_box_mode is not None:
409-
self.water_box_mode_name = self.water_box_mode.name
410-
if self.fan_power is not None:
411-
self.fan_power_options = self.fan_power.keys()
412-
self.fan_power_name = self.fan_power.name
413-
if self.mop_mode is not None:
414-
self.mop_mode_name = self.mop_mode.name
394+
395+
@property
396+
def square_meter_clean_area(self) -> float | None:
397+
return round(self.clean_area / 1000000, 1) if self.clean_area is not None else None
398+
399+
@property
400+
def error_code_name(self) -> str | None:
401+
return self.error_code.name if self.error_code else None
402+
403+
@property
404+
def state_name(self) -> str | None:
405+
return self.state.name if self.state else None
406+
407+
@property
408+
def water_box_mode_name(self) -> str | None:
409+
return self.water_box_mode.name if self.water_box_mode else None
410+
411+
@property
412+
def fan_power_options(self) -> list[str]:
413+
if self.fan_power is None:
414+
return []
415+
return list(self.fan_power.keys())
416+
417+
@property
418+
def fan_power_name(self) -> str | None:
419+
return self.fan_power.name if self.fan_power else None
420+
421+
@property
422+
def mop_mode_name(self) -> str | None:
423+
return self.mop_mode.name if self.mop_mode else None
415424

416425
def get_fan_speed_code(self, fan_speed: str) -> int:
417426
if self.fan_power is None:
@@ -585,28 +594,26 @@ class ValleyElectricityTimer(RoborockBaseTimer):
585594
class CleanSummary(RoborockBase):
586595
clean_time: int | None = None
587596
clean_area: int | None = None
588-
square_meter_clean_area: float | None = None
589597
clean_count: int | None = None
590598
dust_collection_count: int | None = None
591599
records: list[int] | None = None
592600
last_clean_t: int | None = None
593601

594-
def __post_init__(self) -> None:
602+
@property
603+
def square_meter_clean_area(self) -> float | None:
604+
"""Returns the clean area in square meters."""
595605
if isinstance(self.clean_area, list | str):
596606
_LOGGER.warning(f"Clean area is a unexpected type! Please give the following in a issue: {self.clean_area}")
597-
else:
598-
self.square_meter_clean_area = round(self.clean_area / 1000000, 1) if self.clean_area is not None else None
607+
return None
608+
return round(self.clean_area / 1000000, 1) if self.clean_area is not None else None
599609

600610

601611
@dataclass
602612
class CleanRecord(RoborockBase):
603613
begin: int | None = None
604-
begin_datetime: datetime.datetime | None = None
605614
end: int | None = None
606-
end_datetime: datetime.datetime | None = None
607615
duration: int | None = None
608616
area: int | None = None
609-
square_meter_area: float | None = None
610617
error: int | None = None
611618
complete: int | None = None
612619
start_type: RoborockStartType | None = None
@@ -617,12 +624,17 @@ class CleanRecord(RoborockBase):
617624
wash_count: int | None = None
618625
map_flag: int | None = None
619626

620-
def __post_init__(self) -> None:
621-
self.square_meter_area = round(self.area / 1000000, 1) if self.area is not None else None
622-
self.begin_datetime = (
623-
datetime.datetime.fromtimestamp(self.begin).astimezone(timezone.utc) if self.begin else None
624-
)
625-
self.end_datetime = datetime.datetime.fromtimestamp(self.end).astimezone(timezone.utc) if self.end else None
627+
@property
628+
def sqaure_meter_area(self) -> float | None:
629+
return round(self.area / 1000000, 1) if self.area is not None else None
630+
631+
@property
632+
def begin_datetime(self) -> datetime.datetime | None:
633+
return datetime.datetime.fromtimestamp(self.begin).astimezone(timezone.utc) if self.begin else None
634+
635+
@property
636+
def end_datetime(self) -> datetime.datetime | None:
637+
return datetime.datetime.fromtimestamp(self.end).astimezone(timezone.utc) if self.end else None
626638

627639

628640
@dataclass
@@ -636,44 +648,46 @@ class Consumable(RoborockBase):
636648
dust_collection_work_times: int | None = None
637649
cleaning_brush_work_times: int | None = None
638650
moproller_work_time: int | None = None
639-
main_brush_time_left: int | None = None
640-
side_brush_time_left: int | None = None
641-
filter_time_left: int | None = None
642-
sensor_time_left: int | None = None
643-
strainer_time_left: int | None = None
644-
dust_collection_time_left: int | None = None
645-
cleaning_brush_time_left: int | None = None
646-
mop_roller_time_left: int | None = None
647-
648-
def __post_init__(self) -> None:
649-
self.main_brush_time_left = (
650-
MAIN_BRUSH_REPLACE_TIME - self.main_brush_work_time if self.main_brush_work_time is not None else None
651-
)
652-
self.side_brush_time_left = (
653-
SIDE_BRUSH_REPLACE_TIME - self.side_brush_work_time if self.side_brush_work_time is not None else None
654-
)
655-
self.filter_time_left = (
656-
FILTER_REPLACE_TIME - self.filter_work_time if self.filter_work_time is not None else None
657-
)
658-
self.sensor_time_left = (
659-
SENSOR_DIRTY_REPLACE_TIME - self.sensor_dirty_time if self.sensor_dirty_time is not None else None
660-
)
661-
self.strainer_time_left = (
662-
STRAINER_REPLACE_TIME - self.strainer_work_times if self.strainer_work_times is not None else None
663-
)
664-
self.dust_collection_time_left = (
651+
652+
@property
653+
def main_brush_time_left(self) -> int | None:
654+
return MAIN_BRUSH_REPLACE_TIME - self.main_brush_work_time if self.main_brush_work_time is not None else None
655+
656+
@property
657+
def side_brush_time_left(self) -> int | None:
658+
return SIDE_BRUSH_REPLACE_TIME - self.side_brush_work_time if self.side_brush_work_time is not None else None
659+
660+
@property
661+
def filter_time_left(self) -> int | None:
662+
return FILTER_REPLACE_TIME - self.filter_work_time if self.filter_work_time is not None else None
663+
664+
@property
665+
def sensor_time_left(self) -> int | None:
666+
return SENSOR_DIRTY_REPLACE_TIME - self.sensor_dirty_time if self.sensor_dirty_time is not None else None
667+
668+
@property
669+
def strainer_time_left(self) -> int | None:
670+
return STRAINER_REPLACE_TIME - self.strainer_work_times if self.strainer_work_times is not None else None
671+
672+
@property
673+
def dust_collection_time_left(self) -> int | None:
674+
return (
665675
DUST_COLLECTION_REPLACE_TIME - self.dust_collection_work_times
666676
if self.dust_collection_work_times is not None
667677
else None
668678
)
669-
self.cleaning_brush_time_left = (
679+
680+
@property
681+
def cleaning_brush_time_left(self) -> int | None:
682+
return (
670683
CLEANING_BRUSH_REPLACE_TIME - self.cleaning_brush_work_times
671684
if self.cleaning_brush_work_times is not None
672685
else None
673686
)
674-
self.mop_roller_time_left = (
675-
MOP_ROLLER_REPLACE_TIME - self.moproller_work_time if self.moproller_work_time is not None else None
676-
)
687+
688+
@property
689+
def mop_roller_time_left(self) -> int | None:
690+
return MOP_ROLLER_REPLACE_TIME - self.moproller_work_time if self.moproller_work_time is not None else None
677691

678692

679693
@dataclass
@@ -757,11 +771,11 @@ class DeviceData(RoborockBase):
757771
device: HomeDataDevice
758772
model: str
759773
host: str | None = None
760-
product_nickname: RoborockProductNickname | None = None
761774
device_features: DeviceFeatures | None = None
762775

763-
def __post_init__(self):
764-
self.product_nickname = SHORT_MODEL_TO_ENUM.get(self.model.split(".")[-1], RoborockProductNickname.PEARLPLUS)
776+
@property
777+
def product_nickname(self) -> RoborockProductNickname:
778+
return SHORT_MODEL_TO_ENUM.get(self.model.split(".")[-1], RoborockProductNickname.PEARLPLUS)
765779

766780

767781
@dataclass
@@ -846,11 +860,12 @@ class RoborockProduct(RoborockBase):
846860
agreements: list | None = None
847861
cardspec: str | None = None
848862
plugin_pic_url: str | None = None
849-
products_specification: RoborockProductSpec | None = None
850863

851-
def __post_init__(self):
864+
@property
865+
def product_nickname(self) -> RoborockProductNickname | None:
852866
if self.cardspec:
853-
self.products_specification = RoborockProductSpec.from_dict(json.loads(self.cardspec).get("data"))
867+
return RoborockProductSpec.from_dict(json.loads(self.cardspec).get("data"))
868+
return None
854869

855870

856871
@dataclass

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, square_meter_clean_area=91.3, 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, error_code_name='none', state_name='charging', water_box_mode_name='custom', fan_power_options=['off', 'quiet', 'balanced', 'turbo', 'max', 'custom', 'max_plus'], fan_power_name='custom', mop_mode_name=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, 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)
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, square_meter_clean_area=24258.1, 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)
1010
# ---
1111
# name: test_device_trait_command_parsing[payload3-<lambda>]
1212
SoundVolumeTrait(volume=90)

0 commit comments

Comments
 (0)