2022-01-06 11:05:25 +00:00
|
|
|
"""Support for BMW connected drive button entities."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-04-21 08:13:09 +00:00
|
|
|
from collections.abc import Callable, Coroutine
|
2022-01-06 11:05:25 +00:00
|
|
|
from dataclasses import dataclass
|
2022-04-21 08:13:09 +00:00
|
|
|
import logging
|
|
|
|
from typing import TYPE_CHECKING
|
2022-01-06 11:05:25 +00:00
|
|
|
|
|
|
|
from bimmer_connected.vehicle import ConnectedDriveVehicle
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-04-21 08:13:09 +00:00
|
|
|
from . import BMWConnectedDriveBaseEntity
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .coordinator import BMWDataUpdateCoordinator
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2022-01-06 11:05:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class BMWButtonEntityDescription(ButtonEntityDescription):
|
|
|
|
"""Class describing BMW button entities."""
|
|
|
|
|
|
|
|
enabled_when_read_only: bool = False
|
2022-04-21 08:13:09 +00:00
|
|
|
remote_function: str | None = None
|
|
|
|
account_function: Callable[[BMWDataUpdateCoordinator], Coroutine] | None = None
|
2022-01-06 11:05:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
BUTTON_TYPES: tuple[BMWButtonEntityDescription, ...] = (
|
|
|
|
BMWButtonEntityDescription(
|
|
|
|
key="light_flash",
|
|
|
|
icon="mdi:car-light-alert",
|
|
|
|
name="Flash Lights",
|
2022-04-21 08:13:09 +00:00
|
|
|
remote_function="trigger_remote_light_flash",
|
2022-01-06 11:05:25 +00:00
|
|
|
),
|
|
|
|
BMWButtonEntityDescription(
|
|
|
|
key="sound_horn",
|
|
|
|
icon="mdi:bullhorn",
|
|
|
|
name="Sound Horn",
|
2022-04-21 08:13:09 +00:00
|
|
|
remote_function="trigger_remote_horn",
|
2022-01-06 11:05:25 +00:00
|
|
|
),
|
|
|
|
BMWButtonEntityDescription(
|
|
|
|
key="activate_air_conditioning",
|
|
|
|
icon="mdi:hvac",
|
|
|
|
name="Activate Air Conditioning",
|
2022-04-21 08:13:09 +00:00
|
|
|
remote_function="trigger_remote_air_conditioning",
|
2022-01-06 11:05:25 +00:00
|
|
|
),
|
|
|
|
BMWButtonEntityDescription(
|
|
|
|
key="deactivate_air_conditioning",
|
|
|
|
icon="mdi:hvac-off",
|
|
|
|
name="Deactivate Air Conditioning",
|
2022-04-21 08:13:09 +00:00
|
|
|
remote_function="trigger_remote_air_conditioning_stop",
|
2022-01-06 11:05:25 +00:00
|
|
|
),
|
|
|
|
BMWButtonEntityDescription(
|
|
|
|
key="find_vehicle",
|
|
|
|
icon="mdi:crosshairs-question",
|
|
|
|
name="Find Vehicle",
|
2022-04-21 08:13:09 +00:00
|
|
|
remote_function="trigger_remote_vehicle_finder",
|
2022-01-06 11:05:25 +00:00
|
|
|
),
|
|
|
|
BMWButtonEntityDescription(
|
|
|
|
key="refresh",
|
|
|
|
icon="mdi:refresh",
|
|
|
|
name="Refresh from cloud",
|
2022-04-21 08:13:09 +00:00
|
|
|
account_function=lambda coordinator: coordinator.async_request_refresh(),
|
2022-01-06 11:05:25 +00:00
|
|
|
enabled_when_read_only=True,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the BMW ConnectedDrive buttons from config entry."""
|
2022-04-21 08:13:09 +00:00
|
|
|
coordinator: BMWDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
|
2022-01-06 11:05:25 +00:00
|
|
|
entities: list[BMWButton] = []
|
|
|
|
|
2022-04-21 08:13:09 +00:00
|
|
|
for vehicle in coordinator.account.vehicles:
|
2022-01-06 11:05:25 +00:00
|
|
|
entities.extend(
|
|
|
|
[
|
2022-04-21 08:13:09 +00:00
|
|
|
BMWButton(coordinator, vehicle, description)
|
2022-01-06 11:05:25 +00:00
|
|
|
for description in BUTTON_TYPES
|
2022-04-21 08:13:09 +00:00
|
|
|
if not coordinator.read_only
|
|
|
|
or (coordinator.read_only and description.enabled_when_read_only)
|
2022-01-06 11:05:25 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
|
|
|
class BMWButton(BMWConnectedDriveBaseEntity, ButtonEntity):
|
|
|
|
"""Representation of a BMW Connected Drive button."""
|
|
|
|
|
|
|
|
entity_description: BMWButtonEntityDescription
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2022-04-21 08:13:09 +00:00
|
|
|
coordinator: BMWDataUpdateCoordinator,
|
2022-01-06 11:05:25 +00:00
|
|
|
vehicle: ConnectedDriveVehicle,
|
|
|
|
description: BMWButtonEntityDescription,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize BMW vehicle sensor."""
|
2022-04-21 08:13:09 +00:00
|
|
|
super().__init__(coordinator, vehicle)
|
2022-01-06 11:05:25 +00:00
|
|
|
self.entity_description = description
|
|
|
|
|
|
|
|
self._attr_name = f"{vehicle.name} {description.name}"
|
|
|
|
self._attr_unique_id = f"{vehicle.vin}-{description.key}"
|
|
|
|
|
2022-04-21 08:13:09 +00:00
|
|
|
async def async_press(self) -> None:
|
|
|
|
"""Press the button."""
|
2022-01-06 11:05:25 +00:00
|
|
|
if self.entity_description.remote_function:
|
2022-04-21 08:13:09 +00:00
|
|
|
await self.hass.async_add_executor_job(
|
|
|
|
getattr(
|
|
|
|
self.vehicle.remote_services,
|
|
|
|
self.entity_description.remote_function,
|
|
|
|
)
|
|
|
|
)
|
2022-01-06 11:05:25 +00:00
|
|
|
elif self.entity_description.account_function:
|
2022-04-21 08:13:09 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"The 'Refresh from cloud' button is deprecated. Use the 'homeassistant.update_entity' "
|
|
|
|
"service with any BMW entity for a full reload. See https://www.home-assistant.io/"
|
|
|
|
"integrations/bmw_connected_drive/#update-the-state--refresh-from-api for details"
|
|
|
|
)
|
|
|
|
await self.entity_description.account_function(self.coordinator)
|
|
|
|
|
|
|
|
# Always update HA states after a button was executed.
|
|
|
|
# BMW remote services that change the vehicle's state update the local object
|
|
|
|
# when executing the service, so only the HA state machine needs further updates.
|
|
|
|
self.coordinator.notify_listeners()
|