Fix for issue:

Tellstick switches status changes aren't realtime #204
This commit is contained in:
Stefan Jonasson 2015-09-20 00:57:04 +02:00
parent a32229b4ce
commit 60d8266ce0
2 changed files with 60 additions and 22 deletions

View File

@ -8,7 +8,7 @@ import logging
from homeassistant.components.light import Light, ATTR_BRIGHTNESS from homeassistant.components.light import Light, ATTR_BRIGHTNESS
from homeassistant.const import ATTR_FRIENDLY_NAME from homeassistant.const import ATTR_FRIENDLY_NAME
import tellcore.constants as tellcore_constants import tellcore.constants as tellcore_constants
from tellcore.library import DirectCallbackDispatcher
REQUIREMENTS = ['tellcore-py==1.0.4'] REQUIREMENTS = ['tellcore-py==1.0.4']
@ -22,13 +22,19 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"Failed to import tellcore") "Failed to import tellcore")
return [] return []
core = telldus.TelldusCore() # pylint: disable=no-member
if telldus.TelldusCore.callback_dispatcher is None:
dispatcher = DirectCallbackDispatcher()
core = telldus.TelldusCore(callback_dispatcher=dispatcher)
else:
core = telldus.TelldusCore()
switches_and_lights = core.devices() switches_and_lights = core.devices()
lights = [] lights = []
for switch in switches_and_lights: for switch in switches_and_lights:
if switch.methods(tellcore_constants.TELLSTICK_DIM): if switch.methods(tellcore_constants.TELLSTICK_DIM):
lights.append(TellstickLight(switch)) lights.append(TellstickLight(switch, core))
add_devices_callback(lights) add_devices_callback(lights)
@ -40,15 +46,23 @@ class TellstickLight(Light):
tellcore_constants.TELLSTICK_UP | tellcore_constants.TELLSTICK_UP |
tellcore_constants.TELLSTICK_DOWN) tellcore_constants.TELLSTICK_DOWN)
def __init__(self, tellstick): def __init__(self, tellstick_device, core):
self.tellstick = tellstick self.tellstick_device = tellstick_device
self.state_attr = {ATTR_FRIENDLY_NAME: tellstick.name} self.state_attr = {ATTR_FRIENDLY_NAME: tellstick_device.name}
self._brightness = 0 self._brightness = 0
self.callback_id = core.register_device_event(self._device_event)
# pylint: disable=unused-argument
def _device_event(self, id_, method, data, cid):
""" Called when a state has changed . """
if self.tellstick_device.id == id_:
self.update_ha_state()
@property @property
def name(self): def name(self):
""" Returns the name of the switch if any. """ """ Returns the name of the switch if any. """
return self.tellstick.name return self.tellstick_device.name
@property @property
def is_on(self): def is_on(self):
@ -62,7 +76,7 @@ class TellstickLight(Light):
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
""" Turns the switch off. """ """ Turns the switch off. """
self.tellstick.turn_off() self.tellstick_device.turn_off()
self._brightness = 0 self._brightness = 0
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
@ -74,11 +88,11 @@ class TellstickLight(Light):
else: else:
self._brightness = brightness self._brightness = brightness
self.tellstick.dim(self._brightness) self.tellstick_device.dim(self._brightness)
def update(self): def update(self):
""" Update state of the light. """ """ Update state of the light. """
last_command = self.tellstick.last_sent_command( last_command = self.tellstick_device.last_sent_command(
self.last_sent_command_mask) self.last_sent_command_mask)
if last_command == tellcore_constants.TELLSTICK_TURNON: if last_command == tellcore_constants.TELLSTICK_TURNON:
@ -88,6 +102,11 @@ class TellstickLight(Light):
elif (last_command == tellcore_constants.TELLSTICK_DIM or elif (last_command == tellcore_constants.TELLSTICK_DIM or
last_command == tellcore_constants.TELLSTICK_UP or last_command == tellcore_constants.TELLSTICK_UP or
last_command == tellcore_constants.TELLSTICK_DOWN): last_command == tellcore_constants.TELLSTICK_DOWN):
last_sent_value = self.tellstick.last_sent_value() last_sent_value = self.tellstick_device.last_sent_value()
if last_sent_value is not None: if last_sent_value is not None:
self._brightness = last_sent_value self._brightness = last_sent_value
@property
def should_poll(self):
""" Tells Home Assistant not to poll this entity. """
return False

