Rewrite of the tellstick module. It now uses a common base for all shared functionality.

The rewrite addresses a problem with the tellstick hardware dropping
commands when too many simultaneous calls is being made from HA. Also fixes a bug when the dim level was changed externally.
This breaks previous configurations.

The new config for tellstick is

```yaml
tellstick:
  signal_repetitions: X
```
Lights and Switches are detected automatically.
Sensors work like before because they do not share any functionality with the other devices and they also needs a complete other configuration.
This commit is contained in:
Stefan Jonasson committed 2016-03-13 19:28:42 +01:00
1 parent b67964274b
commit eb9ed5ccfe
7 files changed
+302 -170

No files matched your search

+3 -1
View File
@@ -9,7 +9,8 @@ import os
import csv
from homeassistant.components import (
group, discovery, wemo, wink, isy994, zwave, insteon_hub, mysensors)
group, discovery, wemo, wink, isy994,
zwave, insteon_hub, mysensors, tellstick)
from homeassistant.config import load_yaml_config_file
from homeassistant.const import (
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE,
@@ -64,6 +65,7 @@ DISCOVERY_PLATFORMS = {
discovery.SERVICE_HUE: 'hue',
zwave.DISCOVER_LIGHTS: 'zwave',
mysensors.DISCOVER_LIGHTS: 'mysensors',
tellstick.DISCOVER_LIGHTS: 'tellstick',
}
PROP_TO_ATTR = {
+46 -93
View File
@@ -4,127 +4,80 @@ Support for Tellstick lights.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/light.tellstick/
"""
from homeassistant.components import tellstick
from homeassistant.components.light import ATTR_BRIGHTNESS, Light
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
REQUIREMENTS = ['tellcore-py==1.1.2']
SIGNAL_REPETITIONS = 1
from homeassistant.components.tellstick import (DEFAULT_SIGNAL_REPETITIONS,
ATTR_DISCOVER_DEVICES,
ATTR_DISCOVER_CONFIG)
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup Tellstick lights."""
import tellcore.telldus as telldus
from tellcore.library import DirectCallbackDispatcher
import tellcore.constants as tellcore_constants
if (discovery_info is None or
discovery_info[ATTR_DISCOVER_DEVICES] is None or
tellstick.TELLCORE_REGISTRY is None):
return
core = telldus.TelldusCore(callback_dispatcher=DirectCallbackDispatcher())
signal_repetitions = config.get('signal_repetitions', SIGNAL_REPETITIONS)
signal_repetitions = discovery_info.get(ATTR_DISCOVER_CONFIG,
DEFAULT_SIGNAL_REPETITIONS)
switches_and_lights = core.devices()
lights = []
for switch in switches_and_lights:
if switch.methods(tellcore_constants.TELLSTICK_DIM):
lights.append(TellstickLight(switch, signal_repetitions))
def _device_event_callback(id_, method, data, cid):
"""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)
break
callback_id = core.register_device_event(_device_event_callback)
def unload_telldus_lib(event):
"""Un-register the callback bindings."""
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)
add_devices(TellstickLight(
tellstick.TELLCORE_REGISTRY.get_device(switch_id), signal_repetitions)
for switch_id in discovery_info[ATTR_DISCOVER_DEVICES])
class TellstickLight(Light):
class TellstickLight(tellstick.TellstickDevice, Light):
"""Representation of a Tellstick light."""
def __init__(self, tellstick_device, signal_repetitions):
"""Initialize the light."""
import tellcore.constants as tellcore_constants
self.tellstick_device = tellstick_device
self.signal_repetitions = signal_repetitions
self._brightness = 0
self.last_sent_command_mask = (tellcore_constants.TELLSTICK_TURNON |
tellcore_constants.TELLSTICK_TURNOFF |
tellcore_constants.TELLSTICK_DIM |
tellcore_constants.TELLSTICK_UP |
tellcore_constants.TELLSTICK_DOWN)
self.update()
@property
def name(self):
"""Return the name of the switch if any."""
return self.tellstick_device.name
self._brightness = 255
tellstick.TellstickDevice.__init__(self,
tellstick_device,
signal_repetitions)
@property
def is_on(self):
"""Return true if switch is on."""
return self._brightness > 0
return self._state
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
return self._brightness
def turn_off(self, **kwargs):
"""Turn the switch off."""
for _ in range(self.signal_repetitions):
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:
self.tellstick_device.turn_off()
self._brightness = 0
self.update_ha_state()
elif command == TELLSTICK_DIM:
self.tellstick_device.dim(self._brightness)
else:
raise NotImplementedError(
"Command not implemented: {}".format(command))
def turn_on(self, **kwargs):
"""Turn the switch on."""
from tellcore.constants import TELLSTICK_DIM
brightness = kwargs.get(ATTR_BRIGHTNESS)
if brightness is None:
self._brightness = 255
else:
if brightness is not None:
self._brightness = brightness
for _ in range(self.signal_repetitions):
self.tellstick_device.dim(self._brightness)
self.update_ha_state()
self.call_tellstick(TELLSTICK_DIM, self._brightness)
def update(self):
"""Update state of the light."""
import tellcore.constants as tellcore_constants
last_command = self.tellstick_device.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_device.last_sent_value()
if last_sent_value is not None:
self._brightness = last_sent_value
@property
def should_poll(self):
"""No polling needed."""
return False
@property
def assumed_state(self):
"""Tellstick devices are always assumed state."""
return True
def turn_off(self, **kwargs):
"""Turn the switch off."""
from tellcore.constants import TELLSTICK_TURNOFF
self.call_tellstick(TELLSTICK_TURNOFF)