Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Support for Xiaomi Aqara locks."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.lock import LockEntity, LockState
6 from homeassistant.config_entries import ConfigEntry
7 from homeassistant.core import HomeAssistant, callback
8 from homeassistant.helpers.entity_platform import AddEntitiesCallback
9 from homeassistant.helpers.event import async_call_later
10 
11 from .const import DOMAIN, GATEWAYS_KEY
12 from .entity import XiaomiDevice
13 
14 FINGER_KEY = "fing_verified"
15 PASSWORD_KEY = "psw_verified"
16 CARD_KEY = "card_verified"
17 VERIFIED_WRONG_KEY = "verified_wrong"
18 
19 ATTR_VERIFIED_WRONG_TIMES = "verified_wrong_times"
20 
21 UNLOCK_MAINTAIN_TIME = 5
22 
23 
25  hass: HomeAssistant,
26  config_entry: ConfigEntry,
27  async_add_entities: AddEntitiesCallback,
28 ) -> None:
29  """Perform the setup for Xiaomi devices."""
30  gateway = hass.data[DOMAIN][GATEWAYS_KEY][config_entry.entry_id]
32  XiaomiAqaraLock(device, "Lock", gateway, config_entry)
33  for device in gateway.devices["lock"]
34  if device["model"] == "lock.aq1"
35  )
36 
37 
39  """Representation of a XiaomiAqaraLock."""
40 
41  def __init__(self, device, name, xiaomi_hub, config_entry):
42  """Initialize the XiaomiAqaraLock."""
43  self._changed_by_changed_by = 0
44  self._verified_wrong_times_verified_wrong_times = 0
45 
46  super().__init__(device, name, xiaomi_hub, config_entry)
47 
48  @property
49  def is_locked(self) -> bool | None:
50  """Return true if lock is locked."""
51  if self._state_state_state is not None:
52  return self._state_state_state == LockState.LOCKED
53  return None
54 
55  @property
56  def changed_by(self) -> str:
57  """Last change triggered by."""
58  return self._changed_by_changed_by
59 
60  @property
61  def extra_state_attributes(self) -> dict[str, int]:
62  """Return the state attributes."""
63  return {ATTR_VERIFIED_WRONG_TIMES: self._verified_wrong_times_verified_wrong_times}
64 
65  @callback
66  def clear_unlock_state(self, _):
67  """Clear unlock state automatically."""
68  self._state_state_state = LockState.LOCKED
69  self.async_write_ha_stateasync_write_ha_state()
70 
71  def parse_data(self, data, raw_data):
72  """Parse data sent by gateway."""
73  if (value := data.get(VERIFIED_WRONG_KEY)) is not None:
74  self._verified_wrong_times_verified_wrong_times = int(value)
75  return True
76 
77  for key in (FINGER_KEY, PASSWORD_KEY, CARD_KEY):
78  if (value := data.get(key)) is not None:
79  self._changed_by_changed_by = int(value)
80  self._verified_wrong_times_verified_wrong_times = 0
81  self._state_state_state = LockState.UNLOCKED
83  self.hasshass, UNLOCK_MAINTAIN_TIME, self.clear_unlock_stateclear_unlock_state
84  )
85  return True
86 
87  return False
def __init__(self, device, name, xiaomi_hub, config_entry)
Definition: lock.py:41
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:28
CALLBACK_TYPE async_call_later(HomeAssistant hass, float|timedelta delay, HassJob[[datetime], Coroutine[Any, Any, None]|None]|Callable[[datetime], Coroutine[Any, Any, None]|None] action)
Definition: event.py:1597