Replace HAOS light entity with select entities, add zero-brightness optimization
- Remove light.py platform (static color control via HA light entity) - Add select.py with CSS Source and Brightness Source dropdowns for LED targets - Coordinator now fetches color-strip-sources and value-sources lists - Add generic update_target() method for partial target updates - Clean up stale device registry entries on integration reload - Skip frame sends when effective brightness is ~0 (suppresses unnecessary UDP/HTTP traffic while LEDs are dark) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,151 +0,0 @@
|
||||
"""Light platform for LED Screen Controller (static color + brightness)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
ATTR_RGB_COLOR,
|
||||
ColorMode,
|
||||
LightEntity,
|
||||
)
|
||||
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, TARGET_TYPE_KEY_COLORS
|
||||
from .coordinator import WLEDScreenControllerCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up LED Screen Controller light entities."""
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator: WLEDScreenControllerCoordinator = data[DATA_COORDINATOR]
|
||||
|
||||
entities = []
|
||||
if coordinator.data and "targets" in coordinator.data:
|
||||
devices = coordinator.data.get("devices") or {}
|
||||
|
||||
for target_id, target_data in coordinator.data["targets"].items():
|
||||
info = target_data["info"]
|
||||
|
||||
# Only LED targets (skip KC targets)
|
||||
if info.get("target_type") == TARGET_TYPE_KEY_COLORS:
|
||||
continue
|
||||
|
||||
device_id = info.get("device_id", "")
|
||||
if not device_id:
|
||||
continue
|
||||
|
||||
device_data = devices.get(device_id)
|
||||
if not device_data:
|
||||
continue
|
||||
|
||||
capabilities = device_data.get("info", {}).get("capabilities") or []
|
||||
|
||||
# Light entity requires BOTH brightness_control AND static_color
|
||||
if "brightness_control" in capabilities and "static_color" in capabilities:
|
||||
entities.append(
|
||||
WLEDScreenControllerLight(
|
||||
coordinator, target_id, device_id, entry.entry_id,
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class WLEDScreenControllerLight(CoordinatorEntity, LightEntity):
|
||||
"""Light entity for an LED device with brightness and static color."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
_attr_color_mode = ColorMode.RGB
|
||||
_attr_supported_color_modes = {ColorMode.RGB}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: WLEDScreenControllerCoordinator,
|
||||
target_id: str,
|
||||
device_id: str,
|
||||
entry_id: str,
|
||||
) -> None:
|
||||
"""Initialize the light entity."""
|
||||
super().__init__(coordinator)
|
||||
self._target_id = target_id
|
||||
self._device_id = device_id
|
||||
self._entry_id = entry_id
|
||||
self._attr_unique_id = f"{target_id}_light"
|
||||
self._attr_translation_key = "light"
|
||||
|
||||
@property
|
||||
def device_info(self) -> dict[str, Any]:
|
||||
"""Return device information."""
|
||||
return {"identifiers": {(DOMAIN, self._target_id)}}
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return True if static_color is set (not null)."""
|
||||
device_data = self._get_device_data()
|
||||
if not device_data:
|
||||
return None
|
||||
static_color = device_data.get("info", {}).get("static_color")
|
||||
return static_color is not None
|
||||
|
||||
@property
|
||||
def brightness(self) -> int | None:
|
||||
"""Return the brightness (0-255)."""
|
||||
device_data = self._get_device_data()
|
||||
if not device_data:
|
||||
return None
|
||||
return device_data.get("brightness")
|
||||
|
||||
@property
|
||||
def rgb_color(self) -> tuple[int, int, int] | None:
|
||||
"""Return the RGB color tuple."""
|
||||
device_data = self._get_device_data()
|
||||
if not device_data:
|
||||
return None
|
||||
static_color = device_data.get("info", {}).get("static_color")
|
||||
if static_color is not None and len(static_color) == 3:
|
||||
return tuple(static_color)
|
||||
return None
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
if not self.coordinator.data:
|
||||
return False
|
||||
targets = self.coordinator.data.get("targets", {})
|
||||
devices = self.coordinator.data.get("devices", {})
|
||||
return self._target_id in targets and self._device_id in devices
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the light on (set static color and/or brightness)."""
|
||||
if ATTR_BRIGHTNESS in kwargs:
|
||||
await self.coordinator.set_brightness(
|
||||
self._device_id, int(kwargs[ATTR_BRIGHTNESS])
|
||||
)
|
||||
|
||||
if ATTR_RGB_COLOR in kwargs:
|
||||
r, g, b = kwargs[ATTR_RGB_COLOR]
|
||||
await self.coordinator.set_color(self._device_id, [r, g, b])
|
||||
elif not self.is_on:
|
||||
# Turning on without specifying color: default to white
|
||||
await self.coordinator.set_color(self._device_id, [255, 255, 255])
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn the light off (clear static color)."""
|
||||
await self.coordinator.set_color(self._device_id, None)
|
||||
|
||||
def _get_device_data(self) -> dict[str, Any] | None:
|
||||
"""Get device data from coordinator."""
|
||||
if not self.coordinator.data:
|
||||
return None
|
||||
return self.coordinator.data.get("devices", {}).get(self._device_id)
|
||||
Reference in New Issue
Block a user