Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Notion binary sensors."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Literal
7 
8 from aionotion.listener.models import ListenerKind
9 
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13  BinarySensorEntityDescription,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import EntityCategory
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .const import (
21  DOMAIN,
22  LOGGER,
23  SENSOR_BATTERY,
24  SENSOR_DOOR,
25  SENSOR_GARAGE_DOOR,
26  SENSOR_LEAK,
27  SENSOR_MISSING,
28  SENSOR_SAFE,
29  SENSOR_SLIDING,
30  SENSOR_SMOKE_CO,
31  SENSOR_WINDOW_HINGED,
32 )
33 from .coordinator import NotionDataUpdateCoordinator
34 from .entity import NotionEntity, NotionEntityDescription
35 
36 
37 @dataclass(frozen=True, kw_only=True)
39  BinarySensorEntityDescription, NotionEntityDescription
40 ):
41  """Describe a Notion binary sensor."""
42 
43  on_state: Literal["alarm", "leak", "low", "not_missing", "open"]
44 
45 
46 BINARY_SENSOR_DESCRIPTIONS = (
48  key=SENSOR_BATTERY,
49  device_class=BinarySensorDeviceClass.BATTERY,
50  entity_category=EntityCategory.DIAGNOSTIC,
51  listener_kind=ListenerKind.BATTERY,
52  on_state="low",
53  ),
55  key=SENSOR_DOOR,
56  device_class=BinarySensorDeviceClass.DOOR,
57  listener_kind=ListenerKind.DOOR,
58  on_state="open",
59  ),
61  key=SENSOR_GARAGE_DOOR,
62  device_class=BinarySensorDeviceClass.GARAGE_DOOR,
63  listener_kind=ListenerKind.GARAGE_DOOR,
64  on_state="open",
65  ),
67  key=SENSOR_LEAK,
68  device_class=BinarySensorDeviceClass.MOISTURE,
69  listener_kind=ListenerKind.LEAK_STATUS,
70  on_state="leak",
71  ),
73  key=SENSOR_MISSING,
74  device_class=BinarySensorDeviceClass.CONNECTIVITY,
75  entity_category=EntityCategory.DIAGNOSTIC,
76  listener_kind=ListenerKind.CONNECTED,
77  on_state="not_missing",
78  ),
80  key=SENSOR_SAFE,
81  translation_key="safe",
82  device_class=BinarySensorDeviceClass.DOOR,
83  listener_kind=ListenerKind.SAFE,
84  on_state="open",
85  ),
87  key=SENSOR_SLIDING,
88  translation_key="sliding_door_window",
89  device_class=BinarySensorDeviceClass.DOOR,
90  listener_kind=ListenerKind.SLIDING_DOOR_OR_WINDOW,
91  on_state="open",
92  ),
94  key=SENSOR_SMOKE_CO,
95  translation_key="smoke_carbon_monoxide_detector",
96  device_class=BinarySensorDeviceClass.SMOKE,
97  listener_kind=ListenerKind.SMOKE,
98  on_state="alarm",
99  ),
101  key=SENSOR_WINDOW_HINGED,
102  translation_key="hinged_window",
103  listener_kind=ListenerKind.HINGED_WINDOW,
104  on_state="open",
105  ),
106 )
107 
108 
110  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
111 ) -> None:
112  """Set up Notion sensors based on a config entry."""
113  coordinator: NotionDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
114 
116  [
118  coordinator,
119  listener_id,
120  sensor.uuid,
121  sensor.bridge.id,
122  description,
123  )
124  for listener_id, listener in coordinator.data.listeners.items()
125  for description in BINARY_SENSOR_DESCRIPTIONS
126  if description.listener_kind.value == listener.definition_id
127  and (sensor := coordinator.data.sensors[listener.sensor_id])
128  ]
129  )
130 
131 
133  """Define a Notion sensor."""
134 
135  entity_description: NotionBinarySensorDescription
136 
137  @property
138  def is_on(self) -> bool | None:
139  """Return true if the binary sensor is on."""
140  if not self.listenerlistener.insights.primary.value:
141  LOGGER.warning("Unknown listener structure: %s", self.listenerlistener)
142  return False
143  return self.listenerlistener.insights.primary.value == self.entity_descriptionentity_description.on_state
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)