File: sensor.py

package info (click to toggle)
blebox-uniapi 2.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 520 kB
  • sloc: python: 4,994; makefile: 85; sh: 5
file content (276 lines) | stat: -rw-r--r-- 9,495 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import datetime
import numbers
from functools import partial

from .feature import Feature
from typing import TYPE_CHECKING, Union, Optional

if TYPE_CHECKING:
    from .box import Box


class SensorFactory:
    device_constructors: dict[str, type] = {}

    @classmethod
    def register(cls, sensor_type: str, **kwargs):
        if sensor_type in cls.device_constructors:
            raise RuntimeError("Can't register same sensor type twice")

        def decorator(registrable: type):
            constructor = registrable
            if kwargs:
                constructor = partial(registrable, sensor_type=sensor_type, **kwargs)

            cls.device_constructors[sensor_type] = constructor
            # note: returning unmodified, so we can register registrable
            # multiple times under different names and with different kwargs
            return registrable

        return decorator

    @staticmethod
    def _sensor_states(extended_state: dict):
        """Read potential sensor states from extended state dictionary"""
        # note: probably we should iterate extended state in future if there
        # are other api flavours other than multiSensor that provide sensors
        states = extended_state.get("multiSensor", {}).get("sensors", [])
        # note: but for now we are only able to support non-multisensor devices
        # that provide sensor data in extended data payload root
        states.extend(extended_state.get("sensors", []))
        # note: power measuring feature predates multiSensor API, so we need a small
        # shim to adapt older shape of power measuring schema to the new sensor API
        if "powerMeasuring" in extended_state:
            power_states = extended_state["powerMeasuring"].get("powerConsumption", [])
            # note: be careful of names as this has been historically named differently
            # in home-assistant
            states.extend({"type": "powerConsumption", **s} for s in power_states)
        return states

    @classmethod
    def many_from_config(cls, product, box_type_config, extended_state):
        if extended_state:
            object_list = []
            # note: first item was historically an alias, but it has been since
            # abandoned. We still keep it in the box config.
            _, methods = box_type_config[0]

            for sensor in cls._sensor_states(extended_state):
                device_class = sensor.get("type")
                sensor_id = sensor.get("id")

                alias = device_class
                if sensor_id is not None:
                    alias = f"{device_class}_{sensor_id}"

                if constructor := cls.device_constructors.get(device_class):
                    # note: methods for sensor readings are provided as template
                    # functions (lambdas) in the box config. We need to "materialize"
                    # them to make sure they are properly indexed by sensor ID
                    materialized_methods = {
                        **methods,
                        device_class: methods[device_class](sensor_id),
                    }

                    feature = constructor(
                        product=product,
                        alias=alias,
                        methods=materialized_methods,
                        sensor_id=sensor_id,
                    )
                    object_list.append(feature)

            return object_list

        # legacy handling of some old device API that do not provide extended state
        alias, methods = box_type_config[0]
        if alias.endswith("air"):
            method_list = [method for method in methods if "value" in method]
            return [
                AirQuality(product=product, alias=method.split(".")[0], methods=methods)
                for method in method_list
            ]
        if alias.endswith("temperature"):
            return [Temperature(product=product, alias=alias, methods=methods)]
        else:
            return []


class BaseSensor(Feature):
    _unit: str
    _device_class: str
    _native_value: Union[float, int, str]
    _sensor_type: Optional[str]
    _sensor_id: Optional[int]

    def __init__(
        self,
        product: "Box",
        alias: str,
        methods: dict,
        sensor_type: str = None,
        sensor_id: Optional[int] = None,
    ):
        self._sensor_type = sensor_type
        self._sensor_id = sensor_id
        super().__init__(product, alias, methods)

    @property
    def unit(self) -> str:
        return self._unit

    @property
    def device_class(self) -> str:
        return self._device_class

    @property
    def native_value(self):
        return self._native_value

    @property
    def sensor_id(self):
        return self._sensor_id

    @property
    def probe_id(self):
        return self.sensor_id

    @classmethod
    def many_from_config(cls, product, box_type_config, extended_state):
        raise NotImplementedError("Please use SensorFactory")

    def __str__(self):
        return f"<{self.__class__.__name__} sensor_type={self._sensor_type}, alias={self._alias}>"


