Add button platform to Tesla Fleet (#126410)
* Add button platform * Fix tests * Fix button setup * Make func required * do_nothing
This commit is contained in:
parent
f11cdb4ab4
commit
a1abea4e0f
6 changed files with 473 additions and 0 deletions
|
@ -41,6 +41,7 @@ from .oauth import TeslaSystemImplementation
|
|||
|
||||
PLATFORMS: Final = [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.BUTTON,
|
||||
Platform.CLIMATE,
|
||||
Platform.COVER,
|
||||
Platform.DEVICE_TRACKER,
|
||||
|
|
97
homeassistant/components/tesla_fleet/button.py
Normal file
97
homeassistant/components/tesla_fleet/button.py
Normal file
|
@ -0,0 +1,97 @@
|
|||
"""Button platform for Tesla Fleet 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
|
||||
|
||||
from . import TeslaFleetConfigEntry
|
||||
from .entity import TeslaFleetVehicleEntity
|
||||
from .helpers import handle_vehicle_command
|
||||
from .models import TeslaFleetVehicleData
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
async def do_nothing() -> None:
|
||||
"""Do nothing."""
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class TeslaFleetButtonEntityDescription(ButtonEntityDescription):
|
||||
"""Describes a TeslaFleet Button entity."""
|
||||
|
||||
func: Callable[[TeslaFleetButtonEntity], Awaitable[Any]]
|
||||
|
||||
|
||||
DESCRIPTIONS: tuple[TeslaFleetButtonEntityDescription, ...] = (
|
||||
TeslaFleetButtonEntityDescription(
|
||||
key="wake", func=lambda self: do_nothing()
|
||||
), # Every button runs wakeup, so func does nothing
|
||||
TeslaFleetButtonEntityDescription(
|
||||
key="flash_lights", func=lambda self: self.api.flash_lights()
|
||||
),
|
||||
TeslaFleetButtonEntityDescription(
|
||||
key="honk", func=lambda self: self.api.honk_horn()
|
||||
),
|
||||
TeslaFleetButtonEntityDescription(
|
||||
key="enable_keyless_driving", func=lambda self: self.api.remote_start_drive()
|
||||
),
|
||||
TeslaFleetButtonEntityDescription(
|
||||
key="boombox", func=lambda self: self.api.remote_boombox(0)
|
||||
),
|
||||
TeslaFleetButtonEntityDescription(
|
||||
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: TeslaFleetConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the TeslaFleet Button platform from a config entry."""
|
||||
|
||||
async_add_entities(
|
||||
TeslaFleetButtonEntity(vehicle, description)
|
||||
for vehicle in entry.runtime_data.vehicles
|
||||
for description in DESCRIPTIONS
|
||||
if Scope.VEHICLE_CMDS in entry.runtime_data.scopes
|
||||
and (not vehicle.signing or description.key == "wake")
|
||||
# Wake doesn't need signing
|
||||
)
|
||||
|
||||
|
||||
class TeslaFleetButtonEntity(TeslaFleetVehicleEntity, ButtonEntity):
|
||||
"""Base class for TeslaFleet buttons."""
|
||||
|
||||
entity_description: TeslaFleetButtonEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslaFleetVehicleData,
|
||||
description: TeslaFleetButtonEntityDescription,
|
||||
) -> 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()
|
||||
await handle_vehicle_command(self.entity_description.func(self))
|
|
@ -38,6 +38,26 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"button": {
|
||||
"boombox": {
|
||||
"default": "mdi:volume-high"
|
||||
},
|
||||
"enable_keyless_driving": {
|
||||
"default": "mdi:car-key"
|
||||
},
|
||||
"flash_lights": {
|
||||
"default": "mdi:flashlight"
|
||||
},
|
||||
"homelink": {
|
||||
"default": "mdi:garage"
|
||||
},
|
||||
"honk": {
|
||||
"default": "mdi:bullhorn"
|
||||
},
|
||||
"wake": {
|
||||
"default": "mdi:sleep-off"
|
||||
}
|
||||
},
|
||||
"climate": {
|
||||
"driver_temp": {
|
||||
"state_attributes": {
|
||||
|
|
|
@ -107,6 +107,26 @@
|
|||
"name": "Tire pressure warning rear right"
|
||||
}
|
||||
},
|
||||
"button": {
|
||||
"boombox": {
|
||||
"name": "Play fart"
|
||||
},
|
||||
"enable_keyless_driving": {
|
||||
"name": "Keyless driving"
|
||||
},
|
||||
"flash_lights": {
|
||||
"name": "Flash lights"
|
||||
},
|
||||
"homelink": {
|
||||
"name": "Homelink"
|
||||
},
|
||||
"honk": {
|
||||
"name": "Honk horn"
|
||||
},
|
||||
"wake": {
|
||||
"name": "Wake"
|
||||
}
|
||||
},
|
||||
"climate": {
|
||||
"climate_state_cabin_overheat_protection": {
|
||||
"name": "Cabin overheat protection"
|
||||
|
|
277
tests/components/tesla_fleet/snapshots/test_button.ambr
Normal file
277
tests/components/tesla_fleet/snapshots/test_button.ambr
Normal file
|
@ -0,0 +1,277 @@
|
|||
# serializer version: 1
|
||||
# name: test_button[button.test_flash_lights-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.test_flash_lights',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Flash lights',
|
||||
'platform': 'tesla_fleet',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'flash_lights',
|
||||
'unique_id': 'LRWXF7EK4KC700000-flash_lights',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_button[button.test_flash_lights-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Test Flash lights',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.test_flash_lights',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
# name: test_button[button.test_homelink-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.test_homelink',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Homelink',
|
||||
'platform': 'tesla_fleet',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'homelink',
|
||||
'unique_id': 'LRWXF7EK4KC700000-homelink',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_button[button.test_homelink-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Test Homelink',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.test_homelink',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
# name: test_button[button.test_honk_horn-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.test_honk_horn',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Honk horn',
|
||||
'platform': 'tesla_fleet',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'honk',
|
||||
'unique_id': 'LRWXF7EK4KC700000-honk',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_button[button.test_honk_horn-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Test Honk horn',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.test_honk_horn',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
# name: test_button[button.test_keyless_driving-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.test_keyless_driving',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Keyless driving',
|
||||
'platform': 'tesla_fleet',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'enable_keyless_driving',
|
||||
'unique_id': 'LRWXF7EK4KC700000-enable_keyless_driving',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_button[button.test_keyless_driving-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Test Keyless driving',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.test_keyless_driving',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
# name: test_button[button.test_play_fart-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.test_play_fart',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Play fart',
|
||||
'platform': 'tesla_fleet',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'boombox',
|
||||
'unique_id': 'LRWXF7EK4KC700000-boombox',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_button[button.test_play_fart-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Test Play fart',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.test_play_fart',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
# name: test_button[button.test_wake-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': None,
|
||||
'entity_id': 'button.test_wake',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Wake',
|
||||
'platform': 'tesla_fleet',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'wake',
|
||||
'unique_id': 'LRWXF7EK4KC700000-wake',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_button[button.test_wake-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Test Wake',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.test_wake',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
58
tests/components/tesla_fleet/test_button.py
Normal file
58
tests/components/tesla_fleet/test_button.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
"""Test the Tesla Fleet button platform."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
|
||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import assert_entities, setup_platform
|
||||
from .const import COMMAND_OK
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_button(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
normal_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Tests that the button entities are correct."""
|
||||
|
||||
await setup_platform(hass, normal_config_entry, [Platform.BUTTON])
|
||||
assert_entities(hass, normal_config_entry.entry_id, entity_registry, snapshot)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("name", "func"),
|
||||
[
|
||||
("flash_lights", "flash_lights"),
|
||||
("honk_horn", "honk_horn"),
|
||||
("keyless_driving", "remote_start_drive"),
|
||||
("play_fart", "remote_boombox"),
|
||||
("homelink", "trigger_homelink"),
|
||||
],
|
||||
)
|
||||
async def test_press(
|
||||
hass: HomeAssistant, normal_config_entry: MockConfigEntry, name: str, func: str
|
||||
) -> None:
|
||||
"""Test pressing the API buttons."""
|
||||
await setup_platform(hass, normal_config_entry, [Platform.BUTTON])
|
||||
|
||||
with patch(
|
||||
f"homeassistant.components.tesla_fleet.VehicleSpecific.{func}",
|
||||
return_value=COMMAND_OK,
|
||||
) as command:
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
{ATTR_ENTITY_ID: [f"button.test_{name}"]},
|
||||
blocking=True,
|
||||
)
|
||||
command.assert_called_once()
|
Loading…
Add table
Reference in a new issue