Debian Codegraph @

blebox_uniapi/feature.py

raw symbols SCIP
1 from .error import DeviceStateNotAvailable
2 from typing import Any, TYPE_CHECKING, Union
3 from blebox_uniapi.jfollow import follow
4
5 if TYPE_CHECKING:
6 from .box import Box
7
8
9 class Feature:
10 _device_class: str
11
12 def __init__(self, product: "Box", alias: str, methods: dict):
13 self._product = product
14 self._alias = alias
15 self._methods = methods
16
17 @classmethod
18 def many_from_config(
19 cls, product, box_type_config, extended_state
20 ) -> list["Feature"]:
21 # note: by default single config entry yields single feature instance but certain feature
22 # domains (e.g. lights) may handle this differently depending on their `extended_state`
23 return [cls(product, *args) for args in box_type_config]
24
25 @property
26 def unique_id(self) -> str:
27 return f"BleBox-{self._product.type}-{self._product.unique_id}-{self._alias}"
28
29 async def async_update(self) -> None:
30 await self._product.async_update_data()
31
32 @property
33 def full_name(self) -> str:
34 product = self._product
35 return f"{product.name} ({product.type}#{self._alias})"
36
37 @property
38 def device_class(self) -> str:
39 return self._device_class
40
41 @property
42 def product(self) -> "Box":
43 return self._product
44
45 @property
46 def alias(self):
47 return self._alias
48
49 # TODO: (cleanup) move to product/box ?
50 def raw_value(self, name: str) -> Any:
51 product = self._product
52 # TODO: better exception?
53 if product.last_data is None:
54 # TODO: coverage
55 raise DeviceStateNotAvailable # pragma: no cover
56 methods = self._methods
57 if method := methods.get(name):
58 return follow(product.last_data, method)
59 return None
60
61 async def async_api_command(self, *args: Any, **kwargs: Any) -> None:
62 await self._product.async_api_command(*args, **kwargs)
63
64 @staticmethod
65 def resolve_access_method_paths(
66 methods: dict[str, Union[str, callable]], id_val: str = None
67 ) -> dict[str, str]:
68 """Return dict with resolved callable used as data path."""
69 new = dict()
70 if not isinstance(methods, dict):
71 raise TypeError(
72 f"Parameter methods should be dict, instead of {type(methods)}."
73 )
74 for key, value in methods.items():
75 if callable(value):
76 new[key] = value(id_val)
77 else:
78 new[key] = value
79 return new