2021-11-13 13:15:14 +00:00
|
|
|
"""Support for Elgato button."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-02-08 10:09:31 +00:00
|
|
|
from collections.abc import Awaitable, Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from elgato import Elgato, ElgatoError
|
2021-11-13 13:15:14 +00:00
|
|
|
|
2023-02-13 10:54:50 +00:00
|
|
|
from homeassistant.components.button import (
|
|
|
|
ButtonDeviceClass,
|
|
|
|
ButtonEntity,
|
|
|
|
ButtonEntityDescription,
|
|
|
|
)
|
2021-11-13 13:15:14 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-02-09 19:15:37 +00:00
|
|
|
from homeassistant.const import EntityCategory
|
2021-11-13 13:15:14 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-06-15 10:12:07 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2021-11-13 13:15:14 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
2023-02-05 20:54:30 +00:00
|
|
|
from .coordinator import ElgatoDataUpdateCoordinator
|
2022-01-13 12:09:08 +00:00
|
|
|
from .entity import ElgatoEntity
|
2021-11-13 13:15:14 +00:00
|
|
|
|
|
|
|
|
2023-02-08 10:09:31 +00:00
|
|
|
@dataclass
|
|
|
|
class ElgatoButtonEntityDescriptionMixin:
|
|
|
|
"""Mixin values for Elgato entities."""
|
|
|
|
|
|
|
|
press_fn: Callable[[Elgato], Awaitable[Any]]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ElgatoButtonEntityDescription(
|
|
|
|
ButtonEntityDescription, ElgatoButtonEntityDescriptionMixin
|
|
|
|
):
|
|
|
|
"""Class describing Elgato button entities."""
|
|
|
|
|
|
|
|
|
|
|
|
BUTTONS = [
|
|
|
|
ElgatoButtonEntityDescription(
|
|
|
|
key="identify",
|
2023-03-25 22:06:03 +00:00
|
|
|
translation_key="identify",
|
2023-02-08 10:09:31 +00:00
|
|
|
icon="mdi:help",
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
press_fn=lambda client: client.identify(),
|
|
|
|
),
|
2023-02-13 10:54:50 +00:00
|
|
|
ElgatoButtonEntityDescription(
|
|
|
|
key="restart",
|
|
|
|
device_class=ButtonDeviceClass.RESTART,
|
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
press_fn=lambda client: client.restart(),
|
|
|
|
),
|
2023-02-08 10:09:31 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-11-13 13:15:14 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up Elgato button based on a config entry."""
|
2023-02-05 20:54:30 +00:00
|
|
|
coordinator: ElgatoDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2023-02-08 10:09:31 +00:00
|
|
|
async_add_entities(
|
|
|
|
ElgatoButtonEntity(
|
|
|
|
coordinator=coordinator,
|
|
|
|
description=description,
|
|
|
|
)
|
|
|
|
for description in BUTTONS
|
|
|
|
)
|
2021-11-13 13:15:14 +00:00
|
|
|
|
|
|
|
|
2023-02-08 10:09:31 +00:00
|
|
|
class ElgatoButtonEntity(ElgatoEntity, ButtonEntity):
|
|
|
|
"""Defines an Elgato button."""
|
2021-11-13 13:15:14 +00:00
|
|
|
|
2023-02-08 10:09:31 +00:00
|
|
|
entity_description: ElgatoButtonEntityDescription
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: ElgatoDataUpdateCoordinator,
|
|
|
|
description: ElgatoButtonEntityDescription,
|
|
|
|
) -> None:
|
2021-11-13 13:15:14 +00:00
|
|
|
"""Initialize the button entity."""
|
2023-02-05 20:54:30 +00:00
|
|
|
super().__init__(coordinator=coordinator)
|
2023-02-08 10:09:31 +00:00
|
|
|
self.entity_description = description
|
2023-02-05 20:54:30 +00:00
|
|
|
self._attr_unique_id = (
|
2023-02-08 10:09:31 +00:00
|
|
|
f"{coordinator.data.info.serial_number}_{description.key}"
|
2023-02-05 20:54:30 +00:00
|
|
|
)
|
2021-11-13 13:15:14 +00:00
|
|
|
|
|
|
|
async def async_press(self) -> None:
|
2023-02-08 10:09:31 +00:00
|
|
|
"""Trigger button press on the Elgato device."""
|
2021-11-13 13:15:14 +00:00
|
|
|
try:
|
2023-02-08 10:09:31 +00:00
|
|
|
await self.entity_description.press_fn(self.coordinator.client)
|
2022-06-15 10:12:07 +00:00
|
|
|
except ElgatoError as error:
|
|
|
|
raise HomeAssistantError(
|
2023-02-19 19:14:18 +00:00
|
|
|
"An error occurred while communicating with the Elgato Light"
|
2022-06-15 10:12:07 +00:00
|
|
|
) from error
|