2019-02-14 15:01:46 +00:00
|
|
|
"""Support for Homekit lights."""
|
2018-04-13 17:25:35 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.light import (
|
2019-02-14 15:01:46 +00:00
|
|
|
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_HS_COLOR, SUPPORT_BRIGHTNESS,
|
2018-04-13 17:25:35 +00:00
|
|
|
SUPPORT_COLOR, SUPPORT_COLOR_TEMP, Light)
|
|
|
|
|
2019-03-26 06:49:51 +00:00
|
|
|
from . import KNOWN_DEVICES, HomeKitEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-04-13 17:25:35 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-05-13 06:56:05 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass, config, async_add_entities, discovery_info=None):
|
|
|
|
"""Legacy set up platform."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up Homekit lightbulb."""
|
|
|
|
hkid = config_entry.data['AccessoryPairingID']
|
|
|
|
conn = hass.data[KNOWN_DEVICES][hkid]
|
|
|
|
|
|
|
|
def async_add_service(aid, service):
|
|
|
|
if service['stype'] != 'lightbulb':
|
|
|
|
return False
|
|
|
|
info = {'aid': aid, 'iid': service['iid']}
|
|
|
|
async_add_entities([HomeKitLight(conn, info)], True)
|
|
|
|
return True
|
|
|
|
|
|
|
|
conn.add_listener(async_add_service)
|
2018-04-13 17:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HomeKitLight(HomeKitEntity, Light):
|
|
|
|
"""Representation of a Homekit light."""
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
|
|
|
"""Initialise the light."""
|
|
|
|
super().__init__(*args)
|
2019-03-13 19:53:33 +00:00
|
|
|
self._on = False
|
|
|
|
self._brightness = 0
|
|
|
|
self._color_temperature = 0
|
|
|
|
self._hue = 0
|
|
|
|
self._saturation = 0
|
2018-04-13 17:25:35 +00:00
|
|
|
|
2019-01-28 16:21:20 +00:00
|
|
|
def get_characteristic_types(self):
|
|
|
|
"""Define the homekit characteristics the entity cares about."""
|
|
|
|
# pylint: disable=import-error
|
|
|
|
from homekit.model.characteristics import CharacteristicsTypes
|
|
|
|
return [
|
|
|
|
CharacteristicsTypes.ON,
|
|
|
|
CharacteristicsTypes.BRIGHTNESS,
|
|
|
|
CharacteristicsTypes.COLOR_TEMPERATURE,
|
|
|
|
CharacteristicsTypes.HUE,
|
|
|
|
CharacteristicsTypes.SATURATION,
|
|
|
|
]
|
|
|
|
|
|
|
|
def _setup_brightness(self, char):
|
|
|
|
self._features |= SUPPORT_BRIGHTNESS
|
|
|
|
|
|
|
|
def _setup_color_temperature(self, char):
|
|
|
|
self._features |= SUPPORT_COLOR_TEMP
|
|
|
|
|
|
|
|
def _setup_hue(self, char):
|
|
|
|
self._features |= SUPPORT_COLOR
|
|
|
|
|
|
|
|
def _setup_saturation(self, char):
|
|
|
|
self._features |= SUPPORT_COLOR
|
|
|
|
|
2019-01-28 20:27:26 +00:00
|
|
|
def _update_on(self, value):
|
|
|
|
self._on = value
|
|
|
|
|
|
|
|
def _update_brightness(self, value):
|
|
|
|
self._brightness = value
|
|
|
|
|
|
|
|
def _update_color_temperature(self, value):
|
|
|
|
self._color_temperature = value
|
|
|
|
|
|
|
|
def _update_hue(self, value):
|
|
|
|
self._hue = value
|
2018-04-13 17:25:35 +00:00
|
|
|
|
2019-01-28 20:27:26 +00:00
|
|
|
def _update_saturation(self, value):
|
|
|
|
self._saturation = value
|
2018-04-13 17:25:35 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._on
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
2019-03-13 19:53:33 +00:00
|
|
|
return self._brightness * 255 / 100
|
2018-04-13 17:25:35 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def hs_color(self):
|
|
|
|
"""Return the color property."""
|
2019-03-13 19:53:33 +00:00
|
|
|
return (self._hue, self._saturation)
|
2018-04-13 17:25:35 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Return the color temperature."""
|
2019-03-13 19:53:33 +00:00
|
|
|
return self._color_temperature
|
2018-04-13 17:25:35 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return self._features
|
|
|
|
|
2019-03-11 18:59:41 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2018-04-13 17:25:35 +00:00
|
|
|
"""Turn the specified light on."""
|
|
|
|
hs_color = kwargs.get(ATTR_HS_COLOR)
|
|
|
|
temperature = kwargs.get(ATTR_COLOR_TEMP)
|
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
|
|
|
|
|
|
|
characteristics = []
|
|
|
|
if hs_color is not None:
|
|
|
|
characteristics.append({'aid': self._aid,
|
|
|
|
'iid': self._chars['hue'],
|
|
|
|
'value': hs_color[0]})
|
|
|
|
characteristics.append({'aid': self._aid,
|
|
|
|
'iid': self._chars['saturation'],
|
|
|
|
'value': hs_color[1]})
|
|
|
|
if brightness is not None:
|
|
|
|
characteristics.append({'aid': self._aid,
|
|
|
|
'iid': self._chars['brightness'],
|
|
|
|
'value': int(brightness * 100 / 255)})
|
|
|
|
|
|
|
|
if temperature is not None:
|
|
|
|
characteristics.append({'aid': self._aid,
|
|
|
|
'iid': self._chars['color-temperature'],
|
|
|
|
'value': int(temperature)})
|
|
|
|
characteristics.append({'aid': self._aid,
|
|
|
|
'iid': self._chars['on'],
|
|
|
|
'value': True})
|
2019-03-11 18:59:41 +00:00
|
|
|
await self._accessory.put_characteristics(characteristics)
|
2018-04-13 17:25:35 +00:00
|
|
|
|
2019-03-11 18:59:41 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2018-04-13 17:25:35 +00:00
|
|
|
"""Turn the specified light off."""
|
|
|
|
characteristics = [{'aid': self._aid,
|
|
|
|
'iid': self._chars['on'],
|
|
|
|
'value': False}]
|
2019-03-11 18:59:41 +00:00
|
|
|
await self._accessory.put_characteristics(characteristics)
|