Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Locks on Zigbee Home Automation networks."""
2 
3 import functools
4 from typing import Any
5 
6 import voluptuous as vol
7 
8 from homeassistant.components.lock import LockEntity
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import Platform
11 from homeassistant.core import HomeAssistant, State, callback
12 from homeassistant.helpers import config_validation as cv
13 from homeassistant.helpers.dispatcher import async_dispatcher_connect
15  AddEntitiesCallback,
16  async_get_current_platform,
17 )
18 
19 from .entity import ZHAEntity
20 from .helpers import (
21  SIGNAL_ADD_ENTITIES,
22  async_add_entities as zha_async_add_entities,
23  convert_zha_error_to_ha_error,
24  get_zha_data,
25 )
26 
27 SERVICE_SET_LOCK_USER_CODE = "set_lock_user_code"
28 SERVICE_ENABLE_LOCK_USER_CODE = "enable_lock_user_code"
29 SERVICE_DISABLE_LOCK_USER_CODE = "disable_lock_user_code"
30 SERVICE_CLEAR_LOCK_USER_CODE = "clear_lock_user_code"
31 
32 
34  hass: HomeAssistant,
35  config_entry: ConfigEntry,
36  async_add_entities: AddEntitiesCallback,
37 ) -> None:
38  """Set up the Zigbee Home Automation Door Lock from config entry."""
39  zha_data = get_zha_data(hass)
40  entities_to_create = zha_data.platforms[Platform.LOCK]
41 
43  hass,
44  SIGNAL_ADD_ENTITIES,
45  functools.partial(
46  zha_async_add_entities, async_add_entities, ZhaDoorLock, entities_to_create
47  ),
48  )
49  config_entry.async_on_unload(unsub)
50 
51  platform = async_get_current_platform()
52 
53  platform.async_register_entity_service(
54  SERVICE_SET_LOCK_USER_CODE,
55  {
56  vol.Required("code_slot"): vol.Coerce(int),
57  vol.Required("user_code"): cv.string,
58  },
59  "async_set_lock_user_code",
60  )
61 
62  platform.async_register_entity_service(
63  SERVICE_ENABLE_LOCK_USER_CODE,
64  {
65  vol.Required("code_slot"): vol.Coerce(int),
66  },
67  "async_enable_lock_user_code",
68  )
69 
70  platform.async_register_entity_service(
71  SERVICE_DISABLE_LOCK_USER_CODE,
72  {
73  vol.Required("code_slot"): vol.Coerce(int),
74  },
75  "async_disable_lock_user_code",
76  )
77 
78  platform.async_register_entity_service(
79  SERVICE_CLEAR_LOCK_USER_CODE,
80  {
81  vol.Required("code_slot"): vol.Coerce(int),
82  },
83  "async_clear_lock_user_code",
84  )
85 
86 
88  """Representation of a ZHA lock."""
89 
90  _attr_translation_key: str = "door_lock"
91 
92  @property
93  def is_locked(self) -> bool:
94  """Return true if entity is locked."""
95  return self.entity_data.entity.is_locked
96 
97  @convert_zha_error_to_ha_error
98  async def async_lock(self, **kwargs: Any) -> None:
99  """Lock the lock."""
100  await self.entity_data.entity.async_lock()
101  self.async_write_ha_stateasync_write_ha_state()
102 
103  @convert_zha_error_to_ha_error
104  async def async_unlock(self, **kwargs: Any) -> None:
105  """Unlock the lock."""
106  await self.entity_data.entity.async_unlock()
107  self.async_write_ha_stateasync_write_ha_state()
108 
109  @convert_zha_error_to_ha_error
110  async def async_set_lock_user_code(self, code_slot: int, user_code: str) -> None:
111  """Set the user_code to index X on the lock."""
112  await self.entity_data.entity.async_set_lock_user_code(
113  code_slot=code_slot, user_code=user_code
114  )
115  self.async_write_ha_stateasync_write_ha_state()
116 
117  @convert_zha_error_to_ha_error
118  async def async_enable_lock_user_code(self, code_slot: int) -> None:
119  """Enable user_code at index X on the lock."""
120  await self.entity_data.entity.async_enable_lock_user_code(code_slot=code_slot)
121  self.async_write_ha_stateasync_write_ha_state()
122 
123  @convert_zha_error_to_ha_error
124  async def async_disable_lock_user_code(self, code_slot: int) -> None:
125  """Disable user_code at index X on the lock."""
126  await self.entity_data.entity.async_disable_lock_user_code(code_slot=code_slot)
127  self.async_write_ha_stateasync_write_ha_state()
128 
129  @convert_zha_error_to_ha_error
130  async def async_clear_lock_user_code(self, code_slot: int) -> None:
131  """Clear the user_code at index X on the lock."""
132  await self.entity_data.entity.async_clear_lock_user_code(code_slot=code_slot)
133  self.async_write_ha_stateasync_write_ha_state()
134 
135  @callback
136  def restore_external_state_attributes(self, state: State) -> None:
137  """Restore entity state."""
138  self.entity_data.entity.restore_external_state_attributes(
139  state=state.state,
140  )
None async_clear_lock_user_code(self, int code_slot)
Definition: lock.py:130
None async_disable_lock_user_code(self, int code_slot)
Definition: lock.py:124
None async_lock(self, **Any kwargs)
Definition: lock.py:98
None restore_external_state_attributes(self, State state)
Definition: lock.py:136
None async_set_lock_user_code(self, int code_slot, str user_code)
Definition: lock.py:110
None async_enable_lock_user_code(self, int code_slot)
Definition: lock.py:118
None async_unlock(self, **Any kwargs)
Definition: lock.py:104
HAZHAData get_zha_data(HomeAssistant hass)
Definition: helpers.py:1020
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:37
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103