Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Fibaro binary sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any, cast
7 
8 from pyfibaro.fibaro_device import DeviceModel
9 
11  ENTITY_ID_FORMAT,
12  BinarySensorDeviceClass,
13  BinarySensorEntity,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import Platform
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import FibaroController
21 from .const import DOMAIN
22 from .entity import FibaroEntity
23 
24 SENSOR_TYPES = {
25  "com.fibaro.floodSensor": ["Flood", "mdi:water", BinarySensorDeviceClass.MOISTURE],
26  "com.fibaro.motionSensor": ["Motion", "mdi:run", BinarySensorDeviceClass.MOTION],
27  "com.fibaro.doorSensor": ["Door", "mdi:window-open", BinarySensorDeviceClass.DOOR],
28  "com.fibaro.windowSensor": [
29  "Window",
30  "mdi:window-open",
31  BinarySensorDeviceClass.WINDOW,
32  ],
33  "com.fibaro.smokeSensor": ["Smoke", "mdi:smoking", BinarySensorDeviceClass.SMOKE],
34  "com.fibaro.FGMS001": ["Motion", "mdi:run", BinarySensorDeviceClass.MOTION],
35  "com.fibaro.heatDetector": ["Heat", "mdi:fire", BinarySensorDeviceClass.HEAT],
36  "com.fibaro.accelerometer": [
37  "Moving",
38  "mdi:axis-arrow",
39  BinarySensorDeviceClass.MOVING,
40  ],
41 }
42 
43 
45  hass: HomeAssistant,
46  entry: ConfigEntry,
47  async_add_entities: AddEntitiesCallback,
48 ) -> None:
49  """Perform the setup for Fibaro controller devices."""
50  controller: FibaroController = hass.data[DOMAIN][entry.entry_id]
52  [
53  FibaroBinarySensor(device)
54  for device in controller.fibaro_devices[Platform.BINARY_SENSOR]
55  ],
56  True,
57  )
58 
59 
61  """Representation of a Fibaro Binary Sensor."""
62 
63  def __init__(self, fibaro_device: DeviceModel) -> None:
64  """Initialize the binary_sensor."""
65  super().__init__(fibaro_device)
66  self.entity_identity_identity_id = ENTITY_ID_FORMAT.format(self.ha_idha_id)
67  self._own_extra_state_attributes_own_extra_state_attributes: Mapping[str, Any] = {}
68  self._fibaro_sensor_type_fibaro_sensor_type = None
69  if fibaro_device.type in SENSOR_TYPES:
70  self._fibaro_sensor_type_fibaro_sensor_type = fibaro_device.type
71  elif fibaro_device.base_type in SENSOR_TYPES:
72  self._fibaro_sensor_type_fibaro_sensor_type = fibaro_device.base_type
73  if self._fibaro_sensor_type_fibaro_sensor_type:
74  self._attr_device_class_attr_device_class = cast(
75  BinarySensorDeviceClass, SENSOR_TYPES[self._fibaro_sensor_type_fibaro_sensor_type][2]
76  )
77  self._attr_icon_attr_icon = SENSOR_TYPES[self._fibaro_sensor_type_fibaro_sensor_type][1]
78 
79  @property
80  def extra_state_attributes(self) -> Mapping[str, Any]:
81  """Return the extra state attributes of the device."""
82  return {**super().extra_state_attributes, **self._own_extra_state_attributes_own_extra_state_attributes}
83 
84  def update(self) -> None:
85  """Get the latest data and update the state."""
86  super().update()
87  if self._fibaro_sensor_type_fibaro_sensor_type == "com.fibaro.accelerometer":
88  # Accelerator sensors have values for the three axis x, y and z
89  moving_values = self._get_moving_values_get_moving_values()
90  self._attr_is_on_attr_is_on = self._is_moving_is_moving(moving_values)
91  self._own_extra_state_attributes_own_extra_state_attributes = self._get_xyz_moving_get_xyz_moving(moving_values)
92  else:
93  self._attr_is_on_attr_is_on = self.current_binary_statecurrent_binary_state
94 
95  def _get_xyz_moving(self, moving_values: Mapping[str, Any]) -> Mapping[str, Any]:
96  """Return x y z values of the accelerator sensor value."""
97  attrs = {}
98  for axis_name in ("x", "y", "z"):
99  attrs[axis_name] = float(moving_values[axis_name])
100  return attrs
101 
102  def _is_moving(self, moving_values: Mapping[str, Any]) -> bool:
103  """Return that a moving is detected when one axis reports a value."""
104  for axis_name in ("x", "y", "z"):
105  if float(moving_values[axis_name]) != 0:
106  return True
107  return False
108 
109  def _get_moving_values(self) -> Mapping[str, Any]:
110  """Get the moving values of the accelerator sensor in a dict."""
111  return self.fibaro_devicefibaro_device.value.dict_value()
Mapping[str, Any] _get_xyz_moving(self, Mapping[str, Any] moving_values)
bool _is_moving(self, Mapping[str, Any] moving_values)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)