Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Demo lock platform that implements locks."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from typing import Any
7 
8 from homeassistant.components.lock import LockEntity, LockEntityFeature, LockState
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 LOCK_UNLOCK_DELAY = 2 # Used to give a realistic lock/unlock experience in frontend
14 
15 
17  hass: HomeAssistant,
18  config_entry: ConfigEntry,
19  async_add_entities: AddEntitiesCallback,
20 ) -> None:
21  """Set up the Demo config entry."""
23  [
24  DemoLock("Front Door", LockState.LOCKED),
25  DemoLock("Kitchen Door", LockState.UNLOCKED),
26  DemoLock("Poorly Installed Door", LockState.UNLOCKED, False, True),
27  DemoLock("Openable Lock", LockState.LOCKED, True),
28  ]
29  )
30 
31 
33  """Representation of a Demo lock."""
34 
35  _attr_should_poll = False
36 
37  def __init__(
38  self,
39  name: str,
40  state: str,
41  openable: bool = False,
42  jam_on_operation: bool = False,
43  ) -> None:
44  """Initialize the lock."""
45  self._attr_name_attr_name = name
46  if openable:
47  self._attr_supported_features_attr_supported_features = LockEntityFeature.OPEN
48  self._state_state = state
49  self._openable_openable = openable
50  self._jam_on_operation_jam_on_operation = jam_on_operation
51 
52  @property
53  def is_locking(self) -> bool:
54  """Return true if lock is locking."""
55  return self._state_state == LockState.LOCKING
56 
57  @property
58  def is_unlocking(self) -> bool:
59  """Return true if lock is unlocking."""
60  return self._state_state == LockState.UNLOCKING
61 
62  @property
63  def is_jammed(self) -> bool:
64  """Return true if lock is jammed."""
65  return self._state_state == LockState.JAMMED
66 
67  @property
68  def is_locked(self) -> bool:
69  """Return true if lock is locked."""
70  return self._state_state == LockState.LOCKED
71 
72  @property
73  def is_open(self) -> bool:
74  """Return true if lock is open."""
75  return self._state_state == LockState.OPEN
76 
77  @property
78  def is_opening(self) -> bool:
79  """Return true if lock is opening."""
80  return self._state_state == LockState.OPENING
81 
82  async def async_lock(self, **kwargs: Any) -> None:
83  """Lock the device."""
84  self._state_state = LockState.LOCKING
85  self.async_write_ha_stateasync_write_ha_state()
86  await asyncio.sleep(LOCK_UNLOCK_DELAY)
87  if self._jam_on_operation_jam_on_operation:
88  self._state_state = LockState.JAMMED
89  else:
90  self._state_state = LockState.LOCKED
91  self.async_write_ha_stateasync_write_ha_state()
92 
93  async def async_unlock(self, **kwargs: Any) -> None:
94  """Unlock the device."""
95  self._state_state = LockState.UNLOCKING
96  self.async_write_ha_stateasync_write_ha_state()
97  await asyncio.sleep(LOCK_UNLOCK_DELAY)
98  self._state_state = LockState.UNLOCKED
99  self.async_write_ha_stateasync_write_ha_state()
100 
101  async def async_open(self, **kwargs: Any) -> None:
102  """Open the door latch."""
103  self._state_state = LockState.OPENING
104  self.async_write_ha_stateasync_write_ha_state()
105  await asyncio.sleep(LOCK_UNLOCK_DELAY)
106  self._state_state = LockState.OPEN
107  self.async_write_ha_stateasync_write_ha_state()
None async_unlock(self, **Any kwargs)
Definition: lock.py:93
None async_lock(self, **Any kwargs)
Definition: lock.py:82
None __init__(self, str name, str state, bool openable=False, bool jam_on_operation=False)
Definition: lock.py:43
None async_open(self, **Any kwargs)
Definition: lock.py:101
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:20