2015-08-11 12:56:20 +00:00
|
|
|
"""
|
|
|
|
Support for Tellstick lights.
|
2015-10-21 14:10:47 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/light.tellstick/
|
2015-08-11 12:56:20 +00:00
|
|
|
"""
|
2016-04-21 14:57:28 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-03-11 20:54:43 +00:00
|
|
|
from homeassistant.components import tellstick
|
2016-08-16 06:07:07 +00:00
|
|
|
from homeassistant.components.light import (ATTR_BRIGHTNESS,
|
|
|
|
SUPPORT_BRIGHTNESS, Light)
|
2016-03-11 20:54:43 +00:00
|
|
|
from homeassistant.components.tellstick import (DEFAULT_SIGNAL_REPETITIONS,
|
|
|
|
ATTR_DISCOVER_DEVICES,
|
|
|
|
ATTR_DISCOVER_CONFIG)
|
2015-08-09 04:22:34 +00:00
|
|
|
|
2016-04-21 14:57:28 +00:00
|
|
|
PLATFORM_SCHEMA = vol.Schema({vol.Required("platform"): tellstick.DOMAIN})
|
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
SUPPORT_TELLSTICK = SUPPORT_BRIGHTNESS
|
|
|
|
|
2015-09-23 09:46:55 +00:00
|
|
|
|
2015-09-21 06:14:11 +00:00
|
|
|
# pylint: disable=unused-argument
|
2016-03-11 20:54:43 +00:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Setup Tellstick lights."""
|
2016-03-11 20:54:43 +00:00
|
|
|
if (discovery_info is None or
|
|
|
|
discovery_info[ATTR_DISCOVER_DEVICES] is None or
|
|
|
|
tellstick.TELLCORE_REGISTRY is None):
|
|
|
|
return
|
2015-09-23 06:26:40 +00:00
|
|
|
|
2016-03-11 20:54:43 +00:00
|
|
|
signal_repetitions = discovery_info.get(ATTR_DISCOVER_CONFIG,
|
|
|
|
DEFAULT_SIGNAL_REPETITIONS)
|
2015-09-20 12:11:42 +00:00
|
|
|
|
2016-03-11 20:54:43 +00:00
|
|
|
add_devices(TellstickLight(
|
|
|
|
tellstick.TELLCORE_REGISTRY.get_device(switch_id), signal_repetitions)
|
|
|
|
for switch_id in discovery_info[ATTR_DISCOVER_DEVICES])
|
2015-02-04 21:29:30 +00:00
|
|
|
|
|
|
|
|
2016-03-11 20:54:43 +00:00
|
|
|
class TellstickLight(tellstick.TellstickDevice, Light):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Representation of a Tellstick light."""
|
2015-02-04 21:29:30 +00:00
|
|
|
|
2015-11-28 19:39:48 +00:00
|
|
|
def __init__(self, tellstick_device, signal_repetitions):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Initialize the light."""
|
2016-03-11 20:54:43 +00:00
|
|
|
self._brightness = 255
|
|
|
|
tellstick.TellstickDevice.__init__(self,
|
|
|
|
tellstick_device,
|
|
|
|
signal_repetitions)
|
2015-02-04 21:29:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return true if switch is on."""
|
2016-03-11 20:54:43 +00:00
|
|
|
return self._state
|
2015-06-13 23:42:09 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Return the brightness of this light between 0..255."""
|
2015-06-13 23:42:09 +00:00
|
|
|
return self._brightness
|
2015-02-04 21:29:30 +00:00
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_TELLSTICK
|
|
|
|
|
2016-03-11 20:54:43 +00:00
|
|
|
def set_tellstick_state(self, last_command_sent, last_data_sent):
|
|
|
|
"""Update the internal representation of the switch."""
|
|
|
|
from tellcore.constants import TELLSTICK_TURNON, TELLSTICK_DIM
|
|
|
|
if last_command_sent == TELLSTICK_DIM:
|
|
|
|
if last_data_sent is not None:
|
|
|
|
self._brightness = int(last_data_sent)
|
|
|
|
self._state = self._brightness > 0
|
|
|
|
else:
|
|
|
|
self._state = last_command_sent == TELLSTICK_TURNON
|
|
|
|
|
|
|
|
def _send_tellstick_command(self, command, data):
|
|
|
|
"""Handle the turn_on / turn_off commands."""
|
|
|
|
from tellcore.constants import (TELLSTICK_TURNOFF, TELLSTICK_DIM)
|
|
|
|
if command == TELLSTICK_TURNOFF:
|
2015-11-28 19:39:48 +00:00
|
|
|
self.tellstick_device.turn_off()
|
2016-03-11 20:54:43 +00:00
|
|
|
elif command == TELLSTICK_DIM:
|
|
|
|
self.tellstick_device.dim(self._brightness)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError(
|
|
|
|
"Command not implemented: {}".format(command))
|
2015-02-04 21:29:30 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2016-03-07 21:08:21 +00:00
|
|
|
"""Turn the switch on."""
|
2016-03-11 20:54:43 +00:00
|
|
|
from tellcore.constants import TELLSTICK_DIM
|
2015-02-04 21:29:30 +00:00
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
2016-03-11 20:54:43 +00:00
|
|
|
if brightness is not None:
|
2015-06-13 23:42:09 +00:00
|
|
|
self._brightness = brightness
|
2015-02-04 21:29:30 +00:00
|
|
|
|
2016-03-11 20:54:43 +00:00
|
|
|
self.call_tellstick(TELLSTICK_DIM, self._brightness)
|
2015-11-17 08:18:42 +00:00
|
|
|
|
2016-03-11 20:54:43 +00:00
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the switch off."""
|
|
|
|
from tellcore.constants import TELLSTICK_TURNOFF
|
|
|
|
self.call_tellstick(TELLSTICK_TURNOFF)
|