mirror of
https://github.com/home-assistant/core.git
synced 2026-05-29 19:57:40 +01:00
d766aae436
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: frenck <195327+frenck@users.noreply.github.com>
85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
"""Support for Elgato button."""
|
|
|
|
from collections.abc import Awaitable, Callable
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
from elgato import Elgato
|
|
|
|
from homeassistant.components.button import (
|
|
ButtonDeviceClass,
|
|
ButtonEntity,
|
|
ButtonEntityDescription,
|
|
)
|
|
from homeassistant.const import EntityCategory
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
|
|
from .coordinator import ElgatoConfigEntry, ElgatoDataUpdateCoordinator
|
|
from .entity import ElgatoEntity
|
|
from .helpers import elgato_exception_handler
|
|
|
|
PARALLEL_UPDATES = 1
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class ElgatoButtonEntityDescription(ButtonEntityDescription):
|
|
"""Class describing Elgato button entities."""
|
|
|
|
press_fn: Callable[[Elgato], Awaitable[Any]]
|
|
|
|
|
|
BUTTONS = [
|
|
ElgatoButtonEntityDescription(
|
|
key="identify",
|
|
device_class=ButtonDeviceClass.IDENTIFY,
|
|
entity_category=EntityCategory.CONFIG,
|
|
press_fn=lambda client: client.identify(),
|
|
),
|
|
ElgatoButtonEntityDescription(
|
|
key="restart",
|
|
device_class=ButtonDeviceClass.RESTART,
|
|
entity_category=EntityCategory.CONFIG,
|
|
press_fn=lambda client: client.restart(),
|
|
),
|
|
]
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ElgatoConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Set up Elgato button based on a config entry."""
|
|
coordinator = entry.runtime_data
|
|
async_add_entities(
|
|
ElgatoButtonEntity(
|
|
coordinator=coordinator,
|
|
description=description,
|
|
)
|
|
for description in BUTTONS
|
|
)
|
|
|
|
|
|
class ElgatoButtonEntity(ElgatoEntity, ButtonEntity):
|
|
"""Defines an Elgato button."""
|
|
|
|
entity_description: ElgatoButtonEntityDescription
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: ElgatoDataUpdateCoordinator,
|
|
description: ElgatoButtonEntityDescription,
|
|
) -> None:
|
|
"""Initialize the button entity."""
|
|
super().__init__(coordinator=coordinator)
|
|
self.entity_description = description
|
|
self._attr_unique_id = (
|
|
f"{coordinator.data.info.serial_number}_{description.key}"
|
|
)
|
|
|
|
@elgato_exception_handler
|
|
async def async_press(self) -> None:
|
|
"""Trigger button press on the Elgato device."""
|
|
await self.entity_description.press_fn(self.coordinator.client)
|