Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Support for ISY locks."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from pyisy.constants import ISY_VALUE_UNKNOWN
8 
9 from homeassistant.components.lock import LockEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import Platform
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.exceptions import HomeAssistantError
14 from homeassistant.helpers.device_registry import DeviceInfo
16  AddEntitiesCallback,
17  async_get_current_platform,
18 )
19 
20 from .const import DOMAIN
21 from .entity import ISYNodeEntity, ISYProgramEntity
22 from .models import IsyData
23 from .services import (
24  SERVICE_DELETE_USER_CODE_SCHEMA,
25  SERVICE_DELETE_ZWAVE_LOCK_USER_CODE,
26  SERVICE_SET_USER_CODE_SCHEMA,
27  SERVICE_SET_ZWAVE_LOCK_USER_CODE,
28 )
29 
30 VALUE_TO_STATE = {0: False, 100: True}
31 
32 
33 @callback
34 def async_setup_lock_services(hass: HomeAssistant) -> None:
35  """Create lock-specific services for the ISY Integration."""
36  platform = async_get_current_platform()
37 
38  platform.async_register_entity_service(
39  SERVICE_SET_ZWAVE_LOCK_USER_CODE,
40  SERVICE_SET_USER_CODE_SCHEMA,
41  "async_set_zwave_lock_user_code",
42  )
43  platform.async_register_entity_service(
44  SERVICE_DELETE_ZWAVE_LOCK_USER_CODE,
45  SERVICE_DELETE_USER_CODE_SCHEMA,
46  "async_delete_zwave_lock_user_code",
47  )
48 
49 
51  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
52 ) -> None:
53  """Set up the ISY lock platform."""
54  isy_data: IsyData = hass.data[DOMAIN][entry.entry_id]
55  devices: dict[str, DeviceInfo] = isy_data.devices
56  entities: list[ISYLockEntity | ISYLockProgramEntity] = [
57  ISYLockEntity(node, devices.get(node.primary_node))
58  for node in isy_data.nodes[Platform.LOCK]
59  ]
60 
61  entities.extend(
62  ISYLockProgramEntity(name, status, actions)
63  for name, status, actions in isy_data.programs[Platform.LOCK]
64  )
65 
66  async_add_entities(entities)
68 
69 
71  """Representation of an ISY lock device."""
72 
73  @property
74  def is_locked(self) -> bool | None:
75  """Get whether the lock is in locked state."""
76  if self._node_node.status == ISY_VALUE_UNKNOWN:
77  return None
78  return VALUE_TO_STATE.get(self._node_node.status)
79 
80  async def async_lock(self, **kwargs: Any) -> None:
81  """Send the lock command to the ISY device."""
82  if not await self._node_node.secure_lock():
83  raise HomeAssistantError(f"Unable to lock device {self._node.address}")
84 
85  async def async_unlock(self, **kwargs: Any) -> None:
86  """Send the unlock command to the ISY device."""
87  if not await self._node_node.secure_unlock():
88  raise HomeAssistantError(f"Unable to unlock device {self._node.address}")
89 
90  async def async_set_zwave_lock_user_code(self, user_num: int, code: int) -> None:
91  """Set a user lock code for a Z-Wave Lock."""
92  if not await self._node_node.set_zwave_lock_code(user_num, code):
93  raise HomeAssistantError(
94  f"Could not set user code {user_num} for {self._node.address}"
95  )
96 
97  async def async_delete_zwave_lock_user_code(self, user_num: int) -> None:
98  """Delete a user lock code for a Z-Wave Lock."""
99  if not await self._node_node.delete_zwave_lock_code(user_num):
100  raise HomeAssistantError(
101  f"Could not delete user code {user_num} for {self._node.address}"
102  )
103 
104 
106  """Representation of a ISY lock program."""
107 
108  @property
109  def is_locked(self) -> bool:
110  """Return true if the device is locked."""
111  return bool(self._node_node.status)
112 
113  async def async_lock(self, **kwargs: Any) -> None:
114  """Lock the device."""
115  if not await self._actions_actions.run_then():
116  raise HomeAssistantError(f"Unable to lock device {self._node.address}")
117 
118  async def async_unlock(self, **kwargs: Any) -> None:
119  """Unlock the device."""
120  if not await self._actions_actions.run_else():
121  raise HomeAssistantError(f"Unable to unlock device {self._node.address}")
None async_delete_zwave_lock_user_code(self, int user_num)
Definition: lock.py:97
None async_set_zwave_lock_user_code(self, int user_num, int code)
Definition: lock.py:90
None async_unlock(self, **Any kwargs)
Definition: lock.py:85
None async_setup_lock_services(HomeAssistant hass)
Definition: lock.py:34
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:52