2020-02-10 23:16:04 +02:00
|
|
|
"""Support for Dynalite channels as lights."""
|
2020-04-02 21:26:36 +03:00
|
|
|
|
2022-07-31 20:46:13 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2022-11-12 23:59:29 +02:00
|
|
|
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
|
2020-04-02 21:26:36 +03:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 22:36:48 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-02-10 23:16:04 +02:00
|
|
|
|
2020-03-01 16:44:24 -05:00
|
|
|
from .dynalitebase import DynaliteBase, async_setup_entry_base
|
2020-02-10 23:16:04 +02:00
|
|
|
|
|
|
|
|
2020-04-02 21:26:36 +03:00
|
|
|
async def async_setup_entry(
|
2021-05-04 22:36:48 +01:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-04-02 21:26:36 +03:00
|
|
|
) -> None:
|
2020-02-10 23:16:04 +02:00
|
|
|
"""Record the async_add_entities function to add them later when received from Dynalite."""
|
2020-03-01 16:44:24 -05:00
|
|
|
async_setup_entry_base(
|
2020-03-04 22:05:39 -08:00
|
|
|
hass, config_entry, async_add_entities, "light", DynaliteLight
|
2020-03-01 16:44:24 -05:00
|
|
|
)
|
2020-02-10 23:16:04 +02:00
|
|
|
|
|
|
|
|
2020-04-26 18:49:41 +02:00
|
|
|
class DynaliteLight(DynaliteBase, LightEntity):
|
2020-02-10 23:16:04 +02:00
|
|
|
"""Representation of a Dynalite Channel as a Home Assistant Light."""
|
|
|
|
|
2022-04-23 07:50:02 +02:00
|
|
|
_attr_color_mode = ColorMode.BRIGHTNESS
|
|
|
|
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
2022-04-03 13:58:28 +02:00
|
|
|
|
2020-02-10 23:16:04 +02:00
|
|
|
@property
|
2020-04-02 21:26:36 +03:00
|
|
|
def brightness(self) -> int:
|
2020-02-10 23:16:04 +02:00
|
|
|
"""Return the brightness of this light between 0..255."""
|
|
|
|
return self._device.brightness
|
|
|
|
|
|
|
|
@property
|
2020-04-02 21:26:36 +03:00
|
|
|
def is_on(self) -> bool:
|
2020-02-10 23:16:04 +02:00
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._device.is_on
|
|
|
|
|
2022-07-31 20:46:13 +02:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2020-02-10 23:16:04 +02:00
|
|
|
"""Turn the light on."""
|
|
|
|
await self._device.async_turn_on(**kwargs)
|
|
|
|
|
2022-07-31 20:46:13 +02:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-02-10 23:16:04 +02:00
|
|
|
"""Turn the light off."""
|
|
|
|
await self._device.async_turn_off(**kwargs)
|
2022-11-12 23:59:29 +02:00
|
|
|
|
|
|
|
def initialize_state(self, state):
|
|
|
|
"""Initialize the state from cache."""
|
|
|
|
target_level = state.attributes.get(ATTR_BRIGHTNESS)
|
|
|
|
if target_level is not None:
|
|
|
|
self._device.init_level(target_level)
|