Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require name for status embedding #1712

Merged
merged 2 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions miio/devicestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]:
Expand Down
6 changes: 3 additions & 3 deletions miio/integrations/roborock/vacuum/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions miio/integrations/viomi/viomi/viomivacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion miio/tests/test_devicestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down