Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary sensors on Zigbee Home Automation networks."""
2 
3 from __future__ import annotations
4 
5 import functools
6 
8  BinarySensorDeviceClass,
9  BinarySensorEntity,
10 )
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import Platform
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.dispatcher import async_dispatcher_connect
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from .entity import ZHAEntity
18 from .helpers import (
19  SIGNAL_ADD_ENTITIES,
20  EntityData,
21  async_add_entities as zha_async_add_entities,
22  get_zha_data,
23 )
24 
25 
27  hass: HomeAssistant,
28  config_entry: ConfigEntry,
29  async_add_entities: AddEntitiesCallback,
30 ) -> None:
31  """Set up the Zigbee Home Automation binary sensor from config entry."""
32  zha_data = get_zha_data(hass)
33  entities_to_create = zha_data.platforms[Platform.BINARY_SENSOR]
34 
36  hass,
37  SIGNAL_ADD_ENTITIES,
38  functools.partial(
39  zha_async_add_entities, async_add_entities, BinarySensor, entities_to_create
40  ),
41  )
42  config_entry.async_on_unload(unsub)
43 
44 
46  """ZHA BinarySensor."""
47 
48  def __init__(self, entity_data: EntityData) -> None:
49  """Initialize the ZHA binary sensor."""
50  super().__init__(entity_data)
51  if self.entity_data.entity.info_object.device_class is not None:
52  self._attr_device_class_attr_device_class = BinarySensorDeviceClass(
53  self.entity_data.entity.info_object.device_class
54  )
55 
56  @property
57  def is_on(self) -> bool:
58  """Return True if the switch is on based on the state machine."""
59  return self.entity_data.entity.is_on
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
HAZHAData get_zha_data(HomeAssistant hass)
Definition: helpers.py:1020
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103