core/homeassistant/components/light/tellstick.py

121 lines
4.0 KiB
Python
Raw Normal View History

2015-08-11 12:56:20 +00:00
"""
homeassistant.components.light.tellstick
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Support for Tellstick lights.
"""
import logging
# pylint: disable=no-name-in-module, import-error
2015-06-13 23:42:09 +00:00
from homeassistant.components.light import Light, ATTR_BRIGHTNESS
2015-09-23 07:32:11 +00:00
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
ATTR_FRIENDLY_NAME)
import tellcore.constants as tellcore_constants
from tellcore.library import DirectCallbackDispatcher
2015-09-23 06:29:57 +00:00
REQUIREMENTS = ['tellcore-py==1.1.2']
2015-08-09 04:22:34 +00:00
2015-09-21 06:14:11 +00:00
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
2015-08-11 12:56:20 +00:00
""" Find and return Tellstick lights. """
try:
import tellcore.telldus as telldus
except ImportError:
logging.getLogger(__name__).exception(
"Failed to import tellcore")
return []
core = telldus.TelldusCore(callback_dispatcher=DirectCallbackDispatcher())
switches_and_lights = core.devices()
lights = []
for switch in switches_and_lights:
if switch.methods(tellcore_constants.TELLSTICK_DIM):
2015-09-20 12:17:32 +00:00
lights.append(TellstickLight(switch))
def _device_event_callback(id_, method, data, cid):
2015-09-20 12:17:32 +00:00
""" Called from the TelldusCore library to update one device """
for light_device in lights:
if light_device.tellstick_device.id == id_:
# Execute the update in another thread
light_device.update_ha_state(True)
2015-09-23 09:37:45 +00:00
break
callback_id = core.register_device_event(_device_event_callback)
def unload_telldus_lib(event):
if callback_id is not None:
core.unregister_callback(callback_id)
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, unload_telldus_lib)
add_devices_callback(lights)
2015-06-13 23:42:09 +00:00
class TellstickLight(Light):
2015-08-11 12:56:20 +00:00
""" Represents a Tellstick light. """
last_sent_command_mask = (tellcore_constants.TELLSTICK_TURNON |
tellcore_constants.TELLSTICK_TURNOFF |
tellcore_constants.TELLSTICK_DIM |
tellcore_constants.TELLSTICK_UP |
tellcore_constants.TELLSTICK_DOWN)
2015-09-20 12:17:32 +00:00
def __init__(self, tellstick_device):
self.tellstick_device = tellstick_device
self.state_attr = {ATTR_FRIENDLY_NAME: tellstick_device.name}
2015-06-13 23:42:09 +00:00
self._brightness = 0
@property
def name(self):
""" Returns the name of the switch if any. """
return self.tellstick_device.name
@property
def is_on(self):
""" True if switch is on. """
2015-06-13 23:42:09 +00:00
return self._brightness > 0
@property
def brightness(self):
""" Brightness of this light between 0..255. """
return self._brightness
def turn_off(self, **kwargs):
""" Turns the switch off. """
self.tellstick_device.turn_off()
2015-06-13 23:42:09 +00:00
self._brightness = 0
self.update_ha_state()
def turn_on(self, **kwargs):
""" Turns the switch on. """
brightness = kwargs.get(ATTR_BRIGHTNESS)
if brightness is None:
2015-06-13 23:42:09 +00:00
self._brightness = 255
else:
2015-06-13 23:42:09 +00:00
self._brightness = brightness
self.tellstick_device.dim(self._brightness)
self.update_ha_state()
def update(self):
""" Update state of the light. """
last_command = self.tellstick_device.last_sent_command(
self.last_sent_command_mask)
if last_command == tellcore_constants.TELLSTICK_TURNON:
2015-06-13 23:42:09 +00:00
self._brightness = 255
elif last_command == tellcore_constants.TELLSTICK_TURNOFF:
2015-06-13 23:42:09 +00:00
self._brightness = 0
elif (last_command == tellcore_constants.TELLSTICK_DIM or
last_command == tellcore_constants.TELLSTICK_UP or
last_command == tellcore_constants.TELLSTICK_DOWN):
last_sent_value = self.tellstick_device.last_sent_value()
2015-02-05 17:39:03 +00:00
if last_sent_value is not None:
2015-06-13 23:42:09 +00:00
self._brightness = last_sent_value
@property
def should_poll(self):
""" Tells Home Assistant not to poll this entity. """
return False