Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Button platform for Tesla Fleet 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 TeslaFleetConfigEntry
16 from .entity import TeslaFleetVehicleEntity
17 from .helpers import handle_vehicle_command
18 from .models import TeslaFleetVehicleData
19 
20 PARALLEL_UPDATES = 0
21 
22 
23 async def do_nothing() -> dict[str, dict[str, bool]]:
24  """Do nothing with a positive result."""
25  return {"response": {"result": True}}
26 
27 
28 @dataclass(frozen=True, kw_only=True)
30  """Describes a TeslaFleet Button entity."""
31 
32  func: Callable[[TeslaFleetButtonEntity], Awaitable[Any]]
33 
34 
35 DESCRIPTIONS: tuple[TeslaFleetButtonEntityDescription, ...] = (
37  key="wake", func=lambda self: do_nothing()
38  ), # Every button runs wakeup, so func does nothing
40  key="flash_lights", func=lambda self: self.api.flash_lights()
41  ),
43  key="honk", func=lambda self: self.api.honk_horn()
44  ),
46  key="enable_keyless_driving", func=lambda self: self.api.remote_start_drive()
47  ),
49  key="boombox", func=lambda self: self.api.remote_boombox(0)
50  ),
52  key="homelink",
53  func=lambda self: self.api.trigger_homelink(
54  lat=self.coordinator.data["drive_state_latitude"],
55  lon=self.coordinator.data["drive_state_longitude"],
56  ),
57  ),
58 )
59 
60 
62  hass: HomeAssistant,
63  entry: TeslaFleetConfigEntry,
64  async_add_entities: AddEntitiesCallback,
65 ) -> None:
66  """Set up the TeslaFleet Button platform from a config entry."""
67 
69  TeslaFleetButtonEntity(vehicle, description)
70  for vehicle in entry.runtime_data.vehicles
71  for description in DESCRIPTIONS
72  if Scope.VEHICLE_CMDS in entry.runtime_data.scopes
73  )
74 
75 
77  """Base class for TeslaFleet buttons."""
78 
79  entity_description: TeslaFleetButtonEntityDescription
80 
81  def __init__(
82  self,
83  data: TeslaFleetVehicleData,
84  description: TeslaFleetButtonEntityDescription,
85  ) -> None:
86  """Initialize the button."""
87  self.entity_descriptionentity_description = description
88  super().__init__(data, description.key)
89 
90  def _async_update_attrs(self) -> None:
91  """Update the attributes of the entity."""
92 
93  async def async_press(self) -> None:
94  """Press the button."""
95  await self.wake_up_if_asleep()
96  await handle_vehicle_command(self.entity_description.func(self))
None __init__(self, TeslaFleetVehicleData data, TeslaFleetButtonEntityDescription description)
Definition: button.py:85
None async_setup_entry(HomeAssistant hass, TeslaFleetConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:65
dict[str, dict[str, bool]] do_nothing()
Definition: button.py:23
bool handle_vehicle_command(Awaitable command)
Definition: helpers.py:50