Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Support for Freedompro lock."""
2 
3 import json
4 from typing import Any
5 
6 from pyfreedompro import put_state
7 
8 from homeassistant.components.lock import LockEntity
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_API_KEY
11 from homeassistant.core import HomeAssistant, callback
12 from homeassistant.helpers import aiohttp_client
13 from homeassistant.helpers.device_registry import DeviceInfo
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 from homeassistant.helpers.update_coordinator import CoordinatorEntity
16 
17 from .const import DOMAIN
18 from .coordinator import FreedomproDataUpdateCoordinator
19 
20 
22  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
23 ) -> None:
24  """Set up Freedompro lock."""
25  api_key: str = entry.data[CONF_API_KEY]
26  coordinator: FreedomproDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
28  Device(hass, api_key, device, coordinator)
29  for device in coordinator.data
30  if device["type"] == "lock"
31  )
32 
33 
34 class Device(CoordinatorEntity[FreedomproDataUpdateCoordinator], LockEntity):
35  """Representation of a Freedompro lock."""
36 
37  _attr_has_entity_name = True
38  _attr_name = None
39 
40  def __init__(
41  self,
42  hass: HomeAssistant,
43  api_key: str,
44  device: dict[str, Any],
45  coordinator: FreedomproDataUpdateCoordinator,
46  ) -> None:
47  """Initialize the Freedompro lock."""
48  super().__init__(coordinator)
49  self._hass_hass_hass = hass
50  self._session_session = aiohttp_client.async_get_clientsession(self._hass_hass_hass)
51  self._api_key_api_key_api_key = api_key
52  self._attr_unique_id_attr_unique_id = device["uid"]
53  self._type_type = device["type"]
54  self._characteristics_characteristics = device["characteristics"]
55  self._attr_device_info_attr_device_info = DeviceInfo(
56  identifiers={
57  (DOMAIN, device["uid"]),
58  },
59  manufacturer="Freedompro",
60  model=self._type_type,
61  name=device["name"],
62  )
63 
64  @callback
65  def _handle_coordinator_update(self) -> None:
66  """Handle updated data from the coordinator."""
67  device = next(
68  (
69  device
70  for device in self.coordinator.data
71  if device["uid"] == self.unique_idunique_id
72  ),
73  None,
74  )
75  if device is not None and "state" in device:
76  state = device["state"]
77  if "lock" in state:
78  if state["lock"] == 1:
79  self._attr_is_locked_attr_is_locked = True
80  else:
81  self._attr_is_locked_attr_is_locked = False
83 
84  async def async_added_to_hass(self) -> None:
85  """When entity is added to hass."""
86  await super().async_added_to_hass()
87  self._handle_coordinator_update_handle_coordinator_update()
88 
89  async def async_lock(self, **kwargs: Any) -> None:
90  """Async function to lock the lock."""
91  payload = {"lock": 1}
92  await put_state(
93  self._session_session,
94  self._api_key_api_key_api_key,
95  self.unique_idunique_id,
96  json.dumps(payload),
97  )
98  await self.coordinator.async_request_refresh()
99 
100  async def async_unlock(self, **kwargs: Any) -> None:
101  """Async function to unlock the lock."""
102  payload = {"lock": 0}
103  await put_state(
104  self._session_session,
105  self._api_key_api_key_api_key,
106  self.unique_idunique_id,
107  json.dumps(payload),
108  )
109  await self.coordinator.async_request_refresh()
None async_lock(self, **Any kwargs)
Definition: lock.py:89
None async_unlock(self, **Any kwargs)
Definition: lock.py:100
None __init__(self, HomeAssistant hass, str api_key, dict[str, Any] device, FreedomproDataUpdateCoordinator coordinator)
Definition: lock.py:46
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:23