* Add buttons * Add buttons * Fix docstrings * Rebase entry.runtime_data * Revert testing change * Fix tests * format json * Type callable * Remove refresh * Update icons.json * Update strings.json * Update homeassistant/components/teslemetry/button.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * import Awaitable --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
85 lines
2.8 KiB
Python
85 lines
2.8 KiB
Python
"""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.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from .entity import TeslemetryVehicleEntity
|
|
from .models import TeslemetryVehicleData
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class TeslemetryButtonEntityDescription(ButtonEntityDescription):
|
|
"""Describes a Teslemetry Button entity."""
|
|
|
|
func: Callable[[TeslemetryButtonEntity], Awaitable[Any]] | None = None
|
|
|
|
|
|
DESCRIPTIONS: tuple[TeslemetryButtonEntityDescription, ...] = (
|
|
TeslemetryButtonEntityDescription(key="wake"), # Every button runs wakeup
|
|
TeslemetryButtonEntityDescription(
|
|
key="flash_lights", func=lambda self: self.api.flash_lights()
|
|
),
|
|
TeslemetryButtonEntityDescription(
|
|
key="honk", func=lambda self: self.api.honk_horn()
|
|
),
|
|
TeslemetryButtonEntityDescription(
|
|
key="enable_keyless_driving", func=lambda self: self.api.remote_start_drive()
|
|
),
|
|
TeslemetryButtonEntityDescription(
|
|
key="boombox", func=lambda self: self.api.remote_boombox(0)
|
|
),
|
|
TeslemetryButtonEntityDescription(
|
|
key="homelink",
|
|
func=lambda self: self.api.trigger_homelink(
|
|
lat=self.coordinator.data["drive_state_latitude"],
|
|
lon=self.coordinator.data["drive_state_longitude"],
|
|
),
|
|
),
|
|
)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
) -> 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."""
|
|
await self.wake_up_if_asleep()
|
|
if self.entity_description.func:
|
|
await self.handle_command(self.entity_description.func(self))
|