Skip to content

Commit

Permalink
Use __ as delimiter for embedded statuses
Browse files Browse the repository at this point in the history
Leaves ":" free for other uses,
converts to use __getattr__ instead of __getattribute__ to avoid checking on every attribute access.
  • Loading branch information
rytilahti committed Nov 6, 2022
1 parent e6a0c45 commit 4045404
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions miio/devicestatus.py
Expand Up @@ -113,21 +113,21 @@ def embed(self, other: "DeviceStatus"):
self._embedded[other_name] = other

for name, sensor in other.sensors().items():
final_name = f"{other_name}:{name}"
final_name = f"{other_name}__{name}"
import attr

self._sensors[final_name] = attr.evolve(sensor, property=final_name)

for name, setting in other.settings().items():
final_name = f"{other_name}:{name}"
final_name = f"{other_name}__{name}"
self._settings[final_name] = attr.evolve(setting, property=final_name)

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

embed, prop = item.split(":")
embed, prop = item.split("__")
return getattr(self._embedded[embed], prop)


Expand Down
2 changes: 1 addition & 1 deletion miio/tests/test_devicestatus.py
Expand Up @@ -201,7 +201,7 @@ def sub_sensor(self):
assert len(sensors) == 2

assert getattr(main, sensors["main_sensor"].property) == "main"
assert getattr(main, sensors["SubStatus:sub_sensor"].property) == "sub"
assert getattr(main, sensors["SubStatus__sub_sensor"].property) == "sub"

with pytest.raises(KeyError):
main.sensors()["nonexisting_sensor"]
Expand Down

0 comments on commit 4045404

Please sign in to comment.