Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Support for Volvo On Call locks."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from volvooncall.dashboard import Instrument, Lock
8 
9 from homeassistant.components.lock import LockEntity
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant, callback
12 from homeassistant.helpers.dispatcher import async_dispatcher_connect
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from .const import DOMAIN, VOLVO_DISCOVERY_NEW
16 from .coordinator import VolvoUpdateCoordinator
17 from .entity import VolvoEntity
18 
19 
21  hass: HomeAssistant,
22  config_entry: ConfigEntry,
23  async_add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Configure locks from a config entry created in the integrations UI."""
26  coordinator: VolvoUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
27  volvo_data = coordinator.volvo_data
28 
29  @callback
30  def async_discover_device(instruments: list[Instrument]) -> None:
31  """Discover and add a discovered Volvo On Call lock."""
33  VolvoLock(
34  coordinator,
35  instrument.vehicle.vin,
36  instrument.component,
37  instrument.attr,
38  instrument.slug_attr,
39  )
40  for instrument in instruments
41  if instrument.component == "lock"
42  )
43 
44  async_discover_device([*volvo_data.instruments])
45 
46  config_entry.async_on_unload(
47  async_dispatcher_connect(hass, VOLVO_DISCOVERY_NEW, async_discover_device)
48  )
49 
50 
52  """Represents a car lock."""
53 
54  instrument: Lock
55 
56  def __init__(
57  self,
58  coordinator: VolvoUpdateCoordinator,
59  vin: str,
60  component: str,
61  attribute: str,
62  slug_attr: str,
63  ) -> None:
64  """Initialize the lock."""
65  super().__init__(vin, component, attribute, slug_attr, coordinator)
66 
67  @property
68  def is_locked(self) -> bool | None:
69  """Determine if car is locked."""
70  return self.instrumentinstrument.is_locked
71 
72  async def async_lock(self, **kwargs: Any) -> None:
73  """Lock the car."""
74  await self.instrumentinstrument.lock()
75  await self.coordinator.async_request_refresh()
76 
77  async def async_unlock(self, **kwargs: Any) -> None:
78  """Unlock the car."""
79  await self.instrumentinstrument.unlock()
80  await self.coordinator.async_request_refresh()
None unlock(self, **Any kwargs)
Definition: __init__.py:241
None lock(self, **Any kwargs)
Definition: __init__.py:228
None __init__(self, VolvoUpdateCoordinator coordinator, str vin, str component, str attribute, str slug_attr)
Definition: lock.py:63
ElkSystem|None async_discover_device(HomeAssistant hass, str host)
Definition: discovery.py:78
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:24
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103