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-09-30 21:48:28 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2021-09-28 20:39:54 +00:00
|
|
|
from aionanoleaf import Nanoleaf, Unavailable
|
2018-04-06 13:34:56 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
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,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
|
|
|
SUPPORT_COLOR_TEMP,
|
|
|
|
SUPPORT_EFFECT,
|
|
|
|
SUPPORT_TRANSITION,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-08-24 19:09:36 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2018-04-06 16:06:47 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TOKEN
|
2021-08-24 19:09:36 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-04-06 13:34:56 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-09-30 23:25:57 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2021-08-24 19:09:36 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-04-06 13:34:56 +00:00
|
|
|
from homeassistant.util import color as color_util
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.util.color import (
|
|
|
|
color_temperature_mired_to_kelvin as mired_to_kelvin,
|
|
|
|
)
|
2021-08-24 19:09:36 +00:00
|
|
|
|
2021-09-28 20:39:54 +00:00
|
|
|
from .const import DOMAIN
|
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
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_TOKEN): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
|
2021-08-24 19:09:36 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
2021-08-25 15:41:48 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-08-24 19:09:36 +00:00
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
|
|
|
"""Import Nanoleaf light platform."""
|
2021-08-25 15:41:48 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data={CONF_HOST: config[CONF_HOST], CONF_TOKEN: config[CONF_TOKEN]},
|
|
|
|
)
|
2021-08-24 19:09:36 +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-09-28 20:39:54 +00:00
|
|
|
nanoleaf: Nanoleaf = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
async_add_entities([NanoleafLight(nanoleaf)])
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:49:41 +00:00
|
|
|
class NanoleafLight(LightEntity):
|
2019-03-11 15:16:32 +00:00
|
|
|
"""Representation of a Nanoleaf Light."""
|
2018-04-06 13:34:56 +00:00
|
|
|
|
2021-09-28 20:39:54 +00:00
|
|
|
def __init__(self, nanoleaf: Nanoleaf) -> None:
|
2019-03-11 15:16:32 +00:00
|
|
|
"""Initialize an Nanoleaf light."""
|
2021-09-28 20:39:54 +00:00
|
|
|
self._nanoleaf = nanoleaf
|
|
|
|
self._attr_unique_id = self._nanoleaf.serial_no
|
|
|
|
self._attr_name = self._nanoleaf.name
|
2021-09-30 23:25:57 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, self._nanoleaf.serial_no)},
|
|
|
|
name=self._nanoleaf.name,
|
|
|
|
manufacturer=self._nanoleaf.manufacturer,
|
|
|
|
model=self._nanoleaf.model,
|
|
|
|
sw_version=self._nanoleaf.firmware_version,
|
|
|
|
)
|
2021-09-28 20:39:54 +00:00
|
|
|
self._attr_min_mireds = 154
|
|
|
|
self._attr_max_mireds = 833
|
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-09-28 20:39:54 +00:00
|
|
|
return color_util.color_temperature_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
|
2021-09-30 21:48:28 +00:00
|
|
|
def supported_features(self) -> int:
|
2018-04-06 13:34:56 +00:00
|
|
|
"""Flag supported features."""
|
2021-09-28 20:39:54 +00:00
|
|
|
return (
|
|
|
|
SUPPORT_BRIGHTNESS
|
|
|
|
| SUPPORT_COLOR_TEMP
|
|
|
|
| SUPPORT_EFFECT
|
|
|
|
| SUPPORT_COLOR
|
|
|
|
| SUPPORT_TRANSITION
|
|
|
|
)
|
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
|
|
|
|
|
|
|
if hs_color:
|
|
|
|
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))
|
2018-04-06 13:34:56 +00:00
|
|
|
if 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
|
|
|
if effect:
|
2021-09-28 20:39:54 +00:00
|
|
|
if effect not in self.effect_list:
|
2020-06-09 18:26:37 +00:00
|
|
|
raise ValueError(
|
|
|
|
f"Attempting to apply effect not in the effect list: '{effect}'"
|
|
|
|
)
|
2021-09-28 20:39:54 +00:00
|
|
|
await self._nanoleaf.set_effect(effect)
|
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."""
|
2019-03-30 11:08:30 +00:00
|
|
|
transition = kwargs.get(ATTR_TRANSITION)
|
2021-09-28 20:39:54 +00:00
|
|
|
await self._nanoleaf.turn_off(transition)
|
2018-04-06 13:34:56 +00:00
|
|
|
|
2021-09-23 20:37:37 +00:00
|
|
|
async def async_update(self) -> None:
|
2018-04-06 16:06:47 +00:00
|
|
|
"""Fetch new state data for this light."""
|
2019-03-13 19:54:15 +00:00
|
|
|
try:
|
2021-09-28 20:39:54 +00:00
|
|
|
await self._nanoleaf.get_info()
|
2021-09-23 20:37:37 +00:00
|
|
|
except Unavailable:
|
2021-09-28 20:39:54 +00:00
|
|
|
self._attr_available = False
|
2021-09-23 20:37:37 +00:00
|
|
|
return
|
2021-09-28 20:39:54 +00:00
|
|
|
self._attr_available = True
|