2019-02-14 15:01:46 +00:00
|
|
|
"""Support for the Hive lights."""
|
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
|
|
|
SUPPORT_COLOR_TEMP,
|
|
|
|
Light,
|
|
|
|
)
|
2018-03-18 22:00:29 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import DATA_HIVE, DOMAIN
|
|
|
|
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2018-01-15 22:24:12 +00:00
|
|
|
"""Set up Hive light devices."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
session = hass.data.get(DATA_HIVE)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([HiveDeviceLight(session, discovery_info)])
|
2018-01-15 22:24:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HiveDeviceLight(Light):
|
|
|
|
"""Hive Active Light Device."""
|
|
|
|
|
|
|
|
def __init__(self, hivesession, hivedevice):
|
|
|
|
"""Initialize the Light device."""
|
|
|
|
self.node_id = hivedevice["Hive_NodeID"]
|
|
|
|
self.node_name = hivedevice["Hive_NodeName"]
|
|
|
|
self.device_type = hivedevice["HA_DeviceType"]
|
|
|
|
self.light_device_type = hivedevice["Hive_Light_DeviceType"]
|
|
|
|
self.session = hivesession
|
2018-04-16 19:00:13 +00:00
|
|
|
self.attributes = {}
|
2019-09-03 15:27:14 +00:00
|
|
|
self.data_updatesource = f"{self.device_type}.{self.node_id}"
|
|
|
|
self._unique_id = f"{self.node_id}-{self.device_type}"
|
2018-01-15 22:24:12 +00:00
|
|
|
self.session.entities.append(self)
|
|
|
|
|
2019-01-09 04:15:12 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID of entity."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device information."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {"identifiers": {(DOMAIN, self.unique_id)}, "name": self.name}
|
2019-01-09 04:15:12 +00:00
|
|
|
|
2018-01-15 22:24:12 +00:00
|
|
|
def handle_update(self, updatesource):
|
|
|
|
"""Handle the new update request."""
|
2019-09-03 15:27:14 +00:00
|
|
|
if f"{self.device_type}.{self.node_id}" not in updatesource:
|
2018-01-15 22:24:12 +00:00
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the display name of this light."""
|
|
|
|
return self.node_name
|
|
|
|
|
2018-04-16 19:00:13 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Show Device Attributes."""
|
|
|
|
return self.attributes
|
|
|
|
|
2018-01-15 22:24:12 +00:00
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Brightness of the light (an integer in the range 1-255)."""
|
|
|
|
return self.session.light.get_brightness(self.node_id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def min_mireds(self):
|
|
|
|
"""Return the coldest color_temp that this light supports."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
self.light_device_type == "tuneablelight"
|
|
|
|
or self.light_device_type == "colourtuneablelight"
|
|
|
|
):
|
2018-01-15 22:24:12 +00:00
|
|
|
return self.session.light.get_min_color_temp(self.node_id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_mireds(self):
|
|
|
|
"""Return the warmest color_temp that this light supports."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
self.light_device_type == "tuneablelight"
|
|
|
|
or self.light_device_type == "colourtuneablelight"
|
|
|
|
):
|
2018-01-15 22:24:12 +00:00
|
|
|
return self.session.light.get_max_color_temp(self.node_id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Return the CT color value in mireds."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
self.light_device_type == "tuneablelight"
|
|
|
|
or self.light_device_type == "colourtuneablelight"
|
|
|
|
):
|
2018-01-15 22:24:12 +00:00
|
|
|
return self.session.light.get_color_temp(self.node_id)
|
|
|
|
|
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def hs_color(self) -> tuple:
|
|
|
|
"""Return the hs color value."""
|
2018-01-15 22:24:12 +00:00
|
|
|
if self.light_device_type == "colourtuneablelight":
|
2018-03-18 22:00:29 +00:00
|
|
|
rgb = self.session.light.get_color(self.node_id)
|
|
|
|
return color_util.color_RGB_to_hs(*rgb)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
|
|
|
return self.session.light.get_state(self.node_id)
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Instruct the light to turn on."""
|
|
|
|
new_brightness = None
|
|
|
|
new_color_temp = None
|
|
|
|
new_color = None
|
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
|
|
|
tmp_new_brightness = kwargs.get(ATTR_BRIGHTNESS)
|
2019-07-31 19:25:30 +00:00
|
|
|
percentage_brightness = (tmp_new_brightness / 255) * 100
|
2018-01-15 22:24:12 +00:00
|
|
|
new_brightness = int(round(percentage_brightness / 5.0) * 5.0)
|
|
|
|
if new_brightness == 0:
|
|
|
|
new_brightness = 5
|
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
|
|
|
tmp_new_color_temp = kwargs.get(ATTR_COLOR_TEMP)
|
|
|
|
new_color_temp = round(1000000 / tmp_new_color_temp)
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
|
|
|
get_new_color = kwargs.get(ATTR_HS_COLOR)
|
|
|
|
hue = int(get_new_color[0])
|
|
|
|
saturation = int(get_new_color[1])
|
|
|
|
new_color = (hue, saturation, 100)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.session.light.turn_on(
|
|
|
|
self.node_id,
|
|
|
|
self.light_device_type,
|
|
|
|
new_brightness,
|
|
|
|
new_color_temp,
|
|
|
|
new_color,
|
|
|
|
)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
|
|
|
for entity in self.session.entities:
|
|
|
|
entity.handle_update(self.data_updatesource)
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2018-01-15 22:24:12 +00:00
|
|
|
"""Instruct the light to turn off."""
|
|
|
|
self.session.light.turn_off(self.node_id)
|
|
|
|
for entity in self.session.entities:
|
|
|
|
entity.handle_update(self.data_updatesource)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
supported_features = None
|
|
|
|
if self.light_device_type == "warmwhitelight":
|
|
|
|
supported_features = SUPPORT_BRIGHTNESS
|
|
|
|
elif self.light_device_type == "tuneablelight":
|
2019-07-31 19:25:30 +00:00
|
|
|
supported_features = SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP
|
2018-01-15 22:24:12 +00:00
|
|
|
elif self.light_device_type == "colourtuneablelight":
|
2019-07-31 19:25:30 +00:00
|
|
|
supported_features = SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_COLOR
|
2018-01-15 22:24:12 +00:00
|
|
|
|
|
|
|
return supported_features
|
|
|
|
|
|
|
|
def update(self):
|
2018-01-27 19:58:27 +00:00
|
|
|
"""Update all Node data from Hive."""
|
2018-01-15 22:24:12 +00:00
|
|
|
self.session.core.update_data(self.node_id)
|
2019-07-31 19:25:30 +00:00
|
|
|
self.attributes = self.session.attributes.state_attributes(self.node_id)
|