Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Elmax sensor platform."""
2 
3 from __future__ import annotations
4 
5 from elmax_api.model.panel import PanelStatus
6 
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10 )
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .const import DOMAIN
16 from .coordinator import ElmaxCoordinator
17 from .entity import ElmaxEntity
18 
19 
21  hass: HomeAssistant,
22  config_entry: ConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Set up the Elmax sensor platform."""
26  coordinator: ElmaxCoordinator = hass.data[DOMAIN][config_entry.entry_id]
27  known_devices = set()
28 
29  def _discover_new_devices():
30  panel_status: PanelStatus = coordinator.data
31  # In case the panel is offline, its status will be None. In that case, simply do nothing
32  if panel_status is None:
33  return
34 
35  # Otherwise, add all the entities we found
36  entities = []
37  for zone in panel_status.zones:
38  # Skip already handled devices
39  if zone.endpoint_id in known_devices:
40  continue
41  entity = ElmaxSensor(
42  elmax_device=zone,
43  panel_version=panel_status.release,
44  coordinator=coordinator,
45  )
46  entities.append(entity)
47 
48  if entities:
49  async_add_entities(entities)
50  known_devices.update([e.unique_id for e in entities])
51 
52  # Register a listener for the discovery of new devices
53  remove_handle = coordinator.async_add_listener(_discover_new_devices)
54  config_entry.async_on_unload(remove_handle)
55 
56  # Immediately run a discovery, so we don't need to wait for the next update
57  _discover_new_devices()
58 
59 
61  """Elmax Sensor entity implementation."""
62 
63  _attr_device_class = BinarySensorDeviceClass.DOOR
64 
65  @property
66  def is_on(self) -> bool:
67  """Return true if the binary sensor is on."""
68  return self.coordinator.get_zone_state(self._device_device.endpoint_id).opened
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)