Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Support for HomematicIP Cloud lock devices."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from homematicip.aio.device import AsyncDoorLockDrive
9 from homematicip.base.enums import LockState, MotorState
10 
11 from homeassistant.components.lock import LockEntity, LockEntityFeature
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import DOMAIN
17 from .entity import HomematicipGenericEntity
18 from .helpers import handle_errors
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 ATTR_AUTO_RELOCK_DELAY = "auto_relock_delay"
23 ATTR_DOOR_HANDLE_TYPE = "door_handle_type"
24 ATTR_DOOR_LOCK_DIRECTION = "door_lock_direction"
25 ATTR_DOOR_LOCK_NEUTRAL_POSITION = "door_lock_neutral_position"
26 ATTR_DOOR_LOCK_TURNS = "door_lock_turns"
27 
28 DEVICE_DLD_ATTRIBUTES = {
29  "autoRelockDelay": ATTR_AUTO_RELOCK_DELAY,
30  "doorHandleType": ATTR_DOOR_HANDLE_TYPE,
31  "doorLockDirection": ATTR_DOOR_LOCK_DIRECTION,
32  "doorLockNeutralPosition": ATTR_DOOR_LOCK_NEUTRAL_POSITION,
33  "doorLockTurns": ATTR_DOOR_LOCK_TURNS,
34 }
35 
36 
38  hass: HomeAssistant,
39  config_entry: ConfigEntry,
40  async_add_entities: AddEntitiesCallback,
41 ) -> None:
42  """Set up the HomematicIP locks from a config entry."""
43  hap = hass.data[DOMAIN][config_entry.unique_id]
44 
46  HomematicipDoorLockDrive(hap, device)
47  for device in hap.home.devices
48  if isinstance(device, AsyncDoorLockDrive)
49  )
50 
51 
53  """Representation of the HomematicIP DoorLockDrive."""
54 
55  _attr_supported_features = LockEntityFeature.OPEN
56 
57  @property
58  def is_locked(self) -> bool | None:
59  """Return true if device is locked."""
60  return (
61  self._device_device.lockState == LockState.LOCKED
62  and self._device_device.motorState == MotorState.STOPPED
63  )
64 
65  @property
66  def is_locking(self) -> bool:
67  """Return true if device is locking."""
68  return self._device_device.motorState == MotorState.CLOSING
69 
70  @property
71  def is_unlocking(self) -> bool:
72  """Return true if device is unlocking."""
73  return self._device_device.motorState == MotorState.OPENING
74 
75  @handle_errors
76  async def async_lock(self, **kwargs: Any) -> None:
77  """Lock the device."""
78  return await self._device_device.set_lock_state(LockState.LOCKED)
79 
80  @handle_errors
81  async def async_unlock(self, **kwargs: Any) -> None:
82  """Unlock the device."""
83  return await self._device_device.set_lock_state(LockState.UNLOCKED)
84 
85  @handle_errors
86  async def async_open(self, **kwargs: Any) -> None:
87  """Open the door latch."""
88  return await self._device_device.set_lock_state(LockState.OPEN)
89 
90  @property
91  def extra_state_attributes(self) -> dict[str, Any]:
92  """Return the state attributes of the device."""
93  return super().extra_state_attributes | {
94  attr_key: attr_value
95  for attr, attr_key in DEVICE_DLD_ATTRIBUTES.items()
96  if (attr_value := getattr(self._device_device, attr, None)) is not None
97  }
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:41