Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Overkiz binary sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import cast
8 
9 from pyoverkiz.enums import OverkizCommandParam, OverkizState
10 from pyoverkiz.types import StateType as OverkizStateType
11 
13  BinarySensorDeviceClass,
14  BinarySensorEntity,
15  BinarySensorEntityDescription,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import HomeAssistantOverkizData
22 from .const import DOMAIN, IGNORED_OVERKIZ_DEVICES
23 from .entity import OverkizDescriptiveEntity
24 
25 
26 @dataclass(frozen=True, kw_only=True)
28  """Class to describe an Overkiz binary sensor."""
29 
30  value_fn: Callable[[OverkizStateType], bool]
31 
32 
33 BINARY_SENSOR_DESCRIPTIONS: list[OverkizBinarySensorDescription] = [
34  # RainSensor/RainSensor
36  key=OverkizState.CORE_RAIN,
37  name="Rain",
38  icon="mdi:weather-rainy",
39  value_fn=lambda state: state == OverkizCommandParam.DETECTED,
40  ),
41  # SmokeSensor/SmokeSensor
43  key=OverkizState.CORE_SMOKE,
44  name="Smoke",
45  device_class=BinarySensorDeviceClass.SMOKE,
46  value_fn=lambda state: state == OverkizCommandParam.DETECTED,
47  ),
48  # WaterSensor/WaterDetectionSensor
50  key=OverkizState.CORE_WATER_DETECTION,
51  name="Water",
52  icon="mdi:water",
53  value_fn=lambda state: state == OverkizCommandParam.DETECTED,
54  ),
55  # AirSensor/AirFlowSensor
57  key=OverkizState.CORE_GAS_DETECTION,
58  name="Gas",
59  device_class=BinarySensorDeviceClass.GAS,
60  value_fn=lambda state: state == OverkizCommandParam.DETECTED,
61  ),
62  # OccupancySensor/OccupancySensor
63  # OccupancySensor/MotionSensor
65  key=OverkizState.CORE_OCCUPANCY,
66  name="Occupancy",
67  device_class=BinarySensorDeviceClass.OCCUPANCY,
68  value_fn=lambda state: state == OverkizCommandParam.PERSON_INSIDE,
69  ),
70  # ContactSensor/WindowWithTiltSensor
72  key=OverkizState.CORE_VIBRATION,
73  name="Vibration",
74  device_class=BinarySensorDeviceClass.VIBRATION,
75  value_fn=lambda state: state == OverkizCommandParam.DETECTED,
76  ),
77  # ContactSensor/ContactSensor
79  key=OverkizState.CORE_CONTACT,
80  name="Contact",
81  device_class=BinarySensorDeviceClass.DOOR,
82  value_fn=lambda state: state == OverkizCommandParam.OPEN,
83  ),
84  # Siren/SirenStatus
86  key=OverkizState.CORE_ASSEMBLY,
87  name="Assembly",
88  device_class=BinarySensorDeviceClass.PROBLEM,
89  value_fn=lambda state: state == OverkizCommandParam.OPEN,
90  ),
91  # Unknown
93  key=OverkizState.IO_VIBRATION_DETECTED,
94  name="Vibration",
95  device_class=BinarySensorDeviceClass.VIBRATION,
96  value_fn=lambda state: state == OverkizCommandParam.DETECTED,
97  ),
98  # DomesticHotWaterProduction/WaterHeatingSystem
100  key=OverkizState.IO_OPERATING_MODE_CAPABILITIES,
101  name="Energy Demand Status",
102  device_class=BinarySensorDeviceClass.HEAT,
103  value_fn=lambda state: cast(dict, state).get(
104  OverkizCommandParam.ENERGY_DEMAND_STATUS
105  )
106  == 1,
107  ),
109  key=OverkizState.CORE_HEATING_STATUS,
110  name="Heating status",
111  device_class=BinarySensorDeviceClass.HEAT,
112  value_fn=lambda state: cast(str, state).lower()
113  in (OverkizCommandParam.ON, OverkizCommandParam.HEATING),
114  ),
116  key=OverkizState.MODBUSLINK_DHW_ABSENCE_MODE,
117  name="Absence mode",
118  value_fn=(
119  lambda state: state in (OverkizCommandParam.ON, OverkizCommandParam.PROG)
120  ),
121  ),
123  key=OverkizState.MODBUSLINK_DHW_BOOST_MODE,
124  name="Boost mode",
125  value_fn=(
126  lambda state: state in (OverkizCommandParam.ON, OverkizCommandParam.PROG)
127  ),
128  ),
130  key=OverkizState.MODBUSLINK_DHW_MODE,
131  name="Manual mode",
132  value_fn=(
133  lambda state: state
134  in (OverkizCommandParam.MANUAL, OverkizCommandParam.MANUAL_ECO_INACTIVE)
135  ),
136  ),
137 ]
138 
139 SUPPORTED_STATES = {
140  description.key: description for description in BINARY_SENSOR_DESCRIPTIONS
141 }
142 
143 
145  hass: HomeAssistant,
146  entry: ConfigEntry,
147  async_add_entities: AddEntitiesCallback,
148 ) -> None:
149  """Set up the Overkiz binary sensors from a config entry."""
150  data: HomeAssistantOverkizData = hass.data[DOMAIN][entry.entry_id]
151  entities: list[OverkizBinarySensor] = []
152 
153  for device in data.coordinator.data.values():
154  if (
155  device.widget in IGNORED_OVERKIZ_DEVICES
156  or device.ui_class in IGNORED_OVERKIZ_DEVICES
157  ):
158  continue
159 
160  entities.extend(
162  device.device_url,
163  data.coordinator,
164  description,
165  )
166  for state in device.definition.states
167  if (description := SUPPORTED_STATES.get(state.qualified_name))
168  )
169 
170  async_add_entities(entities)
171 
172 
174  """Representation of an Overkiz Binary Sensor."""
175 
176  entity_description: OverkizBinarySensorDescription
177 
178  @property
179  def is_on(self) -> bool | None:
180  """Return the state of the sensor."""
181  if state := self.devicedevice.states.get(self.entity_descriptionentity_description.key):
182  return self.entity_descriptionentity_description.value_fn(state.value)
183 
184  return None
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)