2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Nanoleaf Lights."""
|
2021-08-24 19:09:36 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-04-06 13:34:56 +00:00
|
|
|
import logging
|
|
|
|
|
2021-09-23 20:37:37 +00:00
|
|
|
from aiohttp import ServerDisconnectedError
|
|
|
|
from aionanoleaf import 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-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
|
|
|
|
|
|
|
from .const import DEVICE, DOMAIN, NAME, SERIAL_NO
|
2018-04-06 13:34:56 +00:00
|
|
|
|
2018-04-06 16:06:47 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
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
|
|
|
ICON = "mdi:triangle-outline"
|
2018-04-06 16:06:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_NANOLEAF = (
|
|
|
|
SUPPORT_BRIGHTNESS
|
|
|
|
| SUPPORT_COLOR_TEMP
|
|
|
|
| SUPPORT_EFFECT
|
|
|
|
| SUPPORT_COLOR
|
|
|
|
| SUPPORT_TRANSITION
|
|
|
|
)
|
2018-04-06 13:34:56 +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."""
|
|
|
|
data = hass.data[DOMAIN][entry.entry_id]
|
2021-08-25 15:41:48 +00:00
|
|
|
async_add_entities([NanoleafLight(data[DEVICE], data[NAME], data[SERIAL_NO])], True)
|
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-08-24 19:09:36 +00:00
|
|
|
def __init__(self, light, name, unique_id):
|
2019-03-11 15:16:32 +00:00
|
|
|
"""Initialize an Nanoleaf light."""
|
2021-08-24 19:09:36 +00:00
|
|
|
self._unique_id = unique_id
|
2019-03-13 19:54:15 +00:00
|
|
|
self._available = True
|
2018-04-06 13:34:56 +00:00
|
|
|
self._brightness = None
|
|
|
|
self._color_temp = None
|
|
|
|
self._effect = None
|
|
|
|
self._effects_list = None
|
|
|
|
self._light = light
|
2018-05-20 18:53:57 +00:00
|
|
|
self._name = name
|
2018-04-06 13:34:56 +00:00
|
|
|
self._hs_color = None
|
|
|
|
self._state = None
|
|
|
|
|
2019-03-13 19:54:15 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return availability."""
|
|
|
|
return self._available
|
|
|
|
|
2018-04-06 13:34:56 +00:00
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of the light."""
|
|
|
|
if self._brightness is not None:
|
|
|
|
return int(self._brightness * 2.55)
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Return the current color temperature."""
|
|
|
|
if self._color_temp is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
return color_util.color_temperature_kelvin_to_mired(self._color_temp)
|
2018-04-06 13:34:56 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def effect(self):
|
|
|
|
"""Return the current effect."""
|
|
|
|
return self._effect
|
|
|
|
|
|
|
|
@property
|
|
|
|
def effect_list(self):
|
|
|
|
"""Return the list of supported effects."""
|
|
|
|
return self._effects_list
|
|
|
|
|
2018-05-21 15:02:50 +00:00
|
|
|
@property
|
|
|
|
def min_mireds(self):
|
|
|
|
"""Return the coldest color_temp that this light supports."""
|
|
|
|
return 154
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_mireds(self):
|
|
|
|
"""Return the warmest color_temp that this light supports."""
|
|
|
|
return 833
|
|
|
|
|
2021-03-23 08:18:48 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2018-04-06 13:34:56 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the display name of this light."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
2018-04-06 16:06:47 +00:00
|
|
|
return ICON
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
2021-09-23 20:37:37 +00:00
|
|
|
return self._light.is_on
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def hs_color(self):
|
|
|
|
"""Return the color in HS."""
|
|
|
|
return self._hs_color
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2019-03-11 15:16:32 +00:00
|
|
|
return SUPPORT_NANOLEAF
|
2018-04-06 13:34:56 +00:00
|
|
|
|
2021-09-23 20:37:37 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
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-23 20:37:37 +00:00
|
|
|
await self._light.set_hue(int(hue))
|
|
|
|
await self._light.set_saturation(int(saturation))
|
2018-04-06 13:34:56 +00:00
|
|
|
if color_temp_mired:
|
2021-09-23 20:37:37 +00:00
|
|
|
await self._light.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-23 20:37:37 +00:00
|
|
|
await self._light.set_brightness(
|
|
|
|
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-23 20:37:37 +00:00
|
|
|
await self._light.set_brightness(
|
|
|
|
100, transition=int(kwargs[ATTR_TRANSITION])
|
|
|
|
)
|
2019-03-30 11:08:30 +00:00
|
|
|
else: # If no transition is occurring, turn on the light
|
2021-09-23 20:37:37 +00:00
|
|
|
await self._light.turn_on()
|
2019-03-30 11:08:30 +00:00
|
|
|
if brightness:
|
2021-09-23 20:37:37 +00:00
|
|
|
await self._light.set_brightness(int(brightness / 2.55))
|
2018-04-06 13:34:56 +00:00
|
|
|
if effect:
|
2020-06-09 18:26:37 +00:00
|
|
|
if effect not in self._effects_list:
|
|
|
|
raise ValueError(
|
|
|
|
f"Attempting to apply effect not in the effect list: '{effect}'"
|
|
|
|
)
|
2021-09-23 20:37:37 +00:00
|
|
|
await self._light.set_effect(effect)
|
2018-04-06 13:34:56 +00:00
|
|
|
|
2021-09-23 20:37:37 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
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)
|
|
|
|
if transition:
|
2021-09-23 20:37:37 +00:00
|
|
|
await self._light.set_brightness(0, transition=int(transition))
|
2019-03-30 11:08:30 +00:00
|
|
|
else:
|
2021-09-23 20:37:37 +00:00
|
|
|
await self._light.turn_off()
|
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-23 20:37:37 +00:00
|
|
|
await self._light.get_info()
|
|
|
|
except ServerDisconnectedError:
|
|
|
|
# Retry the request once if the device disconnected
|
|
|
|
await self._light.get_info()
|
|
|
|
except Unavailable:
|
2019-03-13 19:54:15 +00:00
|
|
|
self._available = False
|
2021-09-23 20:37:37 +00:00
|
|
|
return
|
|
|
|
self._available = True
|
|
|
|
self._brightness = self._light.brightness
|
|
|
|
self._effects_list = self._light.effects_list
|
|
|
|
# Nanoleaf api returns non-existent effect named "*Solid*" when light set to solid color.
|
|
|
|
# This causes various issues with scening (see https://github.com/home-assistant/core/issues/36359).
|
|
|
|
# Until fixed at the library level, we should ensure the effect exists before saving to light properties
|
|
|
|
self._effect = (
|
|
|
|
self._light.effect if self._light.effect in self._effects_list else None
|
|
|
|
)
|
|
|
|
if self._effect is None:
|
|
|
|
self._color_temp = self._light.color_temperature
|
|
|
|
self._hs_color = self._light.hue, self._light.saturation
|
|
|
|
else:
|
|
|
|
self._color_temp = None
|
|
|
|
self._hs_color = None
|
|
|
|
self._state = self._light.is_on
|