2019-02-13 20:21:14 +00:00
|
|
|
"""Support for ADS light sources."""
|
2017-12-05 08:44:22 +00:00
|
|
|
import logging
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-12-05 08:44:22 +00:00
|
|
|
import voluptuous as vol
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_BRIGHTNESS, PLATFORM_SCHEMA, SUPPORT_BRIGHTNESS, Light)
|
2017-12-05 08:44:22 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2019-04-01 03:28:43 +00:00
|
|
|
from . import CONF_ADS_VAR, CONF_ADS_VAR_BRIGHTNESS, DATA_ADS, \
|
|
|
|
AdsEntity, STATE_KEY_BRIGHTNESS, STATE_KEY_STATE
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-12-05 08:44:22 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
DEFAULT_NAME = 'ADS Light'
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_ADS_VAR): cv.string,
|
|
|
|
vol.Optional(CONF_ADS_VAR_BRIGHTNESS): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-12-05 08:44:22 +00:00
|
|
|
"""Set up the light platform for ADS."""
|
|
|
|
ads_hub = hass.data.get(DATA_ADS)
|
|
|
|
|
|
|
|
ads_var_enable = config.get(CONF_ADS_VAR)
|
|
|
|
ads_var_brightness = config.get(CONF_ADS_VAR_BRIGHTNESS)
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([AdsLight(ads_hub, ads_var_enable, ads_var_brightness,
|
2019-03-30 17:47:39 +00:00
|
|
|
name)])
|
2017-12-05 08:44:22 +00:00
|
|
|
|
|
|
|
|
2019-04-01 03:28:43 +00:00
|
|
|
class AdsLight(AdsEntity, Light):
|
2017-12-05 08:44:22 +00:00
|
|
|
"""Representation of ADS light."""
|
|
|
|
|
|
|
|
def __init__(self, ads_hub, ads_var_enable, ads_var_brightness, name):
|
|
|
|
"""Initialize AdsLight entity."""
|
2019-04-01 03:28:43 +00:00
|
|
|
super().__init__(ads_hub, name, ads_var_enable)
|
|
|
|
self._state_dict[STATE_KEY_BRIGHTNESS] = None
|
|
|
|
self._ads_var_brightness = ads_var_brightness
|
2017-12-05 08:44:22 +00:00
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-12-05 08:44:22 +00:00
|
|
|
"""Register device notification."""
|
2019-04-01 03:28:43 +00:00
|
|
|
await self.async_initialize_device(self._ads_var,
|
|
|
|
self._ads_hub.PLCTYPE_BOOL)
|
2017-12-05 08:44:22 +00:00
|
|
|
|
2019-04-01 03:28:43 +00:00
|
|
|
if self._ads_var_brightness is not None:
|
|
|
|
await self.async_initialize_device(self._ads_var_brightness,
|
|
|
|
self._ads_hub.PLCTYPE_UINT,
|
|
|
|
STATE_KEY_BRIGHTNESS)
|
2019-02-12 17:42:09 +00:00
|
|
|
|
2017-12-05 08:44:22 +00:00
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of the light (0..255)."""
|
2019-04-01 03:28:43 +00:00
|
|
|
return self._state_dict[STATE_KEY_BRIGHTNESS]
|
2017-12-05 08:44:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2018-12-30 10:39:00 +00:00
|
|
|
support = 0
|
2019-04-01 03:28:43 +00:00
|
|
|
if self._ads_var_brightness is not None:
|
2018-12-30 10:39:00 +00:00
|
|
|
support = SUPPORT_BRIGHTNESS
|
|
|
|
return support
|
2017-12-05 08:44:22 +00:00
|
|
|
|
2019-03-30 17:47:39 +00:00
|
|
|
@property
|
2019-04-01 03:28:43 +00:00
|
|
|
def is_on(self):
|
|
|
|
"""Return True if the entity is on."""
|
|
|
|
return self._state_dict[STATE_KEY_STATE]
|
2019-03-30 17:47:39 +00:00
|
|
|
|
2017-12-05 08:44:22 +00:00
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the light on or set a specific dimmer value."""
|
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
2019-04-01 03:28:43 +00:00
|
|
|
self._ads_hub.write_by_name(self._ads_var, True,
|
2017-12-05 08:44:22 +00:00
|
|
|
self._ads_hub.PLCTYPE_BOOL)
|
|
|
|
|
2019-04-01 03:28:43 +00:00
|
|
|
if self._ads_var_brightness is not None and brightness is not None:
|
|
|
|
self._ads_hub.write_by_name(self._ads_var_brightness, brightness,
|
2017-12-05 08:44:22 +00:00
|
|
|
self._ads_hub.PLCTYPE_UINT)
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the light off."""
|
2019-04-01 03:28:43 +00:00
|
|
|
self._ads_hub.write_by_name(self._ads_var, False,
|
2017-12-05 08:44:22 +00:00
|
|
|
self._ads_hub.PLCTYPE_BOOL)
|