Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Button platform for Teslemetry integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from tesla_fleet_api.const import Scope
10 
11 from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.entity_platform import AddEntitiesCallback
14 
15 from . import TeslemetryConfigEntry
16 from .entity import TeslemetryVehicleEntity
17 from .helpers import handle_vehicle_command
18 from .models import TeslemetryVehicleData
19 
20 PARALLEL_UPDATES = 0
21 
22 
23 @dataclass(frozen=True, kw_only=True)
25  """Describes a Teslemetry Button entity."""
26 
27  func: Callable[[TeslemetryButtonEntity], Awaitable[Any]] | None = None
28 
29 
30 DESCRIPTIONS: tuple[TeslemetryButtonEntityDescription, ...] = (
31  TeslemetryButtonEntityDescription(key="wake"), # Every button runs wakeup
33  key="flash_lights", func=lambda self: self.api.flash_lights()
34  ),
36  key="honk", func=lambda self: self.api.honk_horn()
37  ),
39  key="enable_keyless_driving", func=lambda self: self.api.remote_start_drive()
40  ),
42  key="boombox", func=lambda self: self.api.remote_boombox(0)
43  ),
45  key="homelink",
46  func=lambda self: self.api.trigger_homelink(
47  lat=self.coordinator.data["drive_state_latitude"],
48  lon=self.coordinator.data["drive_state_longitude"],
49  ),
50  ),
51 )
52 
53 
55  hass: HomeAssistant,
56  entry: TeslemetryConfigEntry,
57  async_add_entities: AddEntitiesCallback,
58 ) -> None:
59  """Set up the Teslemetry Button platform from a config entry."""
60 
62  TeslemetryButtonEntity(vehicle, description)
63  for vehicle in entry.runtime_data.vehicles
64  for description in DESCRIPTIONS
65  if Scope.VEHICLE_CMDS in entry.runtime_data.scopes
66  )
67 
68 
70  """Base class for Teslemetry buttons."""
71 
72  entity_description: TeslemetryButtonEntityDescription
73 
74  def __init__(
75  self,
76  data: TeslemetryVehicleData,
77  description: TeslemetryButtonEntityDescription,
78  ) -> None:
79  """Initialize the button."""
80  self.entity_descriptionentity_description = description
81  super().__init__(data, description.key)
82 
83  def _async_update_attrs(self) -> None:
84  """Update the attributes of the entity."""
85 
86  async def async_press(self) -> None:
87  """Press the button."""
88  await self.wake_up_if_asleep()
89  if self.entity_description.func:
90  await handle_vehicle_command(self.entity_description.func(self))
None __init__(self, TeslemetryVehicleData data, TeslemetryButtonEntityDescription description)
Definition: button.py:78
bool handle_vehicle_command(Awaitable command)
Definition: helpers.py:50
None async_setup_entry(HomeAssistant hass, TeslemetryConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:58