""" Hive Integration - light BK-1.0:2017-06-16_2200 """ import logging, json import voluptuous as vol from datetime import datetime from datetime import timedelta # Import the device class from the component that you want to support from homeassistant.components.light import (Light, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, PLATFORM_SCHEMA) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity from homeassistant.helpers.discovery import load_platform import custom_components.hive as hive from homeassistant.loader import get_component # Home Assistant depends on 3rd party packages for API specific code. DEPENDENCIES = ['hive'] _LOGGER = logging.getLogger(__name__) def setup_platform(hass, config, add_devices, DeviceList, discovery_info=None): """Setup Hive light devices""" HiveComponent = get_component('hive') if len(DeviceList) > 0: for aDevice in DeviceList: if "HA_DeviceType" in aDevice and "Hive_NodeID" in aDevice and "Hive_NodeName" in aDevice and "Hive_Light_DeviceType" in aDevice: if aDevice["HA_DeviceType"] == "Hive_Device_Light": add_devices([Hive_Device_Light(hass, HiveComponent.HiveObjects_Global, aDevice["Hive_NodeID"], aDevice["Hive_NodeName"], aDevice["HA_DeviceType"], aDevice["Hive_Light_DeviceType"])]) class Hive_Device_Light(Light): """Hive Active Light Device""" def __init__(self, hass, HiveComponent_HiveObjects, NodeID, NodeName, DeviceType, NodeDeviceType): """Initialize the Light device.""" self.HiveObjects = HiveComponent_HiveObjects self.NodeID = NodeID self.NodeName = NodeName self.DeviceType = DeviceType self.NodeDeviceType = NodeDeviceType def handle_event(event): tmp_attribute = None self.schedule_update_ha_state() hass.bus.listen('Event_Hive_NewNodeData', handle_event) @property def name(self): """Return the display name of this light.""" return self.NodeName @property def min_mireds(self): """Return the coldest color_temp that this light supports.""" if self.NodeDeviceType == "warmwhitelight": MINCOLORTEMP = None elif self.NodeDeviceType == "tuneablelight": MINCOLORTEMP = self.HiveObjects.Get_Light_Min_Color_Temp(self.NodeID, self.DeviceType, self.NodeName) elif self.NodeDeviceType == "colourtuneablelight": MINCOLORTEMP = self.HiveObjects.Get_Light_Min_Color_Temp(self.NodeID, self.DeviceType, self.NodeName) return MINCOLORTEMP @property def max_mireds(self): """Return the warmest color_temp that this light supports.""" if self.NodeDeviceType == "warmwhitelight": MAXCOLORTEMP = None elif self.NodeDeviceType == "tuneablelight": MAXCOLORTEMP = self.HiveObjects.Get_Light_Max_Color_Temp(self.NodeID, self.DeviceType, self.NodeName) elif self.NodeDeviceType == "colourtuneablelight": MAXCOLORTEMP = self.HiveObjects.Get_Light_Max_Color_Temp(self.NodeID, self.DeviceType, self.NodeName) return MAXCOLORTEMP @property def color_temp(self): """Return the CT color value in mireds.""" if self.NodeDeviceType == "warmwhitelight": COLORTEMP = None elif self.NodeDeviceType == "tuneablelight": COLORTEMP = self.HiveObjects.Get_Light_Color_Temp(self.NodeID, self.DeviceType, self.NodeName) elif self.NodeDeviceType == "colourtuneablelight": COLORTEMP = self.HiveObjects.Get_Light_Color_Temp(self.NodeID, self.DeviceType, self.NodeName) return COLORTEMP @property def brightness(self): """Brightness of the light (an integer in the range 1-255).""" return self.HiveObjects.Get_Light_Brightness(self.NodeID, self.DeviceType, self.NodeName) @property def is_on(self): """Return true if light is on.""" return self.HiveObjects.Get_Light_State(self.NodeID, self.DeviceType, self.NodeName) def turn_on(self, **kwargs): """Instruct the light to turn on.""" NewBrightness = None NewColorTemp = None if ATTR_BRIGHTNESS in kwargs: TmpNewBrightness = kwargs.get(ATTR_BRIGHTNESS) PercentageBrightness = ((TmpNewBrightness / 255) * 100) NewBrightness = int(round(PercentageBrightness / 5.0) * 5.0) if NewBrightness == 0: NewBrightness = 5 if ATTR_COLOR_TEMP in kwargs: TmpNewColorTemp = kwargs.get(ATTR_COLOR_TEMP) NewColorTemp = round(1000000 / TmpNewColorTemp) self.HiveObjects.Set_Light_TurnON(self.NodeID, self.DeviceType, self.NodeDeviceType, self.NodeName, NewBrightness, NewColorTemp) def turn_off(self): """Instruct the light to turn off.""" self.HiveObjects.Set_Light_TurnOFF(self.NodeID, self.DeviceType, self.NodeDeviceType, self.NodeName) @property def supported_features(self): """Flag supported features.""" SUPPORTFEATURES = None if self.NodeDeviceType == "warmwhitelight": SUPPORTFEATURES = (SUPPORT_BRIGHTNESS) elif self.NodeDeviceType == "tuneablelight": SUPPORTFEATURES = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP) elif self.NodeDeviceType == "colourtuneablelight": SUPPORTFEATURES = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_RGB_COLOR) return SUPPORTFEATURES