Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for DROP binary sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 import logging
8 
10  BinarySensorDeviceClass,
11  BinarySensorEntity,
12  BinarySensorEntityDescription,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from .const import (
19  CONF_DEVICE_TYPE,
20  DEV_ALERT,
21  DEV_HUB,
22  DEV_LEAK_DETECTOR,
23  DEV_PROTECTION_VALVE,
24  DEV_PUMP_CONTROLLER,
25  DEV_RO_FILTER,
26  DEV_SALT_SENSOR,
27  DEV_SOFTENER,
28  DOMAIN,
29 )
30 from .coordinator import DROPDeviceDataUpdateCoordinator
31 from .entity import DROPEntity
32 
33 _LOGGER = logging.getLogger(__name__)
34 
35 
36 # Binary sensor type constants
37 ALERT_SENSOR = "alert_sensor"
38 LEAK_DETECTED = "leak"
39 PENDING_NOTIFICATION = "pending_notification"
40 POWER = "power"
41 PUMP_STATUS = "pump"
42 RESERVE_IN_USE = "reserve_in_use"
43 SALT_LOW = "salt"
44 
45 
46 @dataclass(kw_only=True, frozen=True)
48  """Describes DROP binary sensor entity."""
49 
50  value_fn: Callable[[DROPDeviceDataUpdateCoordinator], int | None]
51 
52 
53 BINARY_SENSORS: list[DROPBinarySensorEntityDescription] = [
55  key=LEAK_DETECTED,
56  translation_key=LEAK_DETECTED,
57  device_class=BinarySensorDeviceClass.MOISTURE,
58  value_fn=lambda device: device.drop_api.leak_detected(),
59  ),
61  key=PENDING_NOTIFICATION,
62  translation_key=PENDING_NOTIFICATION,
63  value_fn=lambda device: device.drop_api.notification_pending(),
64  ),
66  key=SALT_LOW,
67  translation_key=SALT_LOW,
68  value_fn=lambda device: device.drop_api.salt_low(),
69  ),
71  key=RESERVE_IN_USE,
72  translation_key=RESERVE_IN_USE,
73  value_fn=lambda device: device.drop_api.reserve_in_use(),
74  ),
76  key=PUMP_STATUS,
77  translation_key=PUMP_STATUS,
78  value_fn=lambda device: device.drop_api.pump_status(),
79  ),
81  key=ALERT_SENSOR,
82  translation_key=ALERT_SENSOR,
83  device_class=BinarySensorDeviceClass.PROBLEM,
84  value_fn=lambda device: device.drop_api.sensor_high(),
85  ),
87  key=POWER,
88  translation_key=None, # Use name provided by binary sensor device class
89  device_class=BinarySensorDeviceClass.POWER,
90  value_fn=lambda device: device.drop_api.power(),
91  ),
92 ]
93 
94 # Defines which binary sensors are used by each device type
95 DEVICE_BINARY_SENSORS: dict[str, list[str]] = {
96  DEV_ALERT: [ALERT_SENSOR, POWER],
97  DEV_HUB: [LEAK_DETECTED, PENDING_NOTIFICATION],
98  DEV_LEAK_DETECTOR: [LEAK_DETECTED],
99  DEV_PROTECTION_VALVE: [LEAK_DETECTED],
100  DEV_PUMP_CONTROLLER: [LEAK_DETECTED, PUMP_STATUS],
101  DEV_RO_FILTER: [LEAK_DETECTED],
102  DEV_SALT_SENSOR: [SALT_LOW],
103  DEV_SOFTENER: [RESERVE_IN_USE],
104 }
105 
106 
108  hass: HomeAssistant,
109  config_entry: ConfigEntry,
110  async_add_entities: AddEntitiesCallback,
111 ) -> None:
112  """Set up the DROP binary sensors from config entry."""
113  _LOGGER.debug(
114  "Set up binary sensor for device type %s with entry_id is %s",
115  config_entry.data[CONF_DEVICE_TYPE],
116  config_entry.entry_id,
117  )
118 
119  if config_entry.data[CONF_DEVICE_TYPE] in DEVICE_BINARY_SENSORS:
121  DROPBinarySensor(hass.data[DOMAIN][config_entry.entry_id], sensor)
122  for sensor in BINARY_SENSORS
123  if sensor.key in DEVICE_BINARY_SENSORS[config_entry.data[CONF_DEVICE_TYPE]]
124  )
125 
126 
128  """Representation of a DROP binary sensor."""
129 
130  entity_description: DROPBinarySensorEntityDescription
131 
132  def __init__(
133  self,
134  coordinator: DROPDeviceDataUpdateCoordinator,
135  entity_description: DROPBinarySensorEntityDescription,
136  ) -> None:
137  """Initialize the binary sensor."""
138  super().__init__(entity_description.key, coordinator)
139  self.entity_descriptionentity_description = entity_description
140 
141  @property
142  def is_on(self) -> bool:
143  """Return the state of the binary sensor."""
144  return self.entity_descriptionentity_description.value_fn(self.coordinator) == 1
None __init__(self, DROPDeviceDataUpdateCoordinator coordinator, DROPBinarySensorEntityDescription entity_description)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)