From 20b162a3eff6f3d735c0642dbbbadae272196cbe Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Fri, 3 Feb 2023 16:36:01 +0100 Subject: [PATCH] Add parent reference to embedded containers --- miio/devicestatus.py | 2 ++ miio/tests/test_devicestatus.py | 1 + 2 files changed, 3 insertions(+) diff --git a/miio/devicestatus.py b/miio/devicestatus.py index 4818a3cc8..0554292fa 100644 --- a/miio/devicestatus.py +++ b/miio/devicestatus.py @@ -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 = { @@ -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}" diff --git a/miio/tests/test_devicestatus.py b/miio/tests/test_devicestatus.py index edd3d177a..92de0611a 100644 --- a/miio/tests/test_devicestatus.py +++ b/miio/tests/test_devicestatus.py @@ -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"