Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Flume binary sensors."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10  BinarySensorEntityDescription,
11 )
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import EntityCategory
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from .const import (
18  DOMAIN,
19  FLUME_DEVICES,
20  FLUME_NOTIFICATIONS_COORDINATOR,
21  FLUME_TYPE_BRIDGE,
22  FLUME_TYPE_SENSOR,
23  KEY_DEVICE_ID,
24  KEY_DEVICE_LOCATION,
25  KEY_DEVICE_LOCATION_NAME,
26  KEY_DEVICE_TYPE,
27  NOTIFICATION_HIGH_FLOW,
28  NOTIFICATION_LEAK_DETECTED,
29  NOTIFICATION_LOW_BATTERY,
30 )
31 from .coordinator import (
32  FlumeDeviceConnectionUpdateCoordinator,
33  FlumeNotificationDataUpdateCoordinator,
34 )
35 from .entity import FlumeEntity
36 from .util import get_valid_flume_devices
37 
38 BINARY_SENSOR_DESCRIPTION_CONNECTED = BinarySensorEntityDescription(
39  key="connected", device_class=BinarySensorDeviceClass.CONNECTIVITY
40 )
41 
42 
43 @dataclass(frozen=True, kw_only=True)
45  """Describes a binary sensor entity."""
46 
47  event_rule: str
48 
49 
50 FLUME_BINARY_NOTIFICATION_SENSORS: tuple[FlumeBinarySensorEntityDescription, ...] = (
52  key="leak",
53  translation_key="leak",
54  entity_category=EntityCategory.DIAGNOSTIC,
55  event_rule=NOTIFICATION_LEAK_DETECTED,
56  ),
58  key="flow",
59  translation_key="flow",
60  entity_category=EntityCategory.DIAGNOSTIC,
61  event_rule=NOTIFICATION_HIGH_FLOW,
62  ),
64  key="low_battery",
65  entity_category=EntityCategory.DIAGNOSTIC,
66  device_class=BinarySensorDeviceClass.BATTERY,
67  event_rule=NOTIFICATION_LOW_BATTERY,
68  ),
69 )
70 
71 
73  hass: HomeAssistant,
74  config_entry: ConfigEntry,
75  async_add_entities: AddEntitiesCallback,
76 ) -> None:
77  """Set up a Flume binary sensor.."""
78  flume_domain_data = hass.data[DOMAIN][config_entry.entry_id]
79  flume_devices = flume_domain_data[FLUME_DEVICES]
80 
81  flume_entity_list: list[
82  FlumeNotificationBinarySensor | FlumeConnectionBinarySensor
83  ] = []
84 
85  connection_coordinator = FlumeDeviceConnectionUpdateCoordinator(
86  hass=hass, flume_devices=flume_devices
87  )
88  notification_coordinator = flume_domain_data[FLUME_NOTIFICATIONS_COORDINATOR]
89  flume_devices = get_valid_flume_devices(flume_devices)
90  for device in flume_devices:
91  device_id = device[KEY_DEVICE_ID]
92  device_location_name = device[KEY_DEVICE_LOCATION][KEY_DEVICE_LOCATION_NAME]
93 
94  connection_sensor = FlumeConnectionBinarySensor(
95  coordinator=connection_coordinator,
96  description=BINARY_SENSOR_DESCRIPTION_CONNECTED,
97  device_id=device_id,
98  location_name=device_location_name,
99  is_bridge=(device[KEY_DEVICE_TYPE] is FLUME_TYPE_BRIDGE),
100  )
101 
102  flume_entity_list.append(connection_sensor)
103 
104  if device[KEY_DEVICE_TYPE] != FLUME_TYPE_SENSOR:
105  continue
106 
107  # Build notification sensors
108  flume_entity_list.extend(
109  [
111  coordinator=notification_coordinator,
112  description=description,
113  device_id=device_id,
114  location_name=device_location_name,
115  )
116  for description in FLUME_BINARY_NOTIFICATION_SENSORS
117  ]
118  )
119 
120  async_add_entities(flume_entity_list)
121 
122 
124  FlumeEntity[FlumeNotificationDataUpdateCoordinator], BinarySensorEntity
125 ):
126  """Binary sensor class."""
127 
128  entity_description: FlumeBinarySensorEntityDescription
129 
130  @property
131  def is_on(self) -> bool:
132  """Return on state."""
133  return bool(
134  (
135  notifications := self.coordinator.active_notifications_by_device.get(
136  self.device_id
137  )
138  )
139  and self.entity_description.event_rule in notifications
140  )
141 
142 
144  FlumeEntity[FlumeDeviceConnectionUpdateCoordinator], BinarySensorEntity
145 ):
146  """Binary Sensor class for WIFI Connection status."""
147 
148  entity_description: FlumeBinarySensorEntityDescription
149  _attr_entity_category = EntityCategory.DIAGNOSTIC
150  _attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
151 
152  @property
153  def is_on(self) -> bool:
154  """Return connection status."""
155  return bool(
156  (connected := self.coordinator.connected) and connected[self.device_id]
157  )
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
list[dict[str, Any]] get_valid_flume_devices(FlumeDeviceList flume_devices)
Definition: util.py:12