2023-11-03 00:57:48 +00:00
|
|
|
"""Representation of Idasen Desk buttons."""
|
2024-03-08 13:52:48 +00:00
|
|
|
|
2023-11-03 00:57:48 +00:00
|
|
|
from collections.abc import Callable, Coroutine
|
|
|
|
from dataclasses import dataclass
|
|
|
|
import logging
|
|
|
|
from typing import Any, Final
|
|
|
|
|
2024-03-01 11:42:26 +00:00
|
|
|
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
2023-11-03 00:57:48 +00:00
|
|
|
from homeassistant.const import EntityCategory
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2024-12-18 09:20:14 +00:00
|
|
|
from . import IdasenDeskConfigEntry, IdasenDeskCoordinator
|
|
|
|
from .entity import IdasenDeskEntity
|
2023-11-03 00:57:48 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-03-09 15:05:07 +00:00
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
|
|
class IdasenDeskButtonDescription(ButtonEntityDescription):
|
|
|
|
"""Class to describe a IdasenDesk button entity."""
|
2023-11-03 00:57:48 +00:00
|
|
|
|
|
|
|
press_action: Callable[
|
|
|
|
[IdasenDeskCoordinator], Callable[[], Coroutine[Any, Any, Any]]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
BUTTONS: Final = [
|
|
|
|
IdasenDeskButtonDescription(
|
|
|
|
key="connect",
|
2024-03-01 11:42:26 +00:00
|
|
|
translation_key="connect",
|
2023-11-03 00:57:48 +00:00
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
press_action=lambda coordinator: coordinator.async_connect,
|
|
|
|
),
|
|
|
|
IdasenDeskButtonDescription(
|
|
|
|
key="disconnect",
|
2024-03-01 11:42:26 +00:00
|
|
|
translation_key="disconnect",
|
2023-11-03 00:57:48 +00:00
|
|
|
entity_category=EntityCategory.CONFIG,
|
|
|
|
press_action=lambda coordinator: coordinator.async_disconnect,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
2024-12-18 09:20:14 +00:00
|
|
|
entry: IdasenDeskConfigEntry,
|
2023-11-03 00:57:48 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set buttons for device."""
|
2024-12-18 09:20:14 +00:00
|
|
|
coordinator = entry.runtime_data
|
|
|
|
async_add_entities(IdasenDeskButton(coordinator, button) for button in BUTTONS)
|
2023-11-03 00:57:48 +00:00
|
|
|
|
|
|
|
|
2024-12-18 09:20:14 +00:00
|
|
|
class IdasenDeskButton(IdasenDeskEntity, ButtonEntity):
|
2023-11-03 00:57:48 +00:00
|
|
|
"""Defines a IdasenDesk button."""
|
|
|
|
|
|
|
|
entity_description: IdasenDeskButtonDescription
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: IdasenDeskCoordinator,
|
|
|
|
description: IdasenDeskButtonDescription,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the IdasenDesk button entity."""
|
2024-12-18 09:20:14 +00:00
|
|
|
super().__init__(f"{description.key}-{coordinator.address}", coordinator)
|
2023-11-03 00:57:48 +00:00
|
|
|
self.entity_description = description
|
|
|
|
|
|
|
|
async def async_press(self) -> None:
|
|
|
|
"""Triggers the IdasenDesk button press service."""
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Trigger %s for %s",
|
|
|
|
self.entity_description.key,
|
2024-12-18 09:20:14 +00:00
|
|
|
self.coordinator.address,
|
2023-11-03 00:57:48 +00:00
|
|
|
)
|
2024-12-18 09:20:14 +00:00
|
|
|
await self.entity_description.press_action(self.coordinator)()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Connect/disconnect buttons should always be available."""
|
|
|
|
return True
|