2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Tellstick lights using Tellstick Net."""
|
2016-12-12 05:39:37 +00:00
|
|
|
import logging
|
|
|
|
|
2018-12-10 17:44:45 +00:00
|
|
|
from homeassistant.components import light, tellduslive
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.light import ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light
|
2018-12-10 17:44:45 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2016-12-12 05:39:37 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from .entry import TelldusLiveEntity
|
|
|
|
|
2016-12-12 05:39:37 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-12-10 17:44:45 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up tellduslive sensors dynamically."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-12-10 17:44:45 +00:00
|
|
|
async def async_discover_light(device_id):
|
|
|
|
"""Discover and add a discovered sensor."""
|
|
|
|
client = hass.data[tellduslive.DOMAIN]
|
|
|
|
async_add_entities([TelldusLiveLight(client, device_id)])
|
|
|
|
|
|
|
|
async_dispatcher_connect(
|
|
|
|
hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
tellduslive.TELLDUS_DISCOVERY_NEW.format(light.DOMAIN, tellduslive.DOMAIN),
|
2018-12-10 17:44:45 +00:00
|
|
|
async_discover_light,
|
|
|
|
)
|
2016-12-12 05:39:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TelldusLiveLight(TelldusLiveEntity, Light):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Representation of a Tellstick Net light."""
|
2016-12-12 05:39:37 +00:00
|
|
|
|
2018-12-04 09:08:40 +00:00
|
|
|
def __init__(self, client, device_id):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Initialize the Tellstick Net light."""
|
2018-12-04 09:08:40 +00:00
|
|
|
super().__init__(client, device_id)
|
2016-12-16 06:35:53 +00:00
|
|
|
self._last_brightness = self.brightness
|
|
|
|
|
2016-12-12 05:39:37 +00:00
|
|
|
def changed(self):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Define a property of the device that might have changed."""
|
2016-12-12 05:39:37 +00:00
|
|
|
self._last_brightness = self.brightness
|
2019-03-06 12:52:25 +00:00
|
|
|
self._update_callback()
|
2016-12-12 05:39:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.dim_level
|
2016-12-12 05:39:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_BRIGHTNESS
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
2018-08-26 19:25:39 +00:00
|
|
|
return self.device.is_on
|
2016-12-12 05:39:37 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the light on."""
|
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS, self._last_brightness)
|
2019-05-02 10:53:32 +00:00
|
|
|
if brightness == 0:
|
|
|
|
fallback_brightness = 100
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.info(
|
|
|
|
"Setting brightness to %d%%, because it was 0", fallback_brightness
|
|
|
|
)
|
|
|
|
brightness = int(fallback_brightness * 255 / 100)
|
2018-08-26 19:25:39 +00:00
|
|
|
self.device.dim(level=brightness)
|
2016-12-12 05:39:37 +00:00
|
|
|
self.changed()
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the light off."""
|
2018-08-26 19:25:39 +00:00
|
|
|
self.device.turn_off()
|
2016-12-12 05:39:37 +00:00
|
|
|
self.changed()
|