Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Cover 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, SunRoofCommand, Trunk, WindowCommand
8 
10  CoverDeviceClass,
11  CoverEntity,
12  CoverEntityFeature,
13 )
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import TeslaFleetConfigEntry
18 from .entity import TeslaFleetVehicleEntity
19 from .helpers import handle_vehicle_command
20 from .models import TeslaFleetVehicleData
21 
22 OPEN = 1
23 CLOSED = 0
24 
25 PARALLEL_UPDATES = 0
26 
27 
29  hass: HomeAssistant,
30  entry: TeslaFleetConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up the TeslaFleet cover platform from a config entry."""
34 
36  klass(vehicle, entry.runtime_data.scopes)
37  for (klass) in (
38  TeslaFleetWindowEntity,
39  TeslaFleetChargePortEntity,
40  TeslaFleetFrontTrunkEntity,
41  TeslaFleetRearTrunkEntity,
42  TeslaFleetSunroofEntity,
43  )
44  for vehicle in entry.runtime_data.vehicles
45  )
46 
47 
49  """Cover entity for the windows."""
50 
51  _attr_device_class = CoverDeviceClass.WINDOW
52 
53  def __init__(self, data: TeslaFleetVehicleData, scopes: list[Scope]) -> None:
54  """Initialize the cover."""
55  super().__init__(data, "windows")
56  self.scopedscoped = Scope.VEHICLE_CMDS in scopes
57  self._attr_supported_features_attr_supported_features = (
58  CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
59  )
60  if not self.scopedscoped:
61  self._attr_supported_features_attr_supported_features = CoverEntityFeature(0)
62 
63  def _async_update_attrs(self) -> None:
64  """Update the entity attributes."""
65  fd = self.getget("vehicle_state_fd_window")
66  fp = self.getget("vehicle_state_fp_window")
67  rd = self.getget("vehicle_state_rd_window")
68  rp = self.getget("vehicle_state_rp_window")
69 
70  # Any open set to open
71  if OPEN in (fd, fp, rd, rp):
72  self._attr_is_closed_attr_is_closed = False
73  # All closed set to closed
74  elif CLOSED == fd == fp == rd == rp:
75  self._attr_is_closed_attr_is_closed = True
76  # Otherwise, set to unknown
77  else:
78  self._attr_is_closed_attr_is_closed = None
79 
80  async def async_open_cover(self, **kwargs: Any) -> None:
81  """Vent windows."""
82  await self.wake_up_if_asleepwake_up_if_asleep()
84  self.apiapiapiapiapi.window_control(command=WindowCommand.VENT)
85  )
86  self._attr_is_closed_attr_is_closed = False
87  self.async_write_ha_stateasync_write_ha_state()
88 
89  async def async_close_cover(self, **kwargs: Any) -> None:
90  """Close windows."""
91  await self.wake_up_if_asleepwake_up_if_asleep()
93  self.apiapiapiapiapi.window_control(command=WindowCommand.CLOSE)
94  )
95  self._attr_is_closed_attr_is_closed = True
96  self.async_write_ha_stateasync_write_ha_state()
97 
98 
100  """Cover entity for the charge port."""
101 
102  _attr_device_class = CoverDeviceClass.DOOR
103 
104  def __init__(self, vehicle: TeslaFleetVehicleData, scopes: list[Scope]) -> None:
105  """Initialize the cover."""
106  super().__init__(vehicle, "charge_state_charge_port_door_open")
107  self.scopedscoped = any(
108  scope in scopes
109  for scope in (Scope.VEHICLE_CMDS, Scope.VEHICLE_CHARGING_CMDS)
110  )
111  self._attr_supported_features_attr_supported_features = (
112  CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
113  )
114  if not self.scopedscoped:
115  self._attr_supported_features_attr_supported_features = CoverEntityFeature(0)
116 
117  def _async_update_attrs(self) -> None:
118  """Update the entity attributes."""
119  self._attr_is_closed_attr_is_closed = not self._value_value_value
120 
121  async def async_open_cover(self, **kwargs: Any) -> None:
122  """Open charge port."""
123  await self.wake_up_if_asleepwake_up_if_asleep()
124  await handle_vehicle_command(self.apiapiapiapiapi.charge_port_door_open())
125  self._attr_is_closed_attr_is_closed = False
126  self.async_write_ha_stateasync_write_ha_state()
127 
128  async def async_close_cover(self, **kwargs: Any) -> None:
129  """Close charge port."""
130  await self.wake_up_if_asleepwake_up_if_asleep()
131  await handle_vehicle_command(self.apiapiapiapiapi.charge_port_door_close())
132  self._attr_is_closed_attr_is_closed = True
133  self.async_write_ha_stateasync_write_ha_state()
134 
135 
137  """Cover entity for the front trunk."""
138 
139  _attr_device_class = CoverDeviceClass.DOOR
140 
141  def __init__(self, vehicle: TeslaFleetVehicleData, scopes: list[Scope]) -> None:
142  """Initialize the cover."""
143  super().__init__(vehicle, "vehicle_state_ft")
144 
145  self.scopedscoped = Scope.VEHICLE_CMDS in scopes
146  self._attr_supported_features_attr_supported_features = CoverEntityFeature.OPEN
147  if not self.scopedscoped:
148  self._attr_supported_features_attr_supported_features = CoverEntityFeature(0)
149 
150  def _async_update_attrs(self) -> None:
151  """Update the entity attributes."""
152  self._attr_is_closed_attr_is_closed = self._value_value_value_value == CLOSED
153 
154  async def async_open_cover(self, **kwargs: Any) -> None:
155  """Open front trunk."""
156  await self.wake_up_if_asleepwake_up_if_asleep()
157  await handle_vehicle_command(self.apiapiapiapiapi.actuate_trunk(Trunk.FRONT))
158  self._attr_is_closed_attr_is_closed = False
159  self.async_write_ha_stateasync_write_ha_state()
160 
161 
163  """Cover entity for the rear trunk."""
164 
165  _attr_device_class = CoverDeviceClass.DOOR
166 
167  def __init__(self, vehicle: TeslaFleetVehicleData, scopes: list[Scope]) -> None:
168  """Initialize the cover."""
169  super().__init__(vehicle, "vehicle_state_rt")
170 
171  self.scopedscoped = Scope.VEHICLE_CMDS in scopes
172  self._attr_supported_features_attr_supported_features = (
173  CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
174  )
175  if not self.scopedscoped:
176  self._attr_supported_features_attr_supported_features = CoverEntityFeature(0)
177 
178  def _async_update_attrs(self) -> None:
179  """Update the entity attributes."""
180  self._attr_is_closed_attr_is_closed = self._value_value_value_value == CLOSED
181 
182  async def async_open_cover(self, **kwargs: Any) -> None:
183  """Open rear trunk."""
184  if self.is_closedis_closed is not False:
185  await self.wake_up_if_asleepwake_up_if_asleep()
186  await handle_vehicle_command(self.apiapiapiapiapi.actuate_trunk(Trunk.REAR))
187  self._attr_is_closed_attr_is_closed = False
188  self.async_write_ha_stateasync_write_ha_state()
189 
190  async def async_close_cover(self, **kwargs: Any) -> None:
191  """Close rear trunk."""
192  if self.is_closedis_closed is not True:
193  await self.wake_up_if_asleepwake_up_if_asleep()
194  await handle_vehicle_command(self.apiapiapiapiapi.actuate_trunk(Trunk.REAR))
195  self._attr_is_closed_attr_is_closed = True
196  self.async_write_ha_stateasync_write_ha_state()
197 
198 
200  """Cover entity for the sunroof."""
201 
202  _attr_device_class = CoverDeviceClass.WINDOW
203  _attr_supported_features = (
204  CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP
205  )
206  _attr_entity_registry_enabled_default = False
207 
208  def __init__(self, vehicle: TeslaFleetVehicleData, scopes: list[Scope]) -> None:
209  """Initialize the sensor."""
210  super().__init__(vehicle, "vehicle_state_sun_roof_state")
211 
212  self.scopedscoped = Scope.VEHICLE_CMDS in scopes
213  if not self.scopedscoped:
215 
216  def _async_update_attrs(self) -> None:
217  """Update the entity attributes."""
218  value = self._value_value_value
219  if value in (None, "unknown"):
220  self._attr_is_closed_attr_is_closed = None
221  else:
222  self._attr_is_closed_attr_is_closed = value == "closed"
223 
224  self._attr_current_cover_position_attr_current_cover_position = self.getget(
225  "vehicle_state_sun_roof_percent_open"
226  )
227 
228  async def async_open_cover(self, **kwargs: Any) -> None:
229  """Open sunroof."""
230  await self.wake_up_if_asleepwake_up_if_asleep()
231  await handle_vehicle_command(self.apiapiapiapiapi.sun_roof_control(SunRoofCommand.VENT))
232  self._attr_is_closed_attr_is_closed = False
233  self.async_write_ha_stateasync_write_ha_state()
234 
235  async def async_close_cover(self, **kwargs: Any) -> None:
236  """Close sunroof."""
237  await self.wake_up_if_asleepwake_up_if_asleep()
238  await handle_vehicle_command(self.apiapiapiapiapi.sun_roof_control(SunRoofCommand.CLOSE))
239  self._attr_is_closed_attr_is_closed = True
240  self.async_write_ha_stateasync_write_ha_state()
241 
242  async def async_stop_cover(self, **kwargs: Any) -> None:
243  """Close sunroof."""
244  await self.wake_up_if_asleepwake_up_if_asleep()
245  await handle_vehicle_command(self.apiapiapiapiapi.sun_roof_control(SunRoofCommand.STOP))
246  self._attr_is_closed_attr_is_closed = False
247  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, TeslaFleetVehicleData vehicle, list[Scope] scopes)
Definition: cover.py:104
None __init__(self, TeslaFleetVehicleData vehicle, list[Scope] scopes)
Definition: cover.py:141
None __init__(self, TeslaFleetVehicleData vehicle, list[Scope] scopes)
Definition: cover.py:167
None __init__(self, TeslaFleetVehicleData vehicle, list[Scope] scopes)
Definition: cover.py:208
None __init__(self, TeslaFleetVehicleData data, list[Scope] scopes)
Definition: cover.py:53
Any|None get(self, str key, Any|None default=None)
Definition: entity.py:61
None async_setup_entry(HomeAssistant hass, TeslaFleetConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:32
bool handle_vehicle_command(Awaitable command)
Definition: helpers.py:50