Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Tedee lock entities."""
2 
3 from typing import Any
4 
5 from aiotedee import TedeeClientException, TedeeLock, TedeeLockState
6 
7 from homeassistant.components.lock import LockEntity, LockEntityFeature
8 from homeassistant.core import HomeAssistant
9 from homeassistant.exceptions import HomeAssistantError
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from .const import DOMAIN
13 from .coordinator import TedeeApiCoordinator, TedeeConfigEntry
14 from .entity import TedeeEntity
15 
16 PARALLEL_UPDATES = 1
17 
18 
20  hass: HomeAssistant,
21  entry: TedeeConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up the Tedee lock entity."""
25  coordinator = entry.runtime_data
26 
27  entities: list[TedeeLockEntity] = []
28  for lock in coordinator.data.values():
29  if lock.is_enabled_pullspring:
30  entities.append(TedeeLockWithLatchEntity(lock, coordinator))
31  else:
32  entities.append(TedeeLockEntity(lock, coordinator))
33 
34  def _async_add_new_lock(lock_id: int) -> None:
35  lock = coordinator.data[lock_id]
36  if lock.is_enabled_pullspring:
37  async_add_entities([TedeeLockWithLatchEntity(lock, coordinator)])
38  else:
39  async_add_entities([TedeeLockEntity(lock, coordinator)])
40 
41  coordinator.new_lock_callbacks.append(_async_add_new_lock)
42 
43  async_add_entities(entities)
44 
45 
47  """A tedee lock that doesn't have pullspring enabled."""
48 
49  _attr_name = None
50 
51  def __init__(
52  self,
53  lock: TedeeLock,
54  coordinator: TedeeApiCoordinator,
55  ) -> None:
56  """Initialize the lock."""
57  super().__init__(lock, coordinator, "lock")
58 
59  @property
60  def is_locked(self) -> bool | None:
61  """Return true if lock is locked."""
62  if self._lock_lock.state in (
63  TedeeLockState.HALF_OPEN,
64  TedeeLockState.UNKNOWN,
65  ):
66  return None
67  return self._lock_lock.state == TedeeLockState.LOCKED
68 
69  @property
70  def is_unlocking(self) -> bool:
71  """Return true if lock is unlocking."""
72  return self._lock_lock.state == TedeeLockState.UNLOCKING
73 
74  @property
75  def is_open(self) -> bool:
76  """Return true if lock is open."""
77  return self._lock_lock.state == TedeeLockState.PULLED
78 
79  @property
80  def is_opening(self) -> bool:
81  """Return true if lock is opening."""
82  return self._lock_lock.state == TedeeLockState.PULLING
83 
84  @property
85  def is_locking(self) -> bool:
86  """Return true if lock is locking."""
87  return self._lock_lock.state == TedeeLockState.LOCKING
88 
89  @property
90  def is_jammed(self) -> bool:
91  """Return true if lock is jammed."""
92  return self._lock_lock.is_state_jammed
93 
94  @property
95  def available(self) -> bool:
96  """Return True if entity is available."""
97  return (
98  super().available
99  and self._lock_lock.is_connected
100  and self._lock_lock.state != TedeeLockState.UNCALIBRATED
101  )
102 
103  async def async_unlock(self, **kwargs: Any) -> None:
104  """Unlock the door."""
105  try:
106  self._lock_lock.state = TedeeLockState.UNLOCKING
107  self.async_write_ha_stateasync_write_ha_state()
108 
109  await self.coordinator.tedee_client.unlock(self._lock_lock.lock_id)
110  await self.coordinator.async_request_refresh()
111  except (TedeeClientException, Exception) as ex:
112  raise HomeAssistantError(
113  translation_domain=DOMAIN,
114  translation_key="unlock_failed",
115  translation_placeholders={"lock_id": str(self._lock_lock.lock_id)},
116  ) from ex
117 
118  async def async_lock(self, **kwargs: Any) -> None:
119  """Lock the door."""
120  try:
121  self._lock_lock.state = TedeeLockState.LOCKING
122  self.async_write_ha_stateasync_write_ha_state()
123 
124  await self.coordinator.tedee_client.lock(self._lock_lock.lock_id)
125  await self.coordinator.async_request_refresh()
126  except (TedeeClientException, Exception) as ex:
127  raise HomeAssistantError(
128  translation_domain=DOMAIN,
129  translation_key="lock_failed",
130  translation_placeholders={"lock_id": str(self._lock_lock.lock_id)},
131  ) from ex
132 
133 
135  """A tedee lock but has pullspring enabled, so it additional features."""
136 
137  @property
138  def supported_features(self) -> LockEntityFeature:
139  """Flag supported features."""
140  return LockEntityFeature.OPEN
141 
142  async def async_open(self, **kwargs: Any) -> None:
143  """Open the door with pullspring."""
144  try:
145  self._lock_lock.state = TedeeLockState.UNLOCKING
146  self.async_write_ha_stateasync_write_ha_state()
147 
148  await self.coordinator.tedee_client.open(self._lock_lock.lock_id)
149  await self.coordinator.async_request_refresh()
150  except (TedeeClientException, Exception) as ex:
151  raise HomeAssistantError(
152  translation_domain=DOMAIN,
153  translation_key="open_failed",
154  translation_placeholders={"lock_id": str(self._lock_lock.lock_id)},
155  ) from ex
None __init__(self, TedeeLock lock, TedeeApiCoordinator coordinator)
Definition: lock.py:55
None async_setup_entry(HomeAssistant hass, TedeeConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:23