"""Hauptschalter und Helligkeit eines ninstrip. Bewusst eine Entitaet je Geraet, nicht je Segment: device/state ist der Hauptschalter und traegt keine Farbe - bei mehreren Segmenten gaebe es keine eine Farbe, die stimmen wuerde. Die Segmente selbst werden ueber Szenen gestellt; einzelne Segmententitaeten koennen spaeter dazukommen. """ from __future__ import annotations from typing import Any from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from .coordinator import NinstripConfigEntry, NinstripCoordinator from .entity import NinstripEntity async def async_setup_entry( hass: HomeAssistant, entry: NinstripConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: async_add_entities([NinstripLight(entry.runtime_data)]) class NinstripLight(NinstripEntity, LightEntity): """Der Strip als Ganzes.""" _attr_name = None _attr_color_mode = ColorMode.BRIGHTNESS _attr_supported_color_modes = {ColorMode.BRIGHTNESS} def __init__(self, coordinator: NinstripCoordinator) -> None: super().__init__(coordinator, "light") @property def is_on(self) -> bool: return bool(self.coordinator.data.state.get("on")) @property def brightness(self) -> int | None: """Das Geraet rechnet in Prozent, Home Assistant in 0..255.""" value = self.coordinator.data.state.get("brightness") if value is None: return None return round(max(0, min(100, int(value))) * 255 / 100) @property def extra_state_attributes(self) -> dict[str, Any]: state = self.coordinator.data.state return { "segment_count": state.get("segment_count"), "pixel_count": state.get("pixel_count"), "preset": state.get("preset"), "preset_modified": state.get("preset_modified"), } async def async_turn_on(self, **kwargs: Any) -> None: fields: dict[str, Any] = {"on": True} if ATTR_BRIGHTNESS in kwargs: fields["brightness"] = round(int(kwargs[ATTR_BRIGHTNESS]) * 100 / 255) await self.coordinator.async_set_state(**fields) async def async_turn_off(self, **kwargs: Any) -> None: await self.coordinator.async_set_state(on=False)