Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Platform for eq3 binary sensor entities."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 from typing import TYPE_CHECKING
6 
7 from eq3btsmart.models import Status
8 
10  BinarySensorDeviceClass,
11  BinarySensorEntity,
12  BinarySensorEntityDescription,
13 )
14 from homeassistant.const import EntityCategory
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import Eq3ConfigEntry
19 from .const import ENTITY_KEY_BATTERY, ENTITY_KEY_DST, ENTITY_KEY_WINDOW
20 from .entity import Eq3Entity
21 
22 
23 @dataclass(frozen=True, kw_only=True)
25  """Entity description for eq3 binary sensors."""
26 
27  value_func: Callable[[Status], bool]
28 
29 
30 BINARY_SENSOR_ENTITY_DESCRIPTIONS = [
32  value_func=lambda status: status.is_low_battery,
33  key=ENTITY_KEY_BATTERY,
34  device_class=BinarySensorDeviceClass.BATTERY,
35  entity_category=EntityCategory.DIAGNOSTIC,
36  ),
38  value_func=lambda status: status.is_window_open,
39  key=ENTITY_KEY_WINDOW,
40  device_class=BinarySensorDeviceClass.WINDOW,
41  ),
43  value_func=lambda status: status.is_dst,
44  key=ENTITY_KEY_DST,
45  translation_key=ENTITY_KEY_DST,
46  entity_category=EntityCategory.DIAGNOSTIC,
47  ),
48 ]
49 
50 
52  hass: HomeAssistant,
53  entry: Eq3ConfigEntry,
54  async_add_entities: AddEntitiesCallback,
55 ) -> None:
56  """Set up the entry."""
57 
59  Eq3BinarySensorEntity(entry, entity_description)
60  for entity_description in BINARY_SENSOR_ENTITY_DESCRIPTIONS
61  )
62 
63 
65  """Base class for eQ-3 binary sensor entities."""
66 
67  entity_description: Eq3BinarySensorEntityDescription
68 
69  def __init__(
70  self,
71  entry: Eq3ConfigEntry,
72  entity_description: Eq3BinarySensorEntityDescription,
73  ) -> None:
74  """Initialize the entity."""
75 
76  super().__init__(entry, entity_description.key)
77  self.entity_descriptionentity_description = entity_description
78 
79  @property
80  def is_on(self) -> bool:
81  """Return the state of the binary sensor."""
82 
83  if TYPE_CHECKING:
84  assert self._thermostat_thermostat.status is not None
85 
86  return self.entity_descriptionentity_description.value_func(self._thermostat_thermostat.status)
None __init__(self, Eq3ConfigEntry entry, Eq3BinarySensorEntityDescription entity_description)
None async_setup_entry(HomeAssistant hass, Eq3ConfigEntry entry, AddEntitiesCallback async_add_entities)