2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Nanoleaf Lights."""
|
2021-08-24 19:09:36 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-10-02 07:30:20 +00:00
|
|
|
import math
|
2021-09-30 21:48:28 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2022-02-06 22:11:52 +00:00
|
|
|
from aionanoleaf import Nanoleaf
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_EFFECT,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
ATTR_TRANSITION,
|
2022-04-23 19:25:03 +00:00
|
|
|
ColorMode,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2022-04-26 12:30:42 +00:00
|
|
|
LightEntityFeature,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-03-31 18:18:17 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-02-06 22:11:52 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-08-24 19:09:36 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-02-06 22:11:52 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.util.color import (
|
2021-10-02 07:30:20 +00:00
|
|
|
color_temperature_kelvin_to_mired as kelvin_to_mired,
|
2019-07-31 19:25:30 +00:00
|
|
|
color_temperature_mired_to_kelvin as mired_to_kelvin,
|
|
|
|
)
|
2021-08-24 19:09:36 +00:00
|
|
|
|
2021-12-02 03:50:17 +00:00
|
|
|
from . import NanoleafEntryData
|
2021-09-28 20:39:54 +00:00
|
|
|
from .const import DOMAIN
|
2021-11-23 00:26:37 +00:00
|
|
|
from .entity import NanoleafEntity
|
2018-04-06 16:06:47 +00:00
|
|
|
|
2021-09-28 20:39:54 +00:00
|
|
|
RESERVED_EFFECTS = ("*Solid*", "*Static*", "*Dynamic*")
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Nanoleaf"
|
2018-04-06 16:06:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2021-08-24 19:09:36 +00:00
|
|
|
async def async_setup_entry(
|
2021-08-25 15:41:48 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2021-08-24 19:09:36 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the Nanoleaf light."""
|
2021-12-02 03:50:17 +00:00
|
|
|
entry_data: NanoleafEntryData = hass.data[DOMAIN][entry.entry_id]
|
2022-02-06 22:11:52 +00:00
|
|
|
async_add_entities([NanoleafLight(entry_data.device, entry_data.coordinator)])
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
|
2021-11-23 00:26:37 +00:00
|
|
|
class NanoleafLight(NanoleafEntity, LightEntity):
|
2019-03-11 15:16:32 +00:00
|
|
|
"""Representation of a Nanoleaf Light."""
|
2018-04-06 13:34:56 +00:00
|
|
|
|
2022-04-23 19:25:03 +00:00
|
|
|
_attr_supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS}
|
2022-04-26 12:30:42 +00:00
|
|
|
_attr_supported_features = LightEntityFeature.EFFECT | LightEntityFeature.TRANSITION
|
2022-04-15 18:09:47 +00:00
|
|
|
|
2022-02-06 22:11:52 +00:00
|
|
|
def __init__(self, nanoleaf: Nanoleaf, coordinator: DataUpdateCoordinator) -> None:
|
2021-11-23 00:26:37 +00:00
|
|
|
"""Initialize the Nanoleaf light."""
|
2022-02-06 22:11:52 +00:00
|
|
|
super().__init__(nanoleaf, coordinator)
|
2021-11-23 00:26:37 +00:00
|
|
|
self._attr_unique_id = nanoleaf.serial_no
|
|
|
|
self._attr_name = nanoleaf.name
|
|
|
|
self._attr_min_mireds = math.ceil(1000000 / nanoleaf.color_temperature_max)
|
|
|
|
self._attr_max_mireds = kelvin_to_mired(nanoleaf.color_temperature_min)
|
2019-03-13 19:54:15 +00:00
|
|
|
|
2018-04-06 13:34:56 +00:00
|
|
|
@property
|
2021-09-30 21:48:28 +00:00
|
|
|
def brightness(self) -> int:
|
2018-04-06 13:34:56 +00:00
|
|
|
"""Return the brightness of the light."""
|
2021-09-28 20:39:54 +00:00
|
|
|
return int(self._nanoleaf.brightness * 2.55)
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
@property
|
2021-09-30 21:48:28 +00:00
|
|
|
def color_temp(self) -> int:
|
2018-04-06 13:34:56 +00:00
|
|
|
"""Return the current color temperature."""
|
2021-10-02 07:30:20 +00:00
|
|
|
return kelvin_to_mired(self._nanoleaf.color_temperature)
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
@property
|
2021-09-30 21:48:28 +00:00
|
|
|
def effect(self) -> str | None:
|
2018-04-06 13:34:56 +00:00
|
|
|
"""Return the current effect."""
|
2021-09-28 20:39:54 +00:00
|
|
|
# The API returns the *Solid* effect if the Nanoleaf is in HS or CT mode.
|
|
|
|
# The effects *Static* and *Dynamic* are not supported by Home Assistant.
|
|
|
|
# These reserved effects are implicitly set and are not in the effect_list.
|
|
|
|
# https://forum.nanoleaf.me/docs/openapi#_byoot0bams8f
|
|
|
|
return (
|
|
|
|
None if self._nanoleaf.effect in RESERVED_EFFECTS else self._nanoleaf.effect
|
|
|
|
)
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
@property
|
2021-09-30 21:48:28 +00:00
|
|
|
def effect_list(self) -> list[str]:
|
2018-04-06 13:34:56 +00:00
|
|
|
"""Return the list of supported effects."""
|
2021-09-28 20:39:54 +00:00
|
|
|
return self._nanoleaf.effects_list
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
@property
|
2021-09-30 21:48:28 +00:00
|
|
|
def icon(self) -> str:
|
2018-04-06 13:34:56 +00:00
|
|
|
"""Return the icon to use in the frontend, if any."""
|
2021-09-28 20:39:54 +00:00
|
|
|
return "mdi:triangle-outline"
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
@property
|
2021-09-30 21:48:28 +00:00
|
|
|
def is_on(self) -> bool:
|
2018-04-06 13:34:56 +00:00
|
|
|
"""Return true if light is on."""
|
2021-09-28 20:39:54 +00:00
|
|
|
return self._nanoleaf.is_on
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
@property
|
2021-09-30 21:48:28 +00:00
|
|
|
def hs_color(self) -> tuple[int, int]:
|
2018-04-06 13:34:56 +00:00
|
|
|
"""Return the color in HS."""
|
2021-09-28 20:39:54 +00:00
|
|
|
return self._nanoleaf.hue, self._nanoleaf.saturation
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
@property
|
2022-04-23 19:25:03 +00:00
|
|
|
def color_mode(self) -> ColorMode | None:
|
2022-04-15 18:09:47 +00:00
|
|
|
"""Return the color mode of the light."""
|
|
|
|
# According to API docs, color mode is "ct", "effect" or "hs"
|
|
|
|
# https://forum.nanoleaf.me/docs/openapi#_4qgqrz96f44d
|
|
|
|
if self._nanoleaf.color_mode == "ct":
|
2022-04-23 19:25:03 +00:00
|
|
|
return ColorMode.COLOR_TEMP
|
2022-04-15 18:09:47 +00:00
|
|
|
# Home Assistant does not have an "effect" color mode, just report hs
|
2022-04-23 19:25:03 +00:00
|
|
|
return ColorMode.HS
|
2018-04-06 13:34:56 +00:00
|
|
|
|
2021-09-30 21:48:28 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2018-04-06 13:34:56 +00:00
|
|
|
"""Instruct the light to turn on."""
|
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
|
|
|
hs_color = kwargs.get(ATTR_HS_COLOR)
|
|
|
|
color_temp_mired = kwargs.get(ATTR_COLOR_TEMP)
|
|
|
|
effect = kwargs.get(ATTR_EFFECT)
|
2019-03-30 11:08:30 +00:00
|
|
|
transition = kwargs.get(ATTR_TRANSITION)
|
2018-04-06 13:34:56 +00:00
|
|
|
|
2022-02-25 16:25:13 +00:00
|
|
|
if effect:
|
|
|
|
if effect not in self.effect_list:
|
|
|
|
raise ValueError(
|
|
|
|
f"Attempting to apply effect not in the effect list: '{effect}'"
|
|
|
|
)
|
|
|
|
await self._nanoleaf.set_effect(effect)
|
|
|
|
elif hs_color:
|
2018-04-06 13:34:56 +00:00
|
|
|
hue, saturation = hs_color
|
2021-09-28 20:39:54 +00:00
|
|
|
await self._nanoleaf.set_hue(int(hue))
|
|
|
|
await self._nanoleaf.set_saturation(int(saturation))
|
2022-02-25 16:25:13 +00:00
|
|
|
elif color_temp_mired:
|
2021-09-28 20:39:54 +00:00
|
|
|
await self._nanoleaf.set_color_temperature(
|
|
|
|
mired_to_kelvin(color_temp_mired)
|
|
|
|
)
|
2019-03-30 11:08:30 +00:00
|
|
|
if transition:
|
|
|
|
if brightness: # tune to the required brightness in n seconds
|
2021-09-28 20:39:54 +00:00
|
|
|
await self._nanoleaf.set_brightness(
|
2021-09-23 20:37:37 +00:00
|
|
|
int(brightness / 2.55), transition=int(kwargs[ATTR_TRANSITION])
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-03-30 11:08:30 +00:00
|
|
|
else: # If brightness is not specified, assume full brightness
|
2021-09-28 20:39:54 +00:00
|
|
|
await self._nanoleaf.set_brightness(100, transition=int(transition))
|
2019-03-30 11:08:30 +00:00
|
|
|
else: # If no transition is occurring, turn on the light
|
2021-09-28 20:39:54 +00:00
|
|
|
await self._nanoleaf.turn_on()
|
2019-03-30 11:08:30 +00:00
|
|
|
if brightness:
|
2021-09-28 20:39:54 +00:00
|
|
|
await self._nanoleaf.set_brightness(int(brightness / 2.55))
|
2018-04-06 13:34:56 +00:00
|
|
|
|
2021-09-30 21:48:28 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2018-04-06 13:34:56 +00:00
|
|
|
"""Instruct the light to turn off."""
|
2021-10-08 16:02:01 +00:00
|
|
|
transition: float | None = kwargs.get(ATTR_TRANSITION)
|
|
|
|
await self._nanoleaf.turn_off(None if transition is None else int(transition))
|