Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Support for ESPHome locks."""
2 
3 from __future__ import annotations
4 
5 from functools import partial
6 from typing import Any
7 
8 from aioesphomeapi import EntityInfo, LockCommand, LockEntityState, LockInfo, LockState
9 
10 from homeassistant.components.lock import LockEntity, LockEntityFeature
11 from homeassistant.const import ATTR_CODE
12 from homeassistant.core import callback
13 
14 from .entity import (
15  EsphomeEntity,
16  convert_api_error_ha_error,
17  esphome_state_property,
18  platform_async_setup_entry,
19 )
20 
21 
22 class EsphomeLock(EsphomeEntity[LockInfo, LockEntityState], LockEntity):
23  """A lock implementation for ESPHome."""
24 
25  @callback
26  def _on_static_info_update(self, static_info: EntityInfo) -> None:
27  """Set attrs from static info."""
28  super()._on_static_info_update(static_info)
29  static_info = self._static_info_static_info
30  self._attr_assumed_state_attr_assumed_state = static_info.assumed_state
31  self._attr_supported_features_attr_supported_features = LockEntityFeature(0)
32  if static_info.supports_open:
33  self._attr_supported_features_attr_supported_features |= LockEntityFeature.OPEN
34  if static_info.requires_code:
35  self._attr_code_format_attr_code_format = static_info.code_format
36  else:
37  self._attr_code_format_attr_code_format = None
38 
39  @property
40  @esphome_state_property
41  def is_locked(self) -> bool | None:
42  """Return true if the lock is locked."""
43  return self._state_state.state is LockState.LOCKED
44 
45  @property
46  @esphome_state_property
47  def is_locking(self) -> bool | None:
48  """Return true if the lock is locking."""
49  return self._state_state.state is LockState.LOCKING
50 
51  @property
52  @esphome_state_property
53  def is_unlocking(self) -> bool | None:
54  """Return true if the lock is unlocking."""
55  return self._state_state.state is LockState.UNLOCKING
56 
57  @property
58  @esphome_state_property
59  def is_jammed(self) -> bool | None:
60  """Return true if the lock is jammed (incomplete locking)."""
61  return self._state_state.state is LockState.JAMMED
62 
63  @convert_api_error_ha_error
64  async def async_lock(self, **kwargs: Any) -> None:
65  """Lock the lock."""
66  self._client_client.lock_command(self._key_key, LockCommand.LOCK)
67 
68  @convert_api_error_ha_error
69  async def async_unlock(self, **kwargs: Any) -> None:
70  """Unlock the lock."""
71  code = kwargs.get(ATTR_CODE)
72  self._client_client.lock_command(self._key_key, LockCommand.UNLOCK, code)
73 
74  @convert_api_error_ha_error
75  async def async_open(self, **kwargs: Any) -> None:
76  """Open the door latch."""
77  self._client_client.lock_command(self._key_key, LockCommand.OPEN)
78 
79 
80 async_setup_entry = partial(
81  platform_async_setup_entry,
82  info_type=LockInfo,
83  entity_type=EsphomeLock,
84  state_type=LockEntityState,
85 )
None async_open(self, **Any kwargs)
Definition: lock.py:75
None async_lock(self, **Any kwargs)
Definition: lock.py:64
None _on_static_info_update(self, EntityInfo static_info)
Definition: lock.py:26
None async_unlock(self, **Any kwargs)
Definition: lock.py:69