core/homeassistant/components/nanoleaf/light.py

192 lines
6.4 KiB
Python
Raw Normal View History

"""Support for Nanoleaf Lights."""
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
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
)
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
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
2021-09-30 23:25:57 +00:00
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
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-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,
}
)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Import Nanoleaf light platform."""
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]},
)
)
2019-07-31 19:25:30 +00:00
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> 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)])
2020-04-26 16:49:41 +00:00
class NanoleafLight(LightEntity):
"""Representation of a Nanoleaf Light."""
2021-09-28 20:39:54 +00:00
def __init__(self, nanoleaf: Nanoleaf) -> None:
"""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
@property
2021-09-30 21:48:28 +00:00
def brightness(self) -> int:
"""Return the brightness of the light."""
2021-09-28 20:39:54 +00:00
return int(self._nanoleaf.brightness * 2.55)
@property
2021-09-30 21:48:28 +00:00
def color_temp(self) -> int:
"""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
)
@property
2021-09-30 21:48:28 +00:00
def effect(self) -> str | None:
"""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
)
@property
2021-09-30 21:48:28 +00:00
def effect_list(self) -> list[str]:
"""Return the list of supported effects."""
2021-09-28 20:39:54 +00:00
return self._nanoleaf.effects_list
@property
2021-09-30 21:48:28 +00:00
def icon(self) -> str:
"""Return the icon to use in the frontend, if any."""
2021-09-28 20:39:54 +00:00
return "mdi:triangle-outline"
@property
2021-09-30 21:48:28 +00:00
def is_on(self) -> bool:
"""Return true if light is on."""
2021-09-28 20:39:54 +00:00
return self._nanoleaf.is_on
@property
2021-09-30 21:48:28 +00:00
def hs_color(self) -> tuple[int, int]:
"""Return the color in HS."""
2021-09-28 20:39:54 +00:00
return self._nanoleaf.hue, self._nanoleaf.saturation
@property
2021-09-30 21:48:28 +00:00
def supported_features(self) -> int:
"""Flag supported features."""
2021-09-28 20:39:54 +00:00
return (
SUPPORT_BRIGHTNESS
| SUPPORT_COLOR_TEMP
| SUPPORT_EFFECT
| SUPPORT_COLOR
| SUPPORT_TRANSITION
)
2021-09-30 21:48:28 +00:00
async def async_turn_on(self, **kwargs: Any) -> None:
"""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)
transition = kwargs.get(ATTR_TRANSITION)
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))
if color_temp_mired:
2021-09-28 20:39:54 +00:00
await self._nanoleaf.set_color_temperature(
mired_to_kelvin(color_temp_mired)
)
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(
int(brightness / 2.55), transition=int(kwargs[ATTR_TRANSITION])
2019-07-31 19:25: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))
else: # If no transition is occurring, turn on the light
2021-09-28 20:39:54 +00:00
await self._nanoleaf.turn_on()
if brightness:
2021-09-28 20:39:54 +00:00
await self._nanoleaf.set_brightness(int(brightness / 2.55))
if effect:
2021-09-28 20:39:54 +00:00
if effect not in self.effect_list:
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)
2021-09-30 21:48:28 +00:00
async def async_turn_off(self, **kwargs: Any) -> None:
"""Instruct the light to turn off."""
transition = kwargs.get(ATTR_TRANSITION)
2021-09-28 20:39:54 +00:00
await self._nanoleaf.turn_off(transition)
async def async_update(self) -> None:
2018-04-06 16:06:47 +00:00
"""Fetch new state data for this light."""
try:
2021-09-28 20:39:54 +00:00
await self._nanoleaf.get_info()
except Unavailable:
2021-09-28 20:39:54 +00:00
self._attr_available = False
return
2021-09-28 20:39:54 +00:00
self._attr_available = True