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

Fix access to embedded status containers #1682

Merged
merged 2 commits into from
Jan 24, 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
13 changes: 13 additions & 0 deletions miio/devicestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import (
Callable,
Dict,
Iterable,
Optional,
Type,
Union,
Expand Down Expand Up @@ -124,11 +125,23 @@ def embed(self, other: "DeviceStatus"):
final_name = f"{other_name}__{name}"
self._settings[final_name] = attr.evolve(setting, property=final_name)

def __dir__(self) -> Iterable[str]:
"""Overridden to include properties from embedded containers."""
return (
list(super().__dir__())
+ list(self._embedded)
+ list(self._sensors)
+ list(self._settings)
)

def __getattr__(self, item):
"""Overridden to lookup properties from embedded containers."""
if item.startswith("__") and item.endswith("__"):
return super().__getattribute__(item)

if item in self._embedded:
return self._embedded[item]

embed, prop = item.split("__", maxsplit=1)
if not embed or not prop:
return super().__getattribute__(item)
Expand Down
7 changes: 7 additions & 0 deletions miio/tests/test_devicestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,10 @@ def sub_sensor(self):
repr(main)
== "<MainStatus main_sensor=main SubStatus=<SubStatus sub_sensor=sub>>"
)

# Test attribute access to the sub status
assert isinstance(main.SubStatus, SubStatus)

# Test that __dir__ is implemented correctly
assert "SubStatus" in dir(main)
assert "SubStatus__sub_sensor" in dir(main)