Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for a ScreenLogic Binary Sensor."""
2 
3 from copy import copy
4 import dataclasses
5 
6 from screenlogicpy.const.common import ON_OFF
7 from screenlogicpy.const.data import ATTR, DEVICE, GROUP, VALUE
8 from screenlogicpy.const.msg import CODE
9 from screenlogicpy.device_const.system import EQUIPMENT_FLAG
10 
12  DOMAIN as BINARY_SENSOR_DOMAIN,
13  BinarySensorDeviceClass,
14  BinarySensorEntity,
15  BinarySensorEntityDescription,
16 )
17 from homeassistant.const import EntityCategory
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .coordinator import ScreenlogicDataUpdateCoordinator
22 from .entity import (
23  ScreenLogicEntity,
24  ScreenLogicEntityDescription,
25  ScreenLogicPushEntity,
26  ScreenLogicPushEntityDescription,
27 )
28 from .types import ScreenLogicConfigEntry
29 from .util import cleanup_excluded_entity
30 
31 
32 @dataclasses.dataclass(frozen=True, kw_only=True)
34  BinarySensorEntityDescription, ScreenLogicEntityDescription
35 ):
36  """A class that describes ScreenLogic binary sensor eneites."""
37 
38 
39 @dataclasses.dataclass(frozen=True, kw_only=True)
41  ScreenLogicBinarySensorDescription, ScreenLogicPushEntityDescription
42 ):
43  """Describes a ScreenLogicPushBinarySensor."""
44 
45 
46 SUPPORTED_CORE_SENSORS = [
48  subscription_code=CODE.STATUS_CHANGED,
49  data_root=(DEVICE.CONTROLLER, GROUP.SENSOR),
50  key=VALUE.ACTIVE_ALERT,
51  device_class=BinarySensorDeviceClass.PROBLEM,
52  ),
54  subscription_code=CODE.STATUS_CHANGED,
55  data_root=(DEVICE.CONTROLLER, GROUP.SENSOR),
56  key=VALUE.CLEANER_DELAY,
57  ),
59  subscription_code=CODE.STATUS_CHANGED,
60  data_root=(DEVICE.CONTROLLER, GROUP.SENSOR),
61  key=VALUE.FREEZE_MODE,
62  ),
64  subscription_code=CODE.STATUS_CHANGED,
65  data_root=(DEVICE.CONTROLLER, GROUP.SENSOR),
66  key=VALUE.POOL_DELAY,
67  ),
69  subscription_code=CODE.STATUS_CHANGED,
70  data_root=(DEVICE.CONTROLLER, GROUP.SENSOR),
71  key=VALUE.SPA_DELAY,
72  ),
73 ]
74 
75 SUPPORTED_PUMP_SENSORS = [
77  data_root=(DEVICE.PUMP,),
78  key=VALUE.STATE,
79  )
80 ]
81 
82 SUPPORTED_INTELLICHEM_SENSORS = [
84  subscription_code=CODE.CHEMISTRY_CHANGED,
85  data_root=(DEVICE.INTELLICHEM, GROUP.ALARM),
86  key=VALUE.FLOW_ALARM,
87  device_class=BinarySensorDeviceClass.PROBLEM,
88  ),
90  subscription_code=CODE.CHEMISTRY_CHANGED,
91  data_root=(DEVICE.INTELLICHEM, GROUP.ALARM),
92  key=VALUE.ORP_HIGH_ALARM,
93  device_class=BinarySensorDeviceClass.PROBLEM,
94  ),
96  subscription_code=CODE.CHEMISTRY_CHANGED,
97  data_root=(DEVICE.INTELLICHEM, GROUP.ALARM),
98  key=VALUE.ORP_LOW_ALARM,
99  device_class=BinarySensorDeviceClass.PROBLEM,
100  ),
102  subscription_code=CODE.CHEMISTRY_CHANGED,
103  data_root=(DEVICE.INTELLICHEM, GROUP.ALARM),
104  key=VALUE.ORP_SUPPLY_ALARM,
105  device_class=BinarySensorDeviceClass.PROBLEM,
106  ),
108  subscription_code=CODE.CHEMISTRY_CHANGED,
109  data_root=(DEVICE.INTELLICHEM, GROUP.ALARM),
110  key=VALUE.PH_HIGH_ALARM,
111  device_class=BinarySensorDeviceClass.PROBLEM,
112  ),
114  subscription_code=CODE.CHEMISTRY_CHANGED,
115  data_root=(DEVICE.INTELLICHEM, GROUP.ALARM),
116  key=VALUE.PH_LOW_ALARM,
117  device_class=BinarySensorDeviceClass.PROBLEM,
118  ),
120  subscription_code=CODE.CHEMISTRY_CHANGED,
121  data_root=(DEVICE.INTELLICHEM, GROUP.ALARM),
122  key=VALUE.PH_SUPPLY_ALARM,
123  device_class=BinarySensorDeviceClass.PROBLEM,
124  ),
126  subscription_code=CODE.CHEMISTRY_CHANGED,
127  data_root=(DEVICE.INTELLICHEM, GROUP.ALARM),
128  key=VALUE.PROBE_FAULT_ALARM,
129  device_class=BinarySensorDeviceClass.PROBLEM,
130  ),
132  subscription_code=CODE.CHEMISTRY_CHANGED,
133  data_root=(DEVICE.INTELLICHEM, GROUP.ALERT),
134  key=VALUE.ORP_LIMIT,
135  ),
137  subscription_code=CODE.CHEMISTRY_CHANGED,
138  data_root=(DEVICE.INTELLICHEM, GROUP.ALERT),
139  key=VALUE.PH_LIMIT,
140  ),
142  subscription_code=CODE.CHEMISTRY_CHANGED,
143  data_root=(DEVICE.INTELLICHEM, GROUP.ALERT),
144  key=VALUE.PH_LOCKOUT,
145  ),
147  subscription_code=CODE.CHEMISTRY_CHANGED,
148  data_root=(DEVICE.INTELLICHEM, GROUP.WATER_BALANCE),
149  key=VALUE.CORROSIVE,
150  device_class=BinarySensorDeviceClass.PROBLEM,
151  ),
153  subscription_code=CODE.CHEMISTRY_CHANGED,
154  data_root=(DEVICE.INTELLICHEM, GROUP.WATER_BALANCE),
155  key=VALUE.SCALING,
156  device_class=BinarySensorDeviceClass.PROBLEM,
157  ),
158 ]
159 
160 SUPPORTED_SCG_SENSORS = [
162  data_root=(DEVICE.SCG, GROUP.SENSOR),
163  key=VALUE.STATE,
164  )
165 ]
166 
167 
169  hass: HomeAssistant,
170  config_entry: ScreenLogicConfigEntry,
171  async_add_entities: AddEntitiesCallback,
172 ) -> None:
173  """Set up entry."""
174  coordinator = config_entry.runtime_data
175  gateway = coordinator.gateway
176 
177  entities: list[ScreenLogicBinarySensor] = [
178  ScreenLogicPushBinarySensor(coordinator, core_sensor_description)
179  for core_sensor_description in SUPPORTED_CORE_SENSORS
180  if (
181  gateway.get_data(
182  *core_sensor_description.data_root, core_sensor_description.key
183  )
184  is not None
185  )
186  ]
187 
188  for p_index, p_data in gateway.get_data(DEVICE.PUMP).items():
189  if not p_data or not p_data.get(VALUE.DATA):
190  continue
191  entities.extend(
193  coordinator, copy(proto_pump_sensor_description), p_index
194  )
195  for proto_pump_sensor_description in SUPPORTED_PUMP_SENSORS
196  )
197 
198  chem_sensor_description: ScreenLogicPushBinarySensorDescription
199  for chem_sensor_description in SUPPORTED_INTELLICHEM_SENSORS:
200  chem_sensor_data_path = (
201  *chem_sensor_description.data_root,
202  chem_sensor_description.key,
203  )
204  if EQUIPMENT_FLAG.INTELLICHEM not in gateway.equipment_flags:
206  coordinator, BINARY_SENSOR_DOMAIN, chem_sensor_data_path
207  )
208  continue
209  if gateway.get_data(*chem_sensor_data_path):
210  entities.append(
211  ScreenLogicPushBinarySensor(coordinator, chem_sensor_description)
212  )
213 
214  scg_sensor_description: ScreenLogicBinarySensorDescription
215  for scg_sensor_description in SUPPORTED_SCG_SENSORS:
216  scg_sensor_data_path = (
217  *scg_sensor_description.data_root,
218  scg_sensor_description.key,
219  )
220  if EQUIPMENT_FLAG.CHLORINATOR not in gateway.equipment_flags:
222  coordinator, BINARY_SENSOR_DOMAIN, scg_sensor_data_path
223  )
224  continue
225  if gateway.get_data(*scg_sensor_data_path):
226  entities.append(
227  ScreenLogicBinarySensor(coordinator, scg_sensor_description)
228  )
229 
230  async_add_entities(entities)
231 
232 
234  """Representation of a ScreenLogic binary sensor entity."""
235 
236  entity_description: ScreenLogicBinarySensorDescription
237  _attr_has_entity_name = True
238  _attr_entity_category = EntityCategory.DIAGNOSTIC
239 
240  @property
241  def is_on(self) -> bool:
242  """Determine if the sensor is on."""
243  return self.entity_dataentity_data[ATTR.VALUE] == ON_OFF.ON
244 
245 
247  """Representation of a ScreenLogic push binary sensor entity."""
248 
249  entity_description: ScreenLogicPushBinarySensorDescription
250 
251 
253  """Representation of a ScreenLogic binary sensor entity for pump data."""
254 
255  def __init__(
256  self,
257  coordinator: ScreenlogicDataUpdateCoordinator,
258  entity_description: ScreenLogicBinarySensorDescription,
259  pump_index: int,
260  ) -> None:
261  """Initialize of the entity."""
262  entity_description = dataclasses.replace(
263  entity_description, data_root=(DEVICE.PUMP, pump_index)
264  )
265  super().__init__(coordinator, entity_description)
None __init__(self, ScreenlogicDataUpdateCoordinator coordinator, ScreenLogicBinarySensorDescription entity_description, int pump_index)
None async_setup_entry(HomeAssistant hass, ScreenLogicConfigEntry config_entry, AddEntitiesCallback async_add_entities)
None cleanup_excluded_entity(ScreenlogicDataUpdateCoordinator coordinator, str platform_domain, ScreenLogicDataPath data_path)
Definition: util.py:38