Home Assistant Unofficial Reference 2024.12.1
update.py
Go to the documentation of this file.
1 """Update platform for Teslemetry integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any, cast
6 
7 from tesla_fleet_api.const import Scope
8 
9 from homeassistant.components.update import UpdateEntity, UpdateEntityFeature
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from . import TeslemetryConfigEntry
14 from .entity import TeslemetryVehicleEntity
15 from .helpers import handle_vehicle_command
16 from .models import TeslemetryVehicleData
17 
18 AVAILABLE = "available"
19 DOWNLOADING = "downloading"
20 INSTALLING = "installing"
21 WIFI_WAIT = "downloading_wifi_wait"
22 SCHEDULED = "scheduled"
23 
24 PARALLEL_UPDATES = 0
25 
26 
28  hass: HomeAssistant,
29  entry: TeslemetryConfigEntry,
30  async_add_entities: AddEntitiesCallback,
31 ) -> None:
32  """Set up the Teslemetry update platform from a config entry."""
33 
35  TeslemetryUpdateEntity(vehicle, entry.runtime_data.scopes)
36  for vehicle in entry.runtime_data.vehicles
37  )
38 
39 
41  """Teslemetry Updates entity."""
42 
43  def __init__(
44  self,
45  data: TeslemetryVehicleData,
46  scopes: list[Scope],
47  ) -> None:
48  """Initialize the Update."""
49  self.scopedscoped = Scope.VEHICLE_CMDS in scopes
50  super().__init__(
51  data,
52  "vehicle_state_software_update_status",
53  )
54 
55  def _async_update_attrs(self) -> None:
56  """Update the attributes of the entity."""
57 
58  # Supported Features
59  if self.scopedscoped and self._value_value_value in (
60  AVAILABLE,
61  SCHEDULED,
62  ):
63  # Only allow install when an update has been fully downloaded
64  self._attr_supported_features_attr_supported_features = (
65  UpdateEntityFeature.PROGRESS | UpdateEntityFeature.INSTALL
66  )
67  else:
68  self._attr_supported_features_attr_supported_features = UpdateEntityFeature.PROGRESS
69 
70  # Installed Version
71  self._attr_installed_version_attr_installed_version = self.getget("vehicle_state_car_version")
72  if self._attr_installed_version_attr_installed_version is not None:
73  # Remove build from version
74  self._attr_installed_version_attr_installed_version = self._attr_installed_version_attr_installed_version.split(" ")[0]
75 
76  # Latest Version
77  if self._value_value_value in (
78  AVAILABLE,
79  SCHEDULED,
80  INSTALLING,
81  DOWNLOADING,
82  WIFI_WAIT,
83  ):
84  self._attr_latest_version_attr_latest_version = self.coordinator.data[
85  "vehicle_state_software_update_version"
86  ]
87  else:
88  self._attr_latest_version_attr_latest_version = self._attr_installed_version_attr_installed_version
89 
90  # In Progress
91  if self._value_value_value in (
92  SCHEDULED,
93  INSTALLING,
94  ):
95  self._attr_in_progress_attr_in_progress_attr_in_progress = True
96  if install_perc := self.getget("vehicle_state_software_update_install_perc"):
97  self._attr_update_percentage_attr_update_percentage = cast(int, install_perc)
98  else:
99  self._attr_in_progress_attr_in_progress_attr_in_progress = False
100  self._attr_update_percentage_attr_update_percentage = None
101 
102  async def async_install(
103  self, version: str | None, backup: bool, **kwargs: Any
104  ) -> None:
105  """Install an update."""
106  self.raise_for_scoperaise_for_scope(Scope.ENERGY_CMDS)
107  await self.wake_up_if_asleepwake_up_if_asleep()
108  await handle_vehicle_command(self.apiapiapiapiapiapi.schedule_software_update(offset_sec=60))
109  self._attr_in_progress_attr_in_progress_attr_in_progress = True
110  self._attr_update_percentage_attr_update_percentage = None
111  self.async_write_ha_stateasync_write_ha_state()
Any|None get(self, str key, Any|None default=None)
Definition: entity.py:61
None async_install(self, str|None version, bool backup, **Any kwargs)
Definition: update.py:104
None __init__(self, TeslemetryVehicleData data, list[Scope] scopes)
Definition: update.py:47
bool handle_vehicle_command(Awaitable command)
Definition: helpers.py:50
None async_setup_entry(HomeAssistant hass, TeslemetryConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: update.py:31