Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Doorsensor Support for the Nuki Lock."""
2 
3 from __future__ import annotations
4 
5 from pynuki.constants import STATE_DOORSENSOR_OPENED
6 from pynuki.device import NukiDevice
7 
9  BinarySensorDeviceClass,
10  BinarySensorEntity,
11 )
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import EntityCategory
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import NukiEntryData
18 from .const import DOMAIN as NUKI_DOMAIN
19 from .entity import NukiEntity
20 
21 
23  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
24 ) -> None:
25  """Set up the Nuki binary sensors."""
26  entry_data: NukiEntryData = hass.data[NUKI_DOMAIN][entry.entry_id]
27 
28  entities: list[NukiEntity] = []
29 
30  for lock in entry_data.locks:
31  if lock.is_door_sensor_activated:
32  entities.append(NukiDoorsensorEntity(entry_data.coordinator, lock))
33  entities.append(NukiBatteryCriticalEntity(entry_data.coordinator, lock))
34  entities.append(NukiBatteryChargingEntity(entry_data.coordinator, lock))
35 
36  for opener in entry_data.openers:
37  entities.append(NukiRingactionEntity(entry_data.coordinator, opener))
38  entities.append(NukiBatteryCriticalEntity(entry_data.coordinator, opener))
39 
40  async_add_entities(entities)
41 
42 
43 class NukiDoorsensorEntity(NukiEntity[NukiDevice], BinarySensorEntity):
44  """Representation of a Nuki Lock Doorsensor."""
45 
46  _attr_has_entity_name = True
47  _attr_name = None
48  _attr_device_class = BinarySensorDeviceClass.DOOR
49 
50  @property
51  def unique_id(self) -> str:
52  """Return a unique ID."""
53  return f"{self._nuki_device.nuki_id}_doorsensor"
54 
55  @property
56  def available(self) -> bool:
57  """Return true if door sensor is present and activated."""
58  return super().available and self._nuki_device.is_door_sensor_activated
59 
60  @property
61  def door_sensor_state(self):
62  """Return the state of the door sensor."""
63  return self._nuki_device.door_sensor_state
64 
65  @property
67  """Return the state name of the door sensor."""
68  return self._nuki_device.door_sensor_state_name
69 
70  @property
71  def is_on(self):
72  """Return true if the door is open."""
73  return self.door_sensor_statedoor_sensor_statedoor_sensor_state == STATE_DOORSENSOR_OPENED
74 
75 
76 class NukiRingactionEntity(NukiEntity[NukiDevice], BinarySensorEntity):
77  """Representation of a Nuki Opener Ringaction."""
78 
79  _attr_has_entity_name = True
80  _attr_translation_key = "ring_action"
81 
82  @property
83  def unique_id(self) -> str:
84  """Return a unique ID."""
85  return f"{self._nuki_device.nuki_id}_ringaction"
86 
87  @property
88  def is_on(self) -> bool:
89  """Return the value of the ring action state."""
90  return self._nuki_device.ring_action_state
91 
92 
93 class NukiBatteryCriticalEntity(NukiEntity[NukiDevice], BinarySensorEntity):
94  """Representation of Nuki Battery Critical."""
95 
96  _attr_has_entity_name = True
97  _attr_device_class = BinarySensorDeviceClass.BATTERY
98  _attr_entity_category = EntityCategory.DIAGNOSTIC
99 
100  @property
101  def unique_id(self) -> str:
102  """Return a unique ID."""
103  return f"{self._nuki_device.nuki_id}_battery_critical"
104 
105  @property
106  def is_on(self) -> bool:
107  """Return the value of the battery critical."""
108  return self._nuki_device.battery_critical
109 
110 
111 class NukiBatteryChargingEntity(NukiEntity[NukiDevice], BinarySensorEntity):
112  """Representation of a Nuki Battery charging."""
113 
114  _attr_has_entity_name = True
115  _attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING
116  _attr_entity_category = EntityCategory.DIAGNOSTIC
117  _attr_entity_registry_enabled_default = False
118 
119  @property
120  def unique_id(self) -> str:
121  """Return a unique ID."""
122  return f"{self._nuki_device.nuki_id}_battery_charging"
123 
124  @property
125  def is_on(self) -> bool:
126  """Return the value of the battery charging."""
127  return self._nuki_device.battery_charging
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)