1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-17 23:53:49 +01:00
Files
core/homeassistant/components/sunricher_dali/light.py

157 lines
5.1 KiB
Python

"""Platform for light integration."""
from __future__ import annotations
import logging
from typing import Any
from PySrDaliGateway import CallbackEventType, Device
from PySrDaliGateway.helper import is_light_device
from PySrDaliGateway.types import LightStatus
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP_KELVIN,
ATTR_HS_COLOR,
ATTR_RGBW_COLOR,
ColorMode,
LightEntity,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DOMAIN, MANUFACTURER
from .entity import DaliDeviceEntity
from .types import DaliCenterConfigEntry
_LOGGER = logging.getLogger(__name__)
PARALLEL_UPDATES = 0
async def async_setup_entry(
hass: HomeAssistant,
entry: DaliCenterConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up Sunricher DALI light entities from config entry."""
runtime_data = entry.runtime_data
devices = runtime_data.devices
async_add_entities(
DaliCenterLight(device)
for device in devices
if is_light_device(device.dev_type)
)
class DaliCenterLight(DaliDeviceEntity, LightEntity):
"""Representation of a Sunricher DALI Light."""
_attr_name = None
_attr_min_color_temp_kelvin = 1000
_attr_max_color_temp_kelvin = 8000
_white_level: int | None = None
def __init__(self, light: Device) -> None:
"""Initialize the light entity."""
super().__init__(light)
self._light = light
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, light.dev_id)},
name=light.name,
manufacturer=MANUFACTURER,
model=light.model,
via_device=(DOMAIN, light.gw_sn),
)
self._determine_features()
def _determine_features(self) -> None:
supported_modes: set[ColorMode] = set()
color_mode = self._light.color_mode
color_mode_map: dict[str, ColorMode] = {
"color_temp": ColorMode.COLOR_TEMP,
"hs": ColorMode.HS,
"rgbw": ColorMode.RGBW,
}
self._attr_color_mode = color_mode_map.get(color_mode, ColorMode.BRIGHTNESS)
supported_modes.add(self._attr_color_mode)
self._attr_supported_color_modes = supported_modes
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light."""
_LOGGER.debug(
"Turning on light %s with kwargs: %s", self._attr_unique_id, kwargs
)
brightness = kwargs.get(ATTR_BRIGHTNESS)
color_temp_kelvin = kwargs.get(ATTR_COLOR_TEMP_KELVIN)
hs_color = kwargs.get(ATTR_HS_COLOR)
rgbw_color = kwargs.get(ATTR_RGBW_COLOR)
self._light.turn_on(
brightness=brightness,
color_temp_kelvin=color_temp_kelvin,
hs_color=hs_color,
rgbw_color=rgbw_color,
)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the light."""
self._light.turn_off()
async def async_added_to_hass(self) -> None:
"""Handle entity addition to Home Assistant."""
await super().async_added_to_hass()
self.async_on_remove(
self._light.register_listener(
CallbackEventType.LIGHT_STATUS, self._handle_device_update
)
)
# read_status() only queues a request on the gateway and relies on the
# current event loop via call_later, so it must run in the loop thread.
self._light.read_status()
@callback
def _handle_device_update(self, status: LightStatus) -> None:
if status.get("is_on") is not None:
self._attr_is_on = status["is_on"]
if status.get("brightness") is not None:
self._attr_brightness = status["brightness"]
if status.get("white_level") is not None:
self._white_level = status["white_level"]
if self._attr_rgbw_color is not None and self._white_level is not None:
self._attr_rgbw_color = (
self._attr_rgbw_color[0],
self._attr_rgbw_color[1],
self._attr_rgbw_color[2],
self._white_level,
)
if (
status.get("color_temp_kelvin") is not None
and self._attr_supported_color_modes
and ColorMode.COLOR_TEMP in self._attr_supported_color_modes
):
self._attr_color_temp_kelvin = status["color_temp_kelvin"]
if (
status.get("hs_color") is not None
and self._attr_supported_color_modes
and ColorMode.HS in self._attr_supported_color_modes
):
self._attr_hs_color = status["hs_color"]
if (
status.get("rgbw_color") is not None
and self._attr_supported_color_modes
and ColorMode.RGBW in self._attr_supported_color_modes
):
self._attr_rgbw_color = status["rgbw_color"]
self.schedule_update_ha_state()