Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for MAX! binary sensors via MAX! Cube."""
2 
3 from __future__ import annotations
4 
6  BinarySensorDeviceClass,
7  BinarySensorEntity,
8 )
9 from homeassistant.const import EntityCategory
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
13 
14 from . import DATA_KEY
15 
16 
18  hass: HomeAssistant,
19  config: ConfigType,
20  add_entities: AddEntitiesCallback,
21  discovery_info: DiscoveryInfoType | None = None,
22 ) -> None:
23  """Iterate through all MAX! Devices and add window shutters."""
24  devices: list[MaxCubeBinarySensorBase] = []
25  for handler in hass.data[DATA_KEY].values():
26  for device in handler.cube.devices:
27  devices.append(MaxCubeBattery(handler, device))
28  # Only add Window Shutters
29  if device.is_windowshutter():
30  devices.append(MaxCubeShutter(handler, device))
31 
32  add_entities(devices)
33 
34 
36  """Base class for maxcube binary sensors."""
37 
38  _attr_entity_category = EntityCategory.DIAGNOSTIC
39 
40  def __init__(self, handler, device):
41  """Initialize MAX! Cube BinarySensorEntity."""
42  self._cubehandle_cubehandle = handler
43  self._device_device = device
44  self._room_room = handler.cube.room_by_id(device.room_id)
45 
46  def update(self) -> None:
47  """Get latest data from MAX! Cube."""
48  self._cubehandle_cubehandle.update()
49 
50 
52  """Representation of a MAX! Cube Binary Sensor device."""
53 
54  _attr_device_class = BinarySensorDeviceClass.WINDOW
55 
56  def __init__(self, handler, device):
57  """Initialize MAX! Cube BinarySensorEntity."""
58  super().__init__(handler, device)
59 
60  self._attr_name_attr_name = f"{self._room.name} {self._device.name}"
61  self._attr_unique_id_attr_unique_id = self._device_device.serial
62 
63  @property
64  def is_on(self):
65  """Return true if the binary sensor is on/open."""
66  return self._device_device.is_open
67 
68 
70  """Representation of a MAX! Cube Binary Sensor device."""
71 
72  _attr_device_class = BinarySensorDeviceClass.BATTERY
73 
74  def __init__(self, handler, device):
75  """Initialize MAX! Cube BinarySensorEntity."""
76  super().__init__(handler, device)
77 
78  self._attr_name_attr_name = f"{self._room.name} {device.name} battery"
79  self._attr_unique_id_attr_unique_id = f"{self._device.serial}_battery"
80 
81  @property
82  def is_on(self):
83  """Return true if the binary sensor is on/open."""
84  return self._device_device.battery == 1
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)