2024-05-26 07:04:07 +00:00
|
|
|
"""Button platform for Teslemetry integration."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Awaitable, Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from tesla_fleet_api.const import Scope
|
|
|
|
|
|
|
|
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2024-05-27 17:11:55 +00:00
|
|
|
from . import TeslemetryConfigEntry
|
2024-05-26 07:04:07 +00:00
|
|
|
from .entity import TeslemetryVehicleEntity
|
2025-01-21 07:06:18 +00:00
|
|
|
from .helpers import handle_command, handle_vehicle_command
|
2024-05-26 07:04:07 +00:00
|
|
|
from .models import TeslemetryVehicleData
|
|
|
|
|
2024-06-22 06:56:50 +00:00
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
2024-05-26 07:04:07 +00:00
|
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
|
|
class TeslemetryButtonEntityDescription(ButtonEntityDescription):
|
|
|
|
"""Describes a Teslemetry Button entity."""
|
|
|
|
|
2025-01-15 07:32:46 +00:00
|
|
|
func: Callable[[TeslemetryButtonEntity], Awaitable[Any]]
|
2024-05-26 07:04:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
DESCRIPTIONS: tuple[TeslemetryButtonEntityDescription, ...] = (
|
|
|
|
TeslemetryButtonEntityDescription(
|
2025-01-21 07:06:18 +00:00
|
|
|
key="wake", func=lambda self: handle_command(self.api.wake_up())
|
2024-05-26 07:04:07 +00:00
|
|
|
),
|
|
|
|
TeslemetryButtonEntityDescription(
|
2025-01-21 07:06:18 +00:00
|
|
|
key="flash_lights",
|
|
|
|
func=lambda self: handle_vehicle_command(self.api.flash_lights()),
|
2024-05-26 07:04:07 +00:00
|
|
|
),
|
|
|
|
TeslemetryButtonEntityDescription(
|
2025-01-21 07:06:18 +00:00
|
|
|
key="honk", func=lambda self: handle_vehicle_command(self.api.honk_horn())
|
2024-05-26 07:04:07 +00:00
|
|
|
),
|
|
|
|
TeslemetryButtonEntityDescription(
|
2025-01-21 07:06:18 +00:00
|
|
|
key="enable_keyless_driving",
|
|
|
|
func=lambda self: handle_vehicle_command(self.api.remote_start_drive()),
|
|
|
|
),
|
|
|
|
TeslemetryButtonEntityDescription(
|
|
|
|
key="boombox",
|
|
|
|
func=lambda self: handle_vehicle_command(self.api.remote_boombox(0)),
|
2024-05-26 07:04:07 +00:00
|
|
|
),
|
|
|
|
TeslemetryButtonEntityDescription(
|
|
|
|
key="homelink",
|
2025-01-21 07:06:18 +00:00
|
|
|
func=lambda self: handle_vehicle_command(
|
|
|
|
self.api.trigger_homelink(
|
|
|
|
lat=self.coordinator.data["drive_state_latitude"],
|
|
|
|
lon=self.coordinator.data["drive_state_longitude"],
|
|
|
|
)
|
2024-05-26 07:04:07 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
2024-05-27 17:11:55 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: TeslemetryConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2024-05-26 07:04:07 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the Teslemetry Button platform from a config entry."""
|
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
TeslemetryButtonEntity(vehicle, description)
|
|
|
|
for vehicle in entry.runtime_data.vehicles
|
|
|
|
for description in DESCRIPTIONS
|
|
|
|
if Scope.VEHICLE_CMDS in entry.runtime_data.scopes
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TeslemetryButtonEntity(TeslemetryVehicleEntity, ButtonEntity):
|
|
|
|
"""Base class for Teslemetry buttons."""
|
|
|
|
|
|
|
|
entity_description: TeslemetryButtonEntityDescription
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
data: TeslemetryVehicleData,
|
|
|
|
description: TeslemetryButtonEntityDescription,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the button."""
|
|
|
|
self.entity_description = description
|
|
|
|
super().__init__(data, description.key)
|
|
|
|
|
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Update the attributes of the entity."""
|
|
|
|
|
|
|
|
async def async_press(self) -> None:
|
|
|
|
"""Press the button."""
|
2025-01-21 07:06:18 +00:00
|
|
|
await self.entity_description.func(self)
|