"""Button platform for LED Screen Controller — scene preset activation.""" from __future__ import annotations import logging from typing import Any from homeassistant.components.button import ButtonEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN, DATA_COORDINATOR from .coordinator import LedGrabCoordinator _LOGGER = logging.getLogger(__name__) async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up scene preset buttons.""" data = hass.data[DOMAIN][entry.entry_id] coordinator: LedGrabCoordinator = data[DATA_COORDINATOR] entities: list[ButtonEntity] = [] if coordinator.data: for preset in coordinator.data.get("scene_presets", []): entities.append( SceneActivateButton(coordinator, preset, entry.entry_id) ) for clock in coordinator.data.get("sync_clocks", []): entities.append( SyncClockResetButton(coordinator, clock["id"], entry.entry_id) ) async_add_entities(entities) class SceneActivateButton(CoordinatorEntity, ButtonEntity): """Button that activates a scene preset.""" _attr_has_entity_name = True def __init__( self, coordinator: LedGrabCoordinator, preset: dict[str, Any], entry_id: str, ) -> None: """Initialize the button.""" super().__init__(coordinator) self._preset_id = preset["id"] self._entry_id = entry_id self._attr_unique_id = f"{entry_id}_scene_{preset['id']}" self._attr_translation_key = "activate_scene" self._attr_translation_placeholders = {"scene_name": preset["name"]} self._attr_icon = "mdi:palette" @property def device_info(self) -> dict[str, Any]: """Return device information — all scene buttons belong to the Scenes device.""" return {"identifiers": {(DOMAIN, f"{self._entry_id}_scenes")}} @property def available(self) -> bool: """Return if entity is available.""" if not self.coordinator.data: return False return self._preset_id in { p["id"] for p in self.coordinator.data.get("scene_presets", []) } async def async_press(self) -> None: """Activate the scene preset.""" await self.coordinator.activate_scene(self._preset_id) class SyncClockResetButton(CoordinatorEntity, ButtonEntity): """Button that resets a sync clock to t=0 (linked animations restart).""" _attr_has_entity_name = True _attr_icon = "mdi:restart" def __init__( self, coordinator: LedGrabCoordinator, clock_id: str, entry_id: str, ) -> None: super().__init__(coordinator) self._clock_id = clock_id self._entry_id = entry_id self._attr_unique_id = f"{clock_id}_reset" self._attr_translation_key = "sync_clock_reset" @property def device_info(self) -> dict[str, Any]: return {"identifiers": {(DOMAIN, self._clock_id)}} @property def available(self) -> bool: if not self.coordinator.data: return False return any( c.get("id") == self._clock_id for c in self.coordinator.data.get("sync_clocks", []) ) async def async_press(self) -> None: await self.coordinator.reset_sync_clock(self._clock_id)