-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
/
Copy pathsensor.py
381 lines (347 loc) · 13.7 KB
/
sensor.py
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""Support for a ScreenLogic Sensor."""
from collections.abc import Callable
from copy import copy
import dataclasses
import logging
from screenlogicpy.const.data import ATTR, DEVICE, GROUP, VALUE
from screenlogicpy.const.msg import CODE
from screenlogicpy.device_const.chemistry import DOSE_STATE
from screenlogicpy.device_const.pump import PUMP_TYPE
from screenlogicpy.device_const.system import EQUIPMENT_FLAG
from homeassistant.components.sensor import (
DOMAIN as SENSOR_DOMAIN,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .coordinator import ScreenlogicDataUpdateCoordinator
from .entity import (
ScreenLogicEntity,
ScreenLogicEntityDescription,
ScreenLogicPushEntity,
ScreenLogicPushEntityDescription,
)
from .types import ScreenLogicConfigEntry
from .util import cleanup_excluded_entity, get_ha_unit
_LOGGER = logging.getLogger(__name__)
@dataclasses.dataclass(frozen=True, kw_only=True)
class ScreenLogicSensorDescription(
SensorEntityDescription, ScreenLogicEntityDescription
):
"""Describes a ScreenLogic sensor."""
value_mod: Callable[[int | str], int | str] | None = None
@dataclasses.dataclass(frozen=True, kw_only=True)
class ScreenLogicPushSensorDescription(
ScreenLogicSensorDescription, ScreenLogicPushEntityDescription
):
"""Describes a ScreenLogic push sensor."""
SUPPORTED_CORE_SENSORS = [
ScreenLogicPushSensorDescription(
subscription_code=CODE.STATUS_CHANGED,
data_root=(DEVICE.CONTROLLER, GROUP.SENSOR),
key=VALUE.AIR_TEMPERATURE,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
translation_key="air_temperature",
),
]
SUPPORTED_PUMP_SENSORS = [
ScreenLogicSensorDescription(
data_root=(DEVICE.PUMP,),
key=VALUE.WATTS_NOW,
device_class=SensorDeviceClass.POWER,
),
ScreenLogicSensorDescription(
data_root=(DEVICE.PUMP,),
key=VALUE.GPM_NOW,
enabled_lambda=lambda type: type != PUMP_TYPE.INTELLIFLO_VS,
),
ScreenLogicSensorDescription(
data_root=(DEVICE.PUMP,),
key=VALUE.RPM_NOW,
enabled_lambda=lambda type: type != PUMP_TYPE.INTELLIFLO_VF,
),
]
SUPPORTED_INTELLICHEM_SENSORS = [
ScreenLogicPushSensorDescription(
subscription_code=CODE.STATUS_CHANGED,
data_root=(DEVICE.CONTROLLER, GROUP.SENSOR),
key=VALUE.ORP,
state_class=SensorStateClass.MEASUREMENT,
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.STATUS_CHANGED,
data_root=(DEVICE.CONTROLLER, GROUP.SENSOR),
key=VALUE.PH,
state_class=SensorStateClass.MEASUREMENT,
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.SENSOR),
key=VALUE.ORP_NOW,
state_class=SensorStateClass.MEASUREMENT,
translation_key="chem_now",
translation_placeholders={"chem": "ORP"},
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.SENSOR),
key=VALUE.PH_NOW,
state_class=SensorStateClass.MEASUREMENT,
translation_key="chem_now",
translation_placeholders={"chem": "pH"},
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.SENSOR),
key=VALUE.ORP_SUPPLY_LEVEL,
state_class=SensorStateClass.MEASUREMENT,
value_mod=lambda val: int(val) - 1,
translation_key="chem_supply_level",
translation_placeholders={"chem": "ORP"},
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.SENSOR),
key=VALUE.PH_SUPPLY_LEVEL,
state_class=SensorStateClass.MEASUREMENT,
value_mod=lambda val: int(val) - 1,
translation_key="chem_supply_level",
translation_placeholders={"chem": "pH"},
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.SENSOR),
key=VALUE.PH_PROBE_WATER_TEMP,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
translation_key="ph_probe_water_temp",
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.SENSOR),
key=VALUE.SATURATION,
state_class=SensorStateClass.MEASUREMENT,
translation_key="saturation",
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.CONFIGURATION),
key=VALUE.CALCIUM_HARDNESS,
entity_registry_enabled_default=False, # Superseded by number entity
translation_key="calcium_hardness",
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.CONFIGURATION),
key=VALUE.CYA,
entity_registry_enabled_default=False, # Superseded by number entity
translation_key="cya",
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.CONFIGURATION),
key=VALUE.ORP_SETPOINT,
translation_key="chem_setpoint",
translation_placeholders={"chem": "ORP"},
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.CONFIGURATION),
key=VALUE.PH_SETPOINT,
translation_key="chem_setpoint",
translation_placeholders={"chem": "pH"},
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.CONFIGURATION),
key=VALUE.TOTAL_ALKALINITY,
entity_registry_enabled_default=False, # Superseded by number entity
translation_key="total_alkalinity",
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.CONFIGURATION),
key=VALUE.SALT_TDS_PPM,
entity_registry_enabled_default=False, # Superseded by number entity
translation_key="salt_tds_ppm",
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.DOSE_STATUS),
key=VALUE.ORP_DOSING_STATE,
device_class=SensorDeviceClass.ENUM,
options=["Dosing", "Mixing", "Monitoring"],
value_mod=lambda val: DOSE_STATE(val).title,
translation_key="chem_dose_state",
translation_placeholders={"chem": "ORP"},
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.DOSE_STATUS),
key=VALUE.ORP_LAST_DOSE_TIME,
device_class=SensorDeviceClass.DURATION,
state_class=SensorStateClass.TOTAL_INCREASING,
translation_key="chem_last_dose_time",
translation_placeholders={"chem": "ORP"},
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.DOSE_STATUS),
key=VALUE.ORP_LAST_DOSE_VOLUME,
device_class=SensorDeviceClass.VOLUME,
state_class=SensorStateClass.TOTAL_INCREASING,
translation_key="chem_last_dose_volume",
translation_placeholders={"chem": "ORP"},
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.DOSE_STATUS),
key=VALUE.PH_DOSING_STATE,
device_class=SensorDeviceClass.ENUM,
options=["Dosing", "Mixing", "Monitoring"],
value_mod=lambda val: DOSE_STATE(val).title,
translation_key="chem_dose_state",
translation_placeholders={"chem": "pH"},
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.DOSE_STATUS),
key=VALUE.PH_LAST_DOSE_TIME,
device_class=SensorDeviceClass.DURATION,
state_class=SensorStateClass.TOTAL_INCREASING,
translation_key="chem_last_dose_time",
translation_placeholders={"chem": "pH"},
),
ScreenLogicPushSensorDescription(
subscription_code=CODE.CHEMISTRY_CHANGED,
data_root=(DEVICE.INTELLICHEM, GROUP.DOSE_STATUS),
key=VALUE.PH_LAST_DOSE_VOLUME,
device_class=SensorDeviceClass.VOLUME,
state_class=SensorStateClass.TOTAL_INCREASING,
translation_key="chem_last_dose_volume",
translation_placeholders={"chem": "pH"},
),
]
SUPPORTED_SCG_SENSORS = [
ScreenLogicSensorDescription(
data_root=(DEVICE.SCG, GROUP.SENSOR),
key=VALUE.SALT_PPM,
state_class=SensorStateClass.MEASUREMENT,
translation_key="salt_ppm",
),
ScreenLogicSensorDescription(
data_root=(DEVICE.SCG, GROUP.CONFIGURATION),
key=VALUE.SUPER_CHLOR_TIMER,
translation_key="super_chlor_timer",
),
]
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ScreenLogicConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entry."""
coordinator = config_entry.runtime_data
gateway = coordinator.gateway
entities: list[ScreenLogicSensor] = [
ScreenLogicPushSensor(coordinator, core_sensor_description)
for core_sensor_description in SUPPORTED_CORE_SENSORS
if (
gateway.get_data(
*core_sensor_description.data_root, core_sensor_description.key
)
is not None
)
]
for pump_index, pump_data in gateway.get_data(DEVICE.PUMP).items():
if not pump_data or not pump_data.get(VALUE.DATA):
continue
pump_type = pump_data[VALUE.TYPE]
for proto_pump_sensor_description in SUPPORTED_PUMP_SENSORS:
if not pump_data.get(proto_pump_sensor_description.key):
continue
entities.append(
ScreenLogicPumpSensor(
coordinator,
copy(proto_pump_sensor_description),
pump_index,
pump_type,
)
)
chem_sensor_description: ScreenLogicPushSensorDescription
for chem_sensor_description in SUPPORTED_INTELLICHEM_SENSORS:
chem_sensor_data_path = (
*chem_sensor_description.data_root,
chem_sensor_description.key,
)
if EQUIPMENT_FLAG.INTELLICHEM not in gateway.equipment_flags:
cleanup_excluded_entity(coordinator, SENSOR_DOMAIN, chem_sensor_data_path)
continue
if gateway.get_data(*chem_sensor_data_path):
chem_sensor_description = dataclasses.replace(
chem_sensor_description, entity_category=EntityCategory.DIAGNOSTIC
)
entities.append(ScreenLogicPushSensor(coordinator, chem_sensor_description))
scg_sensor_description: ScreenLogicSensorDescription
for scg_sensor_description in SUPPORTED_SCG_SENSORS:
scg_sensor_data_path = (
*scg_sensor_description.data_root,
scg_sensor_description.key,
)
if EQUIPMENT_FLAG.CHLORINATOR not in gateway.equipment_flags:
cleanup_excluded_entity(coordinator, SENSOR_DOMAIN, scg_sensor_data_path)
continue
if gateway.get_data(*scg_sensor_data_path):
scg_sensor_description = dataclasses.replace(
scg_sensor_description, entity_category=EntityCategory.DIAGNOSTIC
)
entities.append(ScreenLogicSensor(coordinator, scg_sensor_description))
async_add_entities(entities)
class ScreenLogicSensor(ScreenLogicEntity, SensorEntity):
"""Representation of a ScreenLogic sensor entity."""
entity_description: ScreenLogicSensorDescription
_attr_has_entity_name = True
def __init__(
self,
coordinator: ScreenlogicDataUpdateCoordinator,
entity_description: ScreenLogicSensorDescription,
) -> None:
"""Initialize of the entity."""
super().__init__(coordinator, entity_description)
self._attr_native_unit_of_measurement = get_ha_unit(
self.entity_data.get(ATTR.UNIT)
)
@property
def native_value(self) -> str | int | float:
"""State of the sensor."""
val = self.entity_data[ATTR.VALUE]
value_mod = self.entity_description.value_mod
return value_mod(val) if value_mod else val
class ScreenLogicPushSensor(ScreenLogicSensor, ScreenLogicPushEntity):
"""Representation of a ScreenLogic push sensor entity."""
entity_description: ScreenLogicPushSensorDescription
class ScreenLogicPumpSensor(ScreenLogicSensor):
"""Representation of a ScreenLogic pump sensor."""
_attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_state_class = SensorStateClass.MEASUREMENT
def __init__(
self,
coordinator: ScreenlogicDataUpdateCoordinator,
entity_description: ScreenLogicSensorDescription,
pump_index: int,
pump_type: int,
) -> None:
"""Initialize of the entity."""
entity_description = dataclasses.replace(
entity_description, data_root=(DEVICE.PUMP, pump_index)
)
super().__init__(coordinator, entity_description)
if entity_description.enabled_lambda:
self._attr_entity_registry_enabled_default = (
entity_description.enabled_lambda(pump_type)
)