Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Home Assistant component for accessing the Wallbox Portal API. The lock component creates a lock entity."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.lock import LockEntity, LockEntityDescription
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import PlatformNotReady
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .const import (
14  CHARGER_DATA_KEY,
15  CHARGER_LOCKED_UNLOCKED_KEY,
16  CHARGER_SERIAL_NUMBER_KEY,
17  DOMAIN,
18 )
19 from .coordinator import InvalidAuth, WallboxCoordinator
20 from .entity import WallboxEntity
21 
22 LOCK_TYPES: dict[str, LockEntityDescription] = {
23  CHARGER_LOCKED_UNLOCKED_KEY: LockEntityDescription(
24  key=CHARGER_LOCKED_UNLOCKED_KEY,
25  translation_key="lock",
26  ),
27 }
28 
29 
31  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
32 ) -> None:
33  """Create wallbox lock entities in HASS."""
34  coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
35  # Check if the user is authorized to lock, if so, add lock component
36  try:
37  await coordinator.async_set_lock_unlock(
38  coordinator.data[CHARGER_LOCKED_UNLOCKED_KEY]
39  )
40  except InvalidAuth:
41  return
42  except ConnectionError as exc:
43  raise PlatformNotReady from exc
44 
46  WallboxLock(coordinator, description)
47  for ent in coordinator.data
48  if (description := LOCK_TYPES.get(ent))
49  )
50 
51 
53  """Representation of a wallbox lock."""
54 
55  def __init__(
56  self,
57  coordinator: WallboxCoordinator,
58  description: LockEntityDescription,
59  ) -> None:
60  """Initialize a Wallbox lock."""
61 
62  super().__init__(coordinator)
63  self.entity_descriptionentity_description = description
64  self._attr_unique_id_attr_unique_id = f"{description.key}-{coordinator.data[CHARGER_DATA_KEY][CHARGER_SERIAL_NUMBER_KEY]}"
65 
66  @property
67  def is_locked(self) -> bool:
68  """Return the status of the lock."""
69  return self.coordinator.data[CHARGER_LOCKED_UNLOCKED_KEY] # type: ignore[no-any-return]
70 
71  async def async_lock(self, **kwargs: Any) -> None:
72  """Lock charger."""
73  await self.coordinator.async_set_lock_unlock(True)
74 
75  async def async_unlock(self, **kwargs: Any) -> None:
76  """Unlock charger."""
77  await self.coordinator.async_set_lock_unlock(False)
None async_lock(self, **Any kwargs)
Definition: lock.py:71
None __init__(self, WallboxCoordinator coordinator, LockEntityDescription description)
Definition: lock.py:59
None async_unlock(self, **Any kwargs)
Definition: lock.py:75
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:32