Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """YoLink Lock V1/V2."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from yolink.client_request import ClientRequest
8 from yolink.const import ATTR_DEVICE_LOCK, ATTR_DEVICE_LOCK_V2
9 
10 from homeassistant.components.lock import LockEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .const import DOMAIN
16 from .coordinator import YoLinkCoordinator
17 from .entity import YoLinkEntity
18 
19 
21  hass: HomeAssistant,
22  config_entry: ConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Set up YoLink lock from a config entry."""
26  device_coordinators = hass.data[DOMAIN][config_entry.entry_id].device_coordinators
27  entities = [
28  YoLinkLockEntity(config_entry, device_coordinator)
29  for device_coordinator in device_coordinators.values()
30  if device_coordinator.device.device_type
31  in [ATTR_DEVICE_LOCK, ATTR_DEVICE_LOCK_V2]
32  ]
33  async_add_entities(entities)
34 
35 
37  """YoLink Lock Entity."""
38 
39  _attr_name = None
40 
41  def __init__(
42  self,
43  config_entry: ConfigEntry,
44  coordinator: YoLinkCoordinator,
45  ) -> None:
46  """Init YoLink Lock."""
47  super().__init__(config_entry, coordinator)
48  self._attr_unique_id_attr_unique_id = f"{coordinator.device.device_id}_lock_state"
49 
50  @callback
51  def update_entity_state(self, state: dict[str, Any]) -> None:
52  """Update HA Entity State."""
53  state_value = state.get("state")
54  if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2:
55  self._attr_is_locked_attr_is_locked = (
56  state_value["lock"] == "locked" if state_value is not None else None
57  )
58  else:
59  self._attr_is_locked_attr_is_locked = (
60  state_value == "locked" if state_value is not None else None
61  )
62  self.async_write_ha_stateasync_write_ha_state()
63 
64  async def call_lock_state_change(self, state: str) -> None:
65  """Call setState api to change lock state."""
66  if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2:
67  await self.call_devicecall_device(
68  ClientRequest("setState", {"state": {"lock": state}})
69  )
70  else:
71  await self.call_devicecall_device(ClientRequest("setState", {"state": state}))
72  self._attr_is_locked_attr_is_locked = state == "lock"
73  self.async_write_ha_stateasync_write_ha_state()
74 
75  async def async_lock(self, **kwargs: Any) -> None:
76  """Lock device."""
77  state_param = (
78  "locked"
79  if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2
80  else "lock"
81  )
82  await self.call_lock_state_changecall_lock_state_change(state_param)
83 
84  async def async_unlock(self, **kwargs: Any) -> None:
85  """Unlock device."""
86  state_param = (
87  "unlocked"
88  if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2
89  else "unlock"
90  )
91  await self.call_lock_state_changecall_lock_state_change(state_param)