"""Schalter eines ninstrip-Geraets: Aufwecken bei Aenderung und sACN.""" from __future__ import annotations from typing import Any from homeassistant.components.switch import SwitchEntity from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from .coordinator import NinstripConfigEntry, NinstripCoordinator from .entity import NinstripEntity, NinstripSacnEntity PARALLEL_UPDATES = 0 async def async_setup_entry( hass: HomeAssistant, entry: NinstripConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: async_add_entities( [NinstripWakeSwitch(entry.runtime_data), NinstripSacnSwitch(entry.runtime_data)] ) class NinstripWakeSwitch(NinstripEntity, SwitchEntity): """Schaltet den Strip ein, sobald Farbe, Farbtemperatur oder Helligkeit geaendert werden - statt die Aenderung nur zu merken.""" _attr_entity_category = EntityCategory.CONFIG _attr_translation_key = "wake_on_change" def __init__(self, coordinator: NinstripCoordinator) -> None: super().__init__(coordinator, "wake_on_change") @property def is_on(self) -> bool: return bool(self.coordinator.data.state.get("wake_on_change")) async def async_turn_on(self, **kwargs: Any) -> None: await self.coordinator.async_set_state(wake_on_change=True) async def async_turn_off(self, **kwargs: Any) -> None: await self.coordinator.async_set_state(wake_on_change=False) class NinstripSacnSwitch(NinstripSacnEntity, SwitchEntity): """Ob das Geraet ueberhaupt auf sACN/E1.31 hoert. Abschalten holt dem Strom auch die Kontrolle ab - bei on_timeout "hold" bliebe sonst das letzte Bild stehen. Die Firmware erledigt das selbst. """ _attr_entity_category = EntityCategory.CONFIG _attr_translation_key = "sacn_enabled" def __init__(self, coordinator: NinstripCoordinator) -> None: super().__init__(coordinator, "sacn_enabled") @property def is_on(self) -> bool: return bool(self.sacn.get("enabled")) async def async_turn_on(self, **kwargs: Any) -> None: await self.coordinator.async_set_sacn(enabled=True) async def async_turn_off(self, **kwargs: Any) -> None: await self.coordinator.async_set_sacn(enabled=False)