Debian Codegraph @

blebox_uniapi/binary_sensor.py

raw symbols SCIP
1 from typing import Union, Optional, TYPE_CHECKING
2
3 from .feature import Feature
4
5 if TYPE_CHECKING:
6 from .box import Box
7
8
9 class BinarySensor(Feature):
10 """Class representing sensor with bool state."""
11
12 def __init__(
13 self,
14 product: "Box",
15 alias: str,
16 methods: dict,
17 sensor_id: Optional[int] = None,
18 name: Optional[str] = None,
19 ):
20 self._sensor_id = sensor_id
21 self._name = name
22 super().__init__(product, alias, methods)
23
24 @property
25 def index(self) -> Optional[int]:
26 return self._sensor_id
27
28 @property
29 def name(self) -> Optional[str]:
30 return self._name
31
32 @classmethod
33 def many_from_config(
34 cls, product, box_type_config, extended_state
35 ) -> list["Feature"]:
36 type_class_map = {
37 "rain": Rain,
38 "flood": Flood,
39 "openStatus": Open,
40 "binary": Input,
41 }
42
43 output_list = list()
44 sensors_list = extended_state.get("multiSensor").get("sensors", {})
45 alias, methods = box_type_config[0]
46
47 for sensor in sensors_list:
48 sensor_type = sensor.get("type")
49 sensor_id = sensor.get("id")
50
51 if sensor_type in type_class_map:
52 klass = type_class_map[sensor_type]
53
54 if methods.get(sensor_type) is not None:
55 value_method = {sensor_type: methods[sensor_type](sensor_id)}
56 output_list.append(
57 klass(
58 product=product,
59 alias=sensor_type + "_" + str(sensor_id),
60 methods=value_method,
61 sensor_id=sensor_id,
62 name=sensor.get("name"),
63 )
64 )
65
66 return output_list
67
68
69 class Rain(BinarySensor):
70 def __init__(
71 self,
72 product: "Box",
73 alias: str,
74 methods: dict,
75 sensor_id: Optional[int] = None,
76 name: Optional[str] = None,
77 ):
78 self._device_class = "moisture"
79 super().__init__(product, alias, methods, sensor_id=sensor_id, name=name)
80
81 @property
82 def state(self) -> bool:
83 return self._current > 0
84
85 @property
86 def device_class(self) -> str:
87 return self._device_class
88
89 def _read_rain(self, field: str) -> Union[float, int, None]:
90 product = self._product
91 if product.last_data is not None:
92 raw = self.raw_value(field)
93 if raw is not None: # no reading
94 return self.raw_value("rain")
95 return 0
96
97 def after_update(self) -> None:
98 self._current = self._read_rain("rain")
99
100
101 class Flood(BinarySensor):
102 def __init__(
103 self,
104 product: "Box",
105 alias: str,
106 methods: dict,
107 sensor_id: Optional[int] = None,
108 name: Optional[str] = None,
109 ):
110 self._device_class = "moisture"
111 super().__init__(product, alias, methods, sensor_id=sensor_id, name=name)
112
113 @property
114 def state(self) -> bool:
115 return self._current > 0
116
117 @property
118 def device_class(self) -> str:
119 return self._device_class
120
121 def _read_flood(self, field: str) -> Union[float, int, None]:
122 product = self._product
123
124 if product.last_data is not None:
125 raw = self.raw_value(field)
126 if raw is not None: # no reading
127 return self.raw_value("flood")
128 return 0
129
130 def after_update(self) -> None:
131 self._current = self._read_flood("flood")
132
133
134 class Open(BinarySensor):
135 def __init__(
136 self,
137 product: "Box",
138 alias: str,
139 methods: dict,
140 sensor_id: Optional[int] = None,
141 name: Optional[str] = None,
142 ):
143 self._device_class = "open"
144 super().__init__(product, alias, methods, sensor_id=sensor_id, name=name)
145
146 @property
147 def state(self) -> bool:
148 return self._current != 4 # 4 = closed, 0-3 = open
149
150 def _read_input(self, field: str) -> Union[float, int, None]:
151 product = self._product
152
153 if product.last_data is not None:
154 raw = self.raw_value(field)
155 if raw is not None: # no reading
156 return self.raw_value("openStatus")
157 return 0
158
159 def after_update(self) -> None:
160 self._current = self._read_input("openStatus")
161
162
163 class Input(BinarySensor):
164 def __init__(
165 self,
166 product: "Box",
167 alias: str,
168 methods: dict,
169 sensor_id: Optional[int] = None,
170 name: Optional[str] = None,
171 ):
172 self._device_class = "input"
173 super().__init__(product, alias, methods, sensor_id=sensor_id, name=name)
174
175 @property
176 def state(self) -> bool:
177 return self._current > 0
178
179 def _read_input(self, field: str) -> Union[float, int, None]:
180 product = self._product
181
182 if product.last_data is not None:
183 raw = self.raw_value(field)
184 if raw is not None: # no reading
185 return self.raw_value("binary")
186 return 0
187
188 def after_update(self) -> None:
189 self._current = self._read_input("binary")