Home Assistant Unofficial Reference 2024.12.1
lock.py
Go to the documentation of this file.
1 """Lock platform for Tesla Fleet integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from tesla_fleet_api.const import Scope
8 
9 from homeassistant.components.lock import LockEntity
10 from homeassistant.core import HomeAssistant
11 from homeassistant.exceptions import ServiceValidationError
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import TeslaFleetConfigEntry
15 from .const import DOMAIN
16 from .entity import TeslaFleetVehicleEntity
17 from .helpers import handle_vehicle_command
18 from .models import TeslaFleetVehicleData
19 
20 ENGAGED = "Engaged"
21 
22 PARALLEL_UPDATES = 0
23 
24 
26  hass: HomeAssistant,
27  entry: TeslaFleetConfigEntry,
28  async_add_entities: AddEntitiesCallback,
29 ) -> None:
30  """Set up the TeslaFleet lock platform from a config entry."""
31 
33  klass(vehicle, Scope.VEHICLE_CMDS in entry.runtime_data.scopes)
34  for klass in (
35  TeslaFleetVehicleLockEntity,
36  TeslaFleetCableLockEntity,
37  )
38  for vehicle in entry.runtime_data.vehicles
39  )
40 
41 
43  """Lock entity for TeslaFleet."""
44 
45  def __init__(self, data: TeslaFleetVehicleData, scoped: bool) -> None:
46  """Initialize the lock."""
47  super().__init__(data, "vehicle_state_locked")
48  self.scopedscoped = scoped
49 
50  def _async_update_attrs(self) -> None:
51  """Update entity attributes."""
52  self._attr_is_locked_attr_is_locked = self._value_value_value
53 
54  async def async_lock(self, **kwargs: Any) -> None:
55  """Lock the doors."""
56  self.raise_for_read_onlyraise_for_read_only(Scope.VEHICLE_CMDS)
57  await self.wake_up_if_asleepwake_up_if_asleep()
58  await handle_vehicle_command(self.apiapiapiapiapi.door_lock())
59  self._attr_is_locked_attr_is_locked = True
60  self.async_write_ha_stateasync_write_ha_state()
61 
62  async def async_unlock(self, **kwargs: Any) -> None:
63  """Unlock the doors."""
64  self.raise_for_read_onlyraise_for_read_only(Scope.VEHICLE_CMDS)
65  await self.wake_up_if_asleepwake_up_if_asleep()
66  await handle_vehicle_command(self.apiapiapiapiapi.door_unlock())
67  self._attr_is_locked_attr_is_locked = False
68  self.async_write_ha_stateasync_write_ha_state()
69 
70 
72  """Cable Lock entity for TeslaFleet."""
73 
74  def __init__(
75  self,
76  data: TeslaFleetVehicleData,
77  scoped: bool,
78  ) -> None:
79  """Initialize the lock."""
80  super().__init__(data, "charge_state_charge_port_latch")
81  self.scopedscoped = scoped
82 
83  def _async_update_attrs(self) -> None:
84  """Update entity attributes."""
85  if self._value_value_value_value is None:
86  self._attr_is_locked_attr_is_locked = None
87  self._attr_is_locked_attr_is_locked = self._value_value_value_value == ENGAGED
88 
89  async def async_lock(self, **kwargs: Any) -> None:
90  """Charge cable Lock cannot be manually locked."""
92  "Insert cable to lock",
93  translation_domain=DOMAIN,
94  translation_key="no_cable",
95  )
96 
97  async def async_unlock(self, **kwargs: Any) -> None:
98  """Unlock charge cable lock."""
99  self.raise_for_read_onlyraise_for_read_only(Scope.VEHICLE_CMDS)
100  await self.wake_up_if_asleepwake_up_if_asleep()
101  await handle_vehicle_command(self.apiapiapiapiapi.charge_port_door_open())
102  self._attr_is_locked_attr_is_locked = False
103  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, TeslaFleetVehicleData data, bool scoped)
Definition: lock.py:78
None __init__(self, TeslaFleetVehicleData data, bool scoped)
Definition: lock.py:45
bool handle_vehicle_command(Awaitable command)
Definition: helpers.py:50
None async_setup_entry(HomeAssistant hass, TeslaFleetConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: lock.py:29