Update docs for the new module attributes has/get feature (#1301)

This commit is contained in:
Steven B.
2024-11-26 08:38:20 +00:00
committed by GitHub
parent fe53cd7d9c
commit cb4e28394d
5 changed files with 70 additions and 34 deletions

View File

@@ -14,9 +14,17 @@ Light, AutoOff, Firmware etc.
>>> print(dev.alias)
Living Room Bulb
To see whether a device supports functionality check for the existence of the module:
To see whether a device supports a group of functionality
check for the existence of the module:
>>> if light := dev.modules.get("Light"):
>>> print(light.brightness)
100
To see whether a device supports specific functionality, you can check whether the
module has that feature:
>>> if light.has_feature("hsv"):
>>> print(light.hsv)
HSV(hue=0, saturation=100, value=100)
@@ -70,6 +78,9 @@ ModuleT = TypeVar("ModuleT", bound="Module")
class FeatureAttribute:
"""Class for annotating attributes bound to feature."""
def __repr__(self) -> str:
return "FeatureAttribute"
class Module(ABC):
"""Base class implemention for all modules.
@@ -147,6 +158,11 @@ class Module(ABC):
self._module = module
self._module_features: dict[str, Feature] = {}
@property
def _all_features(self) -> dict[str, Feature]:
"""Get the features for this module and any sub modules."""
return self._module_features
def has_feature(self, attribute: str | property | Callable) -> bool:
"""Return True if the module attribute feature is supported."""
return bool(self.get_feature(attribute))
@@ -247,7 +263,7 @@ def _get_bound_feature(
)
check = {attribute_name, attribute_callable}
for feature in module._module_features.values():
for feature in module._all_features.values():
if (getter := feature.attribute_getter) and getter in check:
return feature