Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Support for Subaru door locks."""
2 
3 import logging
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 SERVICE_LOCK, SERVICE_UNLOCK
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import entity_platform
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from . import DOMAIN, get_device_info
16 from .const import (
17  ATTR_DOOR,
18  ENTRY_CONTROLLER,
19  ENTRY_VEHICLES,
20  SERVICE_UNLOCK_SPECIFIC_DOOR,
21  UNLOCK_DOOR_ALL,
22  UNLOCK_VALID_DOORS,
23  VEHICLE_HAS_REMOTE_SERVICE,
24  VEHICLE_NAME,
25  VEHICLE_VIN,
26 )
27 from .remote_service import async_call_remote_service
28 
29 _LOGGER = logging.getLogger(__name__)
30 
31 
33  hass: HomeAssistant,
34  config_entry: ConfigEntry,
35  async_add_entities: AddEntitiesCallback,
36 ) -> None:
37  """Set up the Subaru locks by config_entry."""
38  entry = hass.data[DOMAIN][config_entry.entry_id]
39  controller = entry[ENTRY_CONTROLLER]
40  vehicle_info = entry[ENTRY_VEHICLES]
42  SubaruLock(vehicle, controller)
43  for vehicle in vehicle_info.values()
44  if vehicle[VEHICLE_HAS_REMOTE_SERVICE]
45  )
46 
47  platform = entity_platform.async_get_current_platform()
48 
49  platform.async_register_entity_service(
50  SERVICE_UNLOCK_SPECIFIC_DOOR,
51  {vol.Required(ATTR_DOOR): vol.In(UNLOCK_VALID_DOORS)},
52  "async_unlock_specific_door",
53  )
54 
55 
57  """Representation of a Subaru door lock.
58 
59  Note that the Subaru API currently does not support returning the status of the locks. Lock status is always unknown.
60  """
61 
62  _attr_has_entity_name = True
63  _attr_translation_key = "door_locks"
64 
65  def __init__(self, vehicle_info, controller):
66  """Initialize the locks for the vehicle."""
67  self.controllercontroller = controller
68  self.vehicle_infovehicle_info = vehicle_info
69  vin = vehicle_info[VEHICLE_VIN]
70  self.car_namecar_name = vehicle_info[VEHICLE_NAME]
71  self._attr_unique_id_attr_unique_id = f"{vin}_door_locks"
72  self._attr_device_info_attr_device_info = get_device_info(vehicle_info)
73 
74  async def async_lock(self, **kwargs: Any) -> None:
75  """Send the lock command."""
76  _LOGGER.debug("Locking doors for: %s", self.car_namecar_name)
78  self.controllercontroller,
79  SERVICE_LOCK,
80  self.vehicle_infovehicle_info,
81  )
82 
83  async def async_unlock(self, **kwargs: Any) -> None:
84  """Send the unlock command."""
85  _LOGGER.debug("Unlocking doors for: %s", self.car_namecar_name)
87  self.controllercontroller,
88  SERVICE_UNLOCK,
89  self.vehicle_infovehicle_info,
90  UNLOCK_VALID_DOORS[UNLOCK_DOOR_ALL],
91  )
92 
93  async def async_unlock_specific_door(self, door):
94  """Send the unlock command for a specified door."""
95  _LOGGER.debug("Unlocking %s door for: %s", door, self.car_namecar_name)
97  self.controllercontroller,
98  SERVICE_UNLOCK,
99  self.vehicle_infovehicle_info,
100  UNLOCK_VALID_DOORS[door],
101  )
def __init__(self, vehicle_info, controller)
Definition: lock.py:65
None async_unlock(self, **Any kwargs)
Definition: lock.py:83
None async_lock(self, **Any kwargs)
Definition: lock.py:74
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:36
def async_call_remote_service(controller, cmd, vehicle_info, arg=None)
def get_device_info(vehicle_info)
Definition: __init__.py:167