From 22c75f853b1603be0efb204c9e2de0af09a1cf47 Mon Sep 17 00:00:00 2001 From: Danielhiversen Date: Wed, 4 Feb 2015 22:29:30 +0100 Subject: [PATCH 1/4] Added suport for Tellstick light. Assume dimable switch is a light --- homeassistant/components/light/tellstick.py | 94 ++++++++++++++++++++ homeassistant/components/switch/tellstick.py | 38 +++++--- 2 files changed, 119 insertions(+), 13 deletions(-) create mode 100644 homeassistant/components/light/tellstick.py diff --git a/homeassistant/components/light/tellstick.py b/homeassistant/components/light/tellstick.py new file mode 100644 index 0000000000000..c9eba19b992aa --- /dev/null +++ b/homeassistant/components/light/tellstick.py @@ -0,0 +1,94 @@ +""" Support for Tellstick lights. """ +import logging +# pylint: disable=no-name-in-module, import-error +from homeassistant.components.light import ATTR_BRIGHTNESS +from homeassistant.const import ATTR_FRIENDLY_NAME +from homeassistant.helpers import ToggleDevice +import tellcore.constants as tellcore_constants + + +def setup_platform(hass, config, add_devices_callback, discovery_info=None): + """ 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() + switches_and_lights = core.devices() + lights = [] + + for switch in switches_and_lights: + if switch.methods(tellcore_constants.TELLSTICK_DIM): + lights.append(TellstickLight(switch)) + add_devices_callback(lights) + + +class TellstickLight(ToggleDevice): + """ 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) + + def __init__(self, tellstick): + self.tellstick = tellstick + self.state_attr = {ATTR_FRIENDLY_NAME: tellstick.name} + self.brightness = 0 + + @property + def name(self): + """ Returns the name of the switch if any. """ + return self.tellstick.name + + @property + def is_on(self): + """ True if switch is on. """ + return self.brightness > 0 + + def turn_off(self, **kwargs): + """ Turns the switch off. """ + self.tellstick.turn_off() + self.brightness = 0 + + def turn_on(self, **kwargs): + """ Turns the switch on. """ + brightness = kwargs.get(ATTR_BRIGHTNESS) + + if brightness is None: + self.brightness = 255 + else: + self.brightness = brightness + + self.tellstick.dim(self.brightness) + + @property + def state_attributes(self): + """ Returns optional state attributes. """ + attr = { + ATTR_FRIENDLY_NAME: self.name + } + + attr[ATTR_BRIGHTNESS] = int(self.brightness) + + return attr + + def update(self): + """ Update state of the light. """ + last_command = self.tellstick.last_sent_command( + self.last_sent_command_mask) + + if last_command == tellcore_constants.TELLSTICK_TURNON: + self.brightness = 255 + elif last_command == tellcore_constants.TELLSTICK_TURNOFF: + 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.last_sent_value() + if not last_sent_value is None: + self.brightness = last_sent_value diff --git a/homeassistant/components/switch/tellstick.py b/homeassistant/components/switch/tellstick.py index 1f2cd5c66a8a1..5b3ffc85fecc0 100644 --- a/homeassistant/components/switch/tellstick.py +++ b/homeassistant/components/switch/tellstick.py @@ -1,18 +1,24 @@ """ Support for Tellstick switches. """ import logging -from homeassistant.helpers import ToggleDevice -from homeassistant.const import ATTR_FRIENDLY_NAME -try: - import tellcore.constants as tc_constants -except ImportError: - # Don't care for now. Warning will come when get_switches is called. - pass +from homeassistant.const import ATTR_FRIENDLY_NAME +from homeassistant.helpers import ToggleDevice +import tellcore.constants as tellcore_constants def get_devices(hass, config): """ Find and return Tellstick switches. """ + return get_switches() + + +def devices_discovered(hass, config, info): + """ Called when a device is discovered. """ + return get_switches() + + +def get_switches(): + """ Returns the Wink switches. """ try: import tellcore.telldus as telldus except ImportError: @@ -21,15 +27,21 @@ def get_devices(hass, config): return [] core = telldus.TelldusCore() - switches = core.devices() + switches_and_lights = core.devices() + + switches = [] + + for switch in switches_and_lights: + if not switch.methods(tellcore_constants.TELLSTICK_DIM): + switches.append(TellstickSwitchDevice(switch)) - return [TellstickSwitch(switch) for switch in switches] + return switches -class TellstickSwitch(ToggleDevice): +class TellstickSwitchDevice(ToggleDevice): """ represents a Tellstick switch within home assistant. """ - last_sent_command_mask = (tc_constants.TELLSTICK_TURNON | - tc_constants.TELLSTICK_TURNOFF) + last_sent_command_mask = (tellcore_constants.TELLSTICK_TURNON | + tellcore_constants.TELLSTICK_TURNOFF) def __init__(self, tellstick): self.tellstick = tellstick @@ -51,7 +63,7 @@ def is_on(self): last_command = self.tellstick.last_sent_command( self.last_sent_command_mask) - return last_command == tc_constants.TELLSTICK_TURNON + return last_command == tellcore_constants.TELLSTICK_TURNON def turn_on(self, **kwargs): """ Turns the switch on. """ From 0ed8158f6e6865f033ab12d27e9856d57794e2ec Mon Sep 17 00:00:00 2001 From: Danielhiversen Date: Wed, 4 Feb 2015 22:33:04 +0100 Subject: [PATCH 2/4] Added suport for Tellstick light. Assume dimable switch is a light --- homeassistant/components/light/tellstick.py | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/components/light/tellstick.py b/homeassistant/components/light/tellstick.py index c9eba19b992aa..8073f251ae23c 100644 --- a/homeassistant/components/light/tellstick.py +++ b/homeassistant/components/light/tellstick.py @@ -92,3 +92,4 @@ def update(self): last_sent_value = self.tellstick.last_sent_value() if not last_sent_value is None: self.brightness = last_sent_value + self.tellstick.dim(self.brightness) From cb9ad467af1762f9b384f858541130f2aa3b122a Mon Sep 17 00:00:00 2001 From: Danielhiversen Date: Wed, 4 Feb 2015 22:54:05 +0100 Subject: [PATCH 3/4] Added suport for Tellstick light. Assume dimable switch is a light --- homeassistant/components/light/tellstick.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/light/tellstick.py b/homeassistant/components/light/tellstick.py index 8073f251ae23c..c9eba19b992aa 100644 --- a/homeassistant/components/light/tellstick.py +++ b/homeassistant/components/light/tellstick.py @@ -92,4 +92,3 @@ def update(self): last_sent_value = self.tellstick.last_sent_value() if not last_sent_value is None: self.brightness = last_sent_value - self.tellstick.dim(self.brightness) From 76d14157ec6457c2e3c4e730442c9f3860ccaffc Mon Sep 17 00:00:00 2001 From: Danielhiversen Date: Thu, 5 Feb 2015 18:39:03 +0100 Subject: [PATCH 4/4] Fixed Flake8 error --- homeassistant/components/light/tellstick.py | 2 +- homeassistant/components/switch/tellstick.py | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/homeassistant/components/light/tellstick.py b/homeassistant/components/light/tellstick.py index c9eba19b992aa..a204f8e185639 100644 --- a/homeassistant/components/light/tellstick.py +++ b/homeassistant/components/light/tellstick.py @@ -90,5 +90,5 @@ def update(self): last_command == tellcore_constants.TELLSTICK_UP or last_command == tellcore_constants.TELLSTICK_DOWN): last_sent_value = self.tellstick.last_sent_value() - if not last_sent_value is None: + if last_sent_value is not None: self.brightness = last_sent_value diff --git a/homeassistant/components/switch/tellstick.py b/homeassistant/components/switch/tellstick.py index 5b3ffc85fecc0..29c2a2e140338 100644 --- a/homeassistant/components/switch/tellstick.py +++ b/homeassistant/components/switch/tellstick.py @@ -9,16 +9,6 @@ def get_devices(hass, config): """ Find and return Tellstick switches. """ - return get_switches() - - -def devices_discovered(hass, config, info): - """ Called when a device is discovered. """ - return get_switches() - - -def get_switches(): - """ Returns the Wink switches. """ try: import tellcore.telldus as telldus except ImportError: