From cf702df6c8078e7fc8148148e701e1bbaab98dfd Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Fri, 3 Feb 2023 18:29:07 +0100 Subject: [PATCH 1/2] Require name for status embedding This will remove the magic naming for embedded containers --- miio/devicestatus.py | 14 ++++++-------- miio/integrations/viomi/viomi/viomivacuum.py | 4 ++-- miio/tests/test_devicestatus.py | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/miio/devicestatus.py b/miio/devicestatus.py index 0554292fa..589b29eff 100644 --- a/miio/devicestatus.py +++ b/miio/devicestatus.py @@ -106,7 +106,7 @@ def settings(self) -> Dict[str, SettingDescriptor]: """ return self._settings # type: ignore[attr-defined] - def embed(self, other: "DeviceStatus"): + def embed(self, name: str, other: "DeviceStatus"): """Embed another status container to current one. This makes it easy to provide a single status response for cases where responses @@ -115,18 +115,16 @@ def embed(self, other: "DeviceStatus"): Internally, this will prepend the name of the other class to the property names, and override the __getattribute__ to lookup attributes in the embedded containers. """ - other_name = str(other.__class__.__name__) - - self._embedded[other_name] = other + self._embedded[name] = other other._parent = self # type: ignore[attr-defined] - for name, sensor in other.sensors().items(): - final_name = f"{other_name}__{name}" + for sensor_name, sensor in other.sensors().items(): + final_name = f"{name}__{sensor_name}" self._sensors[final_name] = attr.evolve(sensor, property=final_name) - for name, setting in other.settings().items(): - final_name = f"{other_name}__{name}" + for setting_name, setting in other.settings().items(): + final_name = f"{name}__{setting_name}" self._settings[final_name] = attr.evolve(setting, property=final_name) def __dir__(self) -> Iterable[str]: diff --git a/miio/integrations/viomi/viomi/viomivacuum.py b/miio/integrations/viomi/viomi/viomivacuum.py index 575290272..deac2af39 100644 --- a/miio/integrations/viomi/viomi/viomivacuum.py +++ b/miio/integrations/viomi/viomi/viomivacuum.py @@ -692,8 +692,8 @@ def status(self) -> ViomiVacuumStatus: values = self.get_properties(properties) status = ViomiVacuumStatus(defaultdict(lambda: None, zip(properties, values))) - status.embed(self.consumable_status()) - status.embed(self.dnd_status()) + status.embed("consumables", self.consumable_status()) + status.embed("dnd", self.dnd_status()) return status diff --git a/miio/tests/test_devicestatus.py b/miio/tests/test_devicestatus.py index 92de0611a..e6c30e4d7 100644 --- a/miio/tests/test_devicestatus.py +++ b/miio/tests/test_devicestatus.py @@ -267,7 +267,7 @@ def sub_sensor(self): assert len(main.sensors()) == 1 sub = SubStatus() - main.embed(sub) + main.embed("SubStatus", sub) sensors = main.sensors() assert len(sensors) == 2 assert sub._parent == main From 1d5537b820aa174c5442e7fbf75a9513d813801d Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Fri, 3 Feb 2023 18:42:51 +0100 Subject: [PATCH 2/2] Add names for roborock --- miio/integrations/roborock/vacuum/vacuum.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/miio/integrations/roborock/vacuum/vacuum.py b/miio/integrations/roborock/vacuum/vacuum.py index af20a4dd3..449a9a4e8 100644 --- a/miio/integrations/roborock/vacuum/vacuum.py +++ b/miio/integrations/roborock/vacuum/vacuum.py @@ -338,9 +338,9 @@ def manual_control( def status(self) -> VacuumStatus: """Return status of the vacuum.""" status = self.vacuum_status() - status.embed(self.consumable_status()) - status.embed(self.clean_history()) - status.embed(self.dnd_status()) + status.embed("consumables", self.consumable_status()) + status.embed("cleaning_history", self.clean_history()) + status.embed("dnd", self.dnd_status()) return status @command()