@SensorFactory.register("frequency", unit="Hz", scale=1_000)
@SensorFactory.register("current", unit="mA", scale=1_000)
@SensorFactory.register("voltage", unit="V", scale=10)
@SensorFactory.register("apparentPower", unit="va")
@SensorFactory.register("reactivePower", unit="var")
@SensorFactory.register("activePower", unit="W")
@SensorFactory.register("reverseActiveEnergy", unit="kWh")
@SensorFactory.register("forwardActiveEnergy", unit="kWh")
@SensorFactory.register("illuminance", unit="lx", scale=100)
@SensorFactory.register("humidity", unit="percentage", scale=100)
@SensorFactory.register("wind", unit="m/s", scale=10)
class GenericSensor(BaseSensor):
    def __init__(
        # base sensor params
        self,
        product: "Box",
        alias: str,
        methods: dict,
        sensor_id: Optional[int],
        *,
        # generalization params
        sensor_type: str,
        unit: str,
        scale: float = 1,
        precision: Optional[int] = None,
    ):
        super().__init__(product, alias, methods, sensor_id=sensor_id)
        self._unit = unit
        self._scale = scale
        self._precision = precision
        # note: this seems redundant but there is at least one sensor type that
        # has different mapping in home assistant (wind/wind_speed). Should be
        # fixed in upstream first.
        self._device_class = sensor_type
        self._sensor_type = sensor_type

    def after_update(self):
        product = self._product
        if product.last_data is None:
            return

        raw = self.raw_value(self._device_class)
        if not isinstance(raw, numbers.Number):
            raw = float("nan")

        native = raw / self._scale
        if self._precision:
            native = round(native, self._precision)

        self._native_value = native


@SensorFactory.register("powerConsumption", unit="kWh")
class PowerConsumption(GenericSensor):
    # note: almost the same as typical generic sensor but also provides extra property
    # to read last reset value
    @property
    def last_reset(self):
        return datetime.datetime.now() - datetime.timedelta(
            seconds=self._read_period_of_measurement()
        )

    def _read_period_of_measurement(self) -> int:
        product = self._product
        if product.last_data is not None:
            raw = self.raw_value("periodS")
            if raw is not None:
                alias = self._alias
                return product.expect_int(alias, raw, 3600, 0)
        return 0


@SensorFactory.register("temperature")
class Temperature(BaseSensor):
    _current: Union[float, int, None]

    def __init__(
        self,
        product: "Box",
        alias: str,
        methods: dict,
        sensor_id: Optional[int] = None,
    ):
        super().__init__(product, alias, methods, sensor_id=sensor_id)
        self._unit = "celsius"
        self._device_class = "temperature"

    @property
    def current(self) -> Union[float, int, None]:
        return self._current

    def _read_temperature(self, field: str) -> Union[float, int, None]:
        product = self._product
        if product.last_data is not None:
            raw = self.raw_value(field)
            if raw is not None:
                alias = self._alias
                return round(product.expect_int(alias, raw, 12500, -5500) / 100.0, 1)
        return None

    def after_update(self) -> None:
        self._current = self._read_temperature("temperature")
        self._native_value = self._read_temperature("temperature")


@SensorFactory.register("airSensor")
class AirQuality(BaseSensor):
    _pm: Optional[int]

    def __init__(
        self,
        product: "Box",
        alias: str,
        methods: dict,
        sensor_id: Optional[str] = None,
    ):
        super().__init__(product, alias, methods, sensor_id)
        self._unit = "concentration_of_mp"
        self._device_class = alias

    def _pm_value(self, name: str) -> Optional[int]:
        product = self._product
        if product.last_data is not None:
            raw = self.raw_value(name)
            if raw is not None:
                alias = self._alias
                return product.expect_int(alias, raw, 3000, 0)
        return None

    def after_update(self) -> None:
        self._native_value = self._pm_value(f"{self.device_class}.value")