View File

@ -11,11 +11,10 @@ signal_repetitions: 3
""" """
import logging import logging
from homeassistant.const import ATTR_FRIENDLY_NAME from homeassistant.const import ATTR_FRIENDLY_NAME
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
import tellcore.constants as tellcore_constants import tellcore.constants as tellcore_constants
from tellcore.library import DirectCallbackDispatcher
SINGAL_REPETITIONS = 1 SINGAL_REPETITIONS = 1
REQUIREMENTS = ['tellcore-py==1.0.4'] REQUIREMENTS = ['tellcore-py==1.0.4']
@ -31,16 +30,23 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"Failed to import tellcore") "Failed to import tellcore")
return return
# pylint: disable=no-member
if telldus.TelldusCore.callback_dispatcher is None:
dispatcher = DirectCallbackDispatcher()
core = telldus.TelldusCore(callback_dispatcher=dispatcher)
else:
core = telldus.TelldusCore()
signal_repetitions = config.get('signal_repetitions', SINGAL_REPETITIONS) signal_repetitions = config.get('signal_repetitions', SINGAL_REPETITIONS)
core = telldus.TelldusCore()
switches_and_lights = core.devices() switches_and_lights = core.devices()
switches = [] switches = []
for switch in switches_and_lights: for switch in switches_and_lights:
if not switch.methods(tellcore_constants.TELLSTICK_DIM): if not switch.methods(tellcore_constants.TELLSTICK_DIM):
switches.append(TellstickSwitchDevice(switch, signal_repetitions)) switches.append(
TellstickSwitchDevice(switch, signal_repetitions, core))
add_devices_callback(switches) add_devices_callback(switches)
@ -50,15 +56,28 @@ class TellstickSwitchDevice(ToggleEntity):
last_sent_command_mask = (tellcore_constants.TELLSTICK_TURNON | last_sent_command_mask = (tellcore_constants.TELLSTICK_TURNON |
tellcore_constants.TELLSTICK_TURNOFF) tellcore_constants.TELLSTICK_TURNOFF)
def __init__(self, tellstick, signal_repetitions): def __init__(self, tellstick_device, signal_repetitions, core):
self.tellstick = tellstick self.tellstick_device = tellstick_device
self.state_attr = {ATTR_FRIENDLY_NAME: tellstick.name} self.state_attr = {ATTR_FRIENDLY_NAME: tellstick_device.name}
self.signal_repetitions = signal_repetitions self.signal_repetitions = signal_repetitions
self.callback_id = core.register_device_event(self._device_event)
# pylint: disable=unused-argument
def _device_event(self, id_, method, data, cid):
""" Called when a state has changed . """
if self.tellstick_device.id == id_:
self.update_ha_state()
@property
def should_poll(self):
""" Tells Home Assistant not to poll this entity. """
return False
@property @property
def name(self): def name(self):
""" Returns the name of the switch if any. """ """ Returns the name of the switch if any. """
return self.tellstick.name return self.tellstick_device.name
@property @property
def state_attributes(self): def state_attributes(self):
@ -68,7 +87,7 @@ class TellstickSwitchDevice(ToggleEntity):
@property @property
def is_on(self): def is_on(self):
""" True if switch is on. """ """ True if switch is on. """
last_command = self.tellstick.last_sent_command( last_command = self.tellstick_device.last_sent_command(
self.last_sent_command_mask) self.last_sent_command_mask)
return last_command == tellcore_constants.TELLSTICK_TURNON return last_command == tellcore_constants.TELLSTICK_TURNON
@ -76,9 +95,9 @@ class TellstickSwitchDevice(ToggleEntity):
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
""" Turns the switch on. """ """ Turns the switch on. """
for _ in range(self.signal_repetitions): for _ in range(self.signal_repetitions):
self.tellstick.turn_on() self.tellstick_device.turn_on()
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
""" Turns the switch off. """ """ Turns the switch off. """
for _ in range(self.signal_repetitions): for _ in range(self.signal_repetitions):
self.tellstick.turn_off() self.tellstick_device.turn_off()