Skip to content

Commit

Permalink
Add parent reference to embedded containers (#1711)
Browse files Browse the repository at this point in the history
Allows embedded containers to access data from other embeddeds or the
main status.
  • Loading branch information
rytilahti authored Feb 3, 2023
1 parent 6a71870 commit c656903
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions miio/devicestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __new__(metacls, name, bases, namespace, **kwargs):
cls._sensors: Dict[str, SensorDescriptor] = {}
cls._settings: Dict[str, SettingDescriptor] = {}

cls._parent: Optional["DeviceStatus"] = None
cls._embedded: Dict[str, "DeviceStatus"] = {}

descriptor_map = {
Expand Down Expand Up @@ -117,6 +118,7 @@ def embed(self, other: "DeviceStatus"):
other_name = str(other.__class__.__name__)

self._embedded[other_name] = other
other._parent = self # type: ignore[attr-defined]

for name, sensor in other.sensors().items():
final_name = f"{other_name}__{name}"
Expand Down
1 change: 1 addition & 0 deletions miio/tests/test_devicestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def sub_sensor(self):
main.embed(sub)
sensors = main.sensors()
assert len(sensors) == 2
assert sub._parent == main

assert getattr(main, sensors["main_sensor"].property) == "main"
assert getattr(main, sensors["SubStatus__sub_sensor"].property) == "sub"
Expand Down

0 comments on commit c656903

Please sign in to comment.