Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Support for Vera locks."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import pyvera as veraApi
8 
9 from homeassistant.components.lock import ENTITY_ID_FORMAT, LockEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .common import ControllerData, get_controller_data
16 from .entity import VeraEntity
17 
18 ATTR_LAST_USER_NAME = "changed_by_name"
19 ATTR_LOW_BATTERY = "low_battery"
20 
21 
23  hass: HomeAssistant,
24  entry: ConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Set up the sensor config entry."""
28  controller_data = get_controller_data(hass, entry)
30  [
31  VeraLock(device, controller_data)
32  for device in controller_data.devices[Platform.LOCK]
33  ],
34  True,
35  )
36 
37 
38 class VeraLock(VeraEntity[veraApi.VeraLock], LockEntity):
39  """Representation of a Vera lock."""
40 
41  def __init__(
42  self, vera_device: veraApi.VeraLock, controller_data: ControllerData
43  ) -> None:
44  """Initialize the Vera device."""
45  VeraEntity.__init__(self, vera_device, controller_data)
46  self.entity_identity_identity_id = ENTITY_ID_FORMAT.format(self.vera_id)
47 
48  def lock(self, **kwargs: Any) -> None:
49  """Lock the device."""
50  self.vera_device.lock()
51  self._attr_is_locked_attr_is_locked = True
52 
53  def unlock(self, **kwargs: Any) -> None:
54  """Unlock the device."""
55  self.vera_device.unlock()
56  self._attr_is_locked_attr_is_locked = False
57 
58  @property
59  def extra_state_attributes(self) -> dict[str, Any] | None:
60  """Who unlocked the lock and did a low battery alert fire.
61 
62  Reports on the previous poll cycle.
63  changed_by_name is a string like 'Bob'.
64  low_battery is 1 if an alert fired, 0 otherwise.
65  """
66  data = super().extra_state_attributes or {}
67 
68  last_user = self.vera_device.get_last_user_alert()
69  if last_user is not None:
70  data[ATTR_LAST_USER_NAME] = last_user[1]
71 
72  data[ATTR_LOW_BATTERY] = self.vera_device.get_low_battery_alert()
73  return data
74 
75  @property
76  def changed_by(self) -> str | None:
77  """Who unlocked the lock.
78 
79  Reports on the previous poll cycle.
80  changed_by is an integer user ID.
81  """
82  last_user = self.vera_device.get_last_user_alert()
83  if last_user is not None:
84  return last_user[0]
85  return None
86 
87  def update(self) -> None:
88  """Update state by the Vera device callback."""
89  self._attr_is_locked_attr_is_locked = self.vera_device.is_locked(True)
None __init__(self, veraApi.VeraLock vera_device, ControllerData controller_data)
Definition: lock.py:43
None lock(self, **Any kwargs)
Definition: lock.py:48
None unlock(self, **Any kwargs)
Definition: lock.py:53
dict[str, Any]|None extra_state_attributes(self)
Definition: lock.py:59
ControllerData get_controller_data(HomeAssistant hass, ConfigEntry config_entry)
Definition: common.py:40
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:26