Skip to content

Commit

Permalink
Rename properties to descriptors for devicestatus (#1870)
Browse files Browse the repository at this point in the history
  • Loading branch information
rytilahti committed Dec 5, 2023
1 parent 36b4025 commit c2ddfaa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
23 changes: 6 additions & 17 deletions miio/devicestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import attr

from .descriptorcollection import DescriptorCollection
from .descriptors import (
AccessFlags,
ActionDescriptor,
Expand All @@ -34,7 +35,7 @@ class _StatusMeta(type):
def __new__(metacls, name, bases, namespace, **kwargs):
cls = super().__new__(metacls, name, bases, namespace)

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

Expand Down Expand Up @@ -86,25 +87,13 @@ def __repr__(self):
s += ">"
return s

def properties(self) -> Dict[str, PropertyDescriptor]:
def descriptors(self) -> DescriptorCollection[PropertyDescriptor]:
"""Return the dict of sensors exposed by the status container.
Use @sensor and @setting decorators to define properties.
"""
return self._descriptors # type: ignore[attr-defined]

def settings(self) -> Dict[str, PropertyDescriptor]:
"""Return the dict of settings exposed by the status container.
This is just a dict of writable properties, see :meth:`properties`.
"""
# TODO: this is not probably worth having, remove?
return {
prop.id: prop
for prop in self.properties().values()
if prop.access & AccessFlags.Write
}

def embed(self, name: str, other: "DeviceStatus"):
"""Embed another status container to current one.
Expand All @@ -117,8 +106,8 @@ def embed(self, name: str, other: "DeviceStatus"):
self._embedded[name] = other
other._parent = self # type: ignore[attr-defined]

for property_name, prop in other.properties().items():
final_name = f"{name}__{property_name}"
for descriptor_name, prop in other.descriptors().items():
final_name = f"{name}__{descriptor_name}"

self._descriptors[final_name] = attr.evolve(
prop, status_attribute=final_name
Expand All @@ -132,7 +121,7 @@ def __dir__(self) -> Iterable[str]:
def __cli_output__(self) -> str:
"""Return a CLI formatted output of the status."""
out = ""
for descriptor in self.properties().values():
for descriptor in self.descriptors().values():
try:
value = getattr(self, descriptor.status_attribute)
except KeyError:
Expand Down
8 changes: 4 additions & 4 deletions miio/tests/test_devicestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def unknown(self):
pass

status = DecoratedProps()
sensors = status.properties()
sensors = status.descriptors()
assert len(sensors) == 3

all_kwargs = sensors["all_kwargs"]
Expand Down Expand Up @@ -274,19 +274,19 @@ def sub_sensor(self):
return "sub"

main = MainStatus()
assert len(main.properties()) == 1
assert len(main.descriptors()) == 1

sub = SubStatus()
main.embed("SubStatus", sub)
sensors = main.properties()
sensors = main.descriptors()
assert len(sensors) == 2
assert sub._parent == main

assert getattr(main, sensors["main_sensor"].status_attribute) == "main"
assert getattr(main, sensors["SubStatus__sub_sensor"].status_attribute) == "sub"

with pytest.raises(KeyError):
main.properties()["nonexisting_sensor"]
main.descriptors()["nonexisting_sensor"]

assert (
repr(main)
Expand Down

0 comments on commit c2ddfaa

Please sign in to comment.