Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Tedee sensor entities."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 
6 from aiotedee import TedeeLock
7 from aiotedee.lock import TedeeLockState
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 .coordinator import TedeeConfigEntry
19 from .entity import TedeeDescriptionEntity
20 
21 
22 @dataclass(frozen=True, kw_only=True)
24  BinarySensorEntityDescription,
25 ):
26  """Describes Tedee binary sensor entity."""
27 
28  is_on_fn: Callable[[TedeeLock], bool | None]
29 
30 
31 ENTITIES: tuple[TedeeBinarySensorEntityDescription, ...] = (
33  key="charging",
34  device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
35  is_on_fn=lambda lock: lock.is_charging,
36  entity_category=EntityCategory.DIAGNOSTIC,
37  ),
39  key="semi_locked",
40  translation_key="semi_locked",
41  is_on_fn=lambda lock: lock.state == TedeeLockState.HALF_OPEN,
42  entity_category=EntityCategory.DIAGNOSTIC,
43  ),
45  key="pullspring_enabled",
46  translation_key="pullspring_enabled",
47  is_on_fn=lambda lock: lock.is_enabled_pullspring,
48  entity_category=EntityCategory.DIAGNOSTIC,
49  ),
51  key="uncalibrated",
52  translation_key="uncalibrated",
53  is_on_fn=lambda lock: lock.state == TedeeLockState.UNCALIBRATED,
54  device_class=BinarySensorDeviceClass.PROBLEM,
55  entity_category=EntityCategory.DIAGNOSTIC,
56  entity_registry_enabled_default=False,
57  ),
58 )
59 
60 
62  hass: HomeAssistant,
63  entry: TedeeConfigEntry,
64  async_add_entities: AddEntitiesCallback,
65 ) -> None:
66  """Set up the Tedee sensor entity."""
67  coordinator = entry.runtime_data
68 
70  TedeeBinarySensorEntity(lock, coordinator, entity_description)
71  for lock in coordinator.data.values()
72  for entity_description in ENTITIES
73  )
74 
75  def _async_add_new_lock(lock_id: int) -> None:
76  lock = coordinator.data[lock_id]
78  TedeeBinarySensorEntity(lock, coordinator, entity_description)
79  for entity_description in ENTITIES
80  )
81 
82  coordinator.new_lock_callbacks.append(_async_add_new_lock)
83 
84 
86  """Tedee sensor entity."""
87 
88  entity_description: TedeeBinarySensorEntityDescription
89 
90  @property
91  def is_on(self) -> bool | None:
92  """Return true if the binary sensor is on."""
93  return self.entity_descriptionentity_description.is_on_fn(self._lock_lock)
None async_setup_entry(HomeAssistant hass, TedeeConfigEntry entry, AddEntitiesCallback async_add_entities)