2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Nanoleaf Lights."""
|
2018-04-06 13:34:56 +00:00
|
|
|
import logging
|
|
|
|
|
2019-12-03 23:45:46 +00:00
|
|
|
from pynanoleaf 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,
|
|
|
|
Light,
|
|
|
|
)
|
2018-04-06 16:06:47 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TOKEN
|
2018-04-06 13:34:56 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
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,
|
|
|
|
)
|
2018-05-20 18:53:57 +00:00
|
|
|
from homeassistant.util.json import load_json, save_json
|
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
|
|
|
DATA_NANOLEAF = "nanoleaf"
|
2018-05-20 18:53:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_FILE = ".nanoleaf.conf"
|
2018-05-20 18:53:57 +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
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2019-03-11 15:16:32 +00:00
|
|
|
"""Set up the Nanoleaf light."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-03-11 15:16:32 +00:00
|
|
|
if DATA_NANOLEAF not in hass.data:
|
2020-04-04 20:31:56 +00:00
|
|
|
hass.data[DATA_NANOLEAF] = {}
|
2018-05-20 18:53:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
token = ""
|
2018-05-20 18:53:57 +00:00
|
|
|
if discovery_info is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
host = discovery_info["host"]
|
|
|
|
name = discovery_info["hostname"]
|
2018-05-20 18:53:57 +00:00
|
|
|
# if device already exists via config, skip discovery setup
|
2019-03-11 15:16:32 +00:00
|
|
|
if host in hass.data[DATA_NANOLEAF]:
|
2018-05-20 18:53:57 +00:00
|
|
|
return
|
2019-03-11 15:16:32 +00:00
|
|
|
_LOGGER.info("Discovered a new Nanoleaf: %s", discovery_info)
|
2018-05-20 18:53:57 +00:00
|
|
|
conf = load_json(hass.config.path(CONFIG_FILE))
|
2019-07-31 19:25:30 +00:00
|
|
|
if conf.get(host, {}).get("token"):
|
|
|
|
token = conf[host]["token"]
|
2018-05-20 18:53:57 +00:00
|
|
|
else:
|
|
|
|
host = config[CONF_HOST]
|
|
|
|
name = config[CONF_NAME]
|
|
|
|
token = config[CONF_TOKEN]
|
|
|
|
|
2019-03-13 19:54:15 +00:00
|
|
|
nanoleaf_light = Nanoleaf(host)
|
2019-03-11 15:16:32 +00:00
|
|
|
|
2018-05-20 18:53:57 +00:00
|
|
|
if not token:
|
2019-03-11 15:16:32 +00:00
|
|
|
token = nanoleaf_light.request_token()
|
2018-05-20 18:53:57 +00:00
|
|
|
if not token:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Could not generate the auth token, did you press "
|
|
|
|
"and hold the power button on %s"
|
|
|
|
"for 5-7 seconds?",
|
|
|
|
name,
|
|
|
|
)
|
2018-05-20 18:53:57 +00:00
|
|
|
return
|
|
|
|
conf = load_json(hass.config.path(CONFIG_FILE))
|
2019-07-31 19:25:30 +00:00
|
|
|
conf[host] = {"token": token}
|
2018-05-20 18:53:57 +00:00
|
|
|
save_json(hass.config.path(CONFIG_FILE), conf)
|
|
|
|
|
2019-03-11 15:16:32 +00:00
|
|
|
nanoleaf_light.token = token
|
2018-04-06 13:34:56 +00:00
|
|
|
|
2019-03-13 19:54:15 +00:00
|
|
|
try:
|
|
|
|
nanoleaf_light.available
|
|
|
|
except Unavailable:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Could not connect to Nanoleaf Light: %s on %s", name, host)
|
2018-04-06 16:06:47 +00:00
|
|
|
return
|
|
|
|
|
2019-03-11 15:16:32 +00:00
|
|
|
hass.data[DATA_NANOLEAF][host] = nanoleaf_light
|
|
|
|
add_entities([NanoleafLight(nanoleaf_light, name)], True)
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
|
2019-03-11 15:16:32 +00:00
|
|
|
class NanoleafLight(Light):
|
|
|
|
"""Representation of a Nanoleaf Light."""
|
2018-04-06 13:34:56 +00:00
|
|
|
|
2018-05-20 18:53:57 +00:00
|
|
|
def __init__(self, light, name):
|
2019-03-11 15:16:32 +00:00
|
|
|
"""Initialize an Nanoleaf light."""
|
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
|
|
|
|
|
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."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""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
|
|
|
|
self._light.hue = int(hue)
|
|
|
|
self._light.saturation = int(saturation)
|
|
|
|
if color_temp_mired:
|
|
|
|
self._light.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
|
|
|
|
self._light.brightness_transition(
|
2019-07-31 19:25:30 +00:00
|
|
|
int(brightness / 2.55), int(transition)
|
|
|
|
)
|
2019-03-30 11:08:30 +00:00
|
|
|
else: # If brightness is not specified, assume full brightness
|
|
|
|
self._light.brightness_transition(100, int(transition))
|
|
|
|
else: # If no transition is occurring, turn on the light
|
|
|
|
self._light.on = True
|
|
|
|
if brightness:
|
|
|
|
self._light.brightness = int(brightness / 2.55)
|
|
|
|
|
2018-04-06 13:34:56 +00:00
|
|
|
if effect:
|
|
|
|
self._light.effect = effect
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Instruct the light to turn off."""
|
2019-03-30 11:08:30 +00:00
|
|
|
transition = kwargs.get(ATTR_TRANSITION)
|
|
|
|
if transition:
|
|
|
|
self._light.brightness_transition(0, int(transition))
|
|
|
|
else:
|
|
|
|
self._light.on = False
|
2018-04-06 13:34:56 +00:00
|
|
|
|
|
|
|
def update(self):
|
2018-04-06 16:06:47 +00:00
|
|
|
"""Fetch new state data for this light."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-03-13 19:54:15 +00:00
|
|
|
try:
|
|
|
|
self._available = self._light.available
|
|
|
|
self._brightness = self._light.brightness
|
|
|
|
self._color_temp = self._light.color_temperature
|
|
|
|
self._effect = self._light.effect
|
|
|
|
self._effects_list = self._light.effects
|
|
|
|
self._hs_color = self._light.hue, self._light.saturation
|
|
|
|
self._state = self._light.on
|
2019-03-21 03:32:06 +00:00
|
|
|
except Unavailable as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Could not update status for %s (%s)", self.name, err)
|
2019-03-13 19:54:15 +00:00
|
|
|
self._available = False
|