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:
1 parent
b67964274b
commit
eb9ed5ccfe
7 files changed
+302
-170
No files matched your search
@@ -16,7 +16,8 @@ from homeassistant.const import (
|
||||
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE,
|
||||
ATTR_ENTITY_ID)
|
||||
from homeassistant.components import (
|
||||
group, wemo, wink, isy994, verisure, zwave, tellduslive, mysensors)
|
||||
group, wemo, wink, isy994, verisure,
|
||||
zwave, tellduslive, tellstick, mysensors)
|
||||
|
||||
DOMAIN = 'switch'
|
||||
SCAN_INTERVAL = 30
|
||||
@@ -40,6 +41,7 @@ DISCOVERY_PLATFORMS = {
|
||||
zwave.DISCOVER_SWITCHES: 'zwave',
|
||||
tellduslive.DISCOVER_SWITCHES: 'tellduslive',
|
||||
mysensors.DISCOVER_SWITCHES: 'mysensors',
|
||||
tellstick.DISCOVER_SWITCHES: 'tellstick',
|
||||
}
|
||||
|
||||
PROP_TO_ATTR = {
|
||||
|
||||
@@ -4,98 +4,56 @@ Support for Tellstick switches.
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.tellstick/
|
||||
"""
|
||||
import logging
|
||||
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.components import tellstick
|
||||
from homeassistant.components.tellstick import (ATTR_DISCOVER_DEVICES,
|
||||
ATTR_DISCOVER_CONFIG)
|
||||
from homeassistant.helpers.entity import ToggleEntity
|
||||
|
||||
SIGNAL_REPETITIONS = 1
|
||||
REQUIREMENTS = ['tellcore-py==1.1.2']
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# 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 switches."""
|
||||
import tellcore.telldus as telldus
|
||||
import tellcore.constants as tellcore_constants
|
||||
from tellcore.library import DirectCallbackDispatcher
|
||||
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)
|
||||
switches_and_lights = core.devices()
|
||||
# Allow platform level override, fallback to module config
|
||||
signal_repetitions = discovery_info.get(
|
||||
ATTR_DISCOVER_CONFIG, tellstick.DEFAULT_SIGNAL_REPETITIONS)
|
||||
|
||||
switches = []
|
||||
for switch in switches_and_lights:
|
||||
if not switch.methods(tellcore_constants.TELLSTICK_DIM):
|
||||
switches.append(
|
||||
TellstickSwitchDevice(switch, signal_repetitions))
|
||||
|
||||
def _device_event_callback(id_, method, data, cid):
|
||||
"""Called from the TelldusCore library to update one device."""
|
||||
for switch_device in switches:
|
||||
if switch_device.tellstick_device.id == id_:
|
||||
switch_device.update_ha_state()
|
||||
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(switches)
|
||||
add_devices(TellstickSwitchDevice(
|
||||
tellstick.TELLCORE_REGISTRY.get_device(switch_id), signal_repetitions)
|
||||
for switch_id in discovery_info[ATTR_DISCOVER_DEVICES])
|
||||
|
||||
|
||||
class TellstickSwitchDevice(ToggleEntity):
|
||||
class TellstickSwitchDevice(tellstick.TellstickDevice, ToggleEntity):
|
||||
"""Representation of a Tellstick switch."""
|
||||
|
||||
def __init__(self, tellstick_device, signal_repetitions):
|
||||
"""Initialize the Tellstick switch."""
|
||||
import tellcore.constants as tellcore_constants
|
||||
|
||||
self.tellstick_device = tellstick_device
|
||||
self.signal_repetitions = signal_repetitions
|
||||
|
||||
self.last_sent_command_mask = (tellcore_constants.TELLSTICK_TURNON |
|
||||
tellcore_constants.TELLSTICK_TURNOFF)
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""No polling needed."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def assumed_state(self):
|
||||
"""The Tellstick devices are always assumed state."""
|
||||
return True
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the switch if any."""
|
||||
return self.tellstick_device.name
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if switch is on."""
|
||||
import tellcore.constants as tellcore_constants
|
||||
return self._state
|
||||
|
||||
last_command = self.tellstick_device.last_sent_command(
|
||||
self.last_sent_command_mask)
|
||||
def set_tellstick_state(self, last_command_sent, last_data_sent):
|
||||
"""Update the internal representation of the switch."""
|
||||
from tellcore.constants import TELLSTICK_TURNON
|
||||
self._state = last_command_sent == TELLSTICK_TURNON
|
||||
|
||||
return last_command == tellcore_constants.TELLSTICK_TURNON
|
||||
def _send_tellstick_command(self, command, data):
|
||||
"""Handle the turn_on / turn_off commands."""
|
||||
from tellcore.constants import TELLSTICK_TURNON, TELLSTICK_TURNOFF
|
||||
if command == TELLSTICK_TURNON:
|
||||
self.tellstick_device.turn_on()
|
||||
elif command == TELLSTICK_TURNOFF:
|
||||
self.tellstick_device.turn_off()
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the switch on."""
|
||||
for _ in range(self.signal_repetitions):
|
||||
self.tellstick_device.turn_on()
|
||||
self.update_ha_state()
|
||||
from tellcore.constants import TELLSTICK_TURNON
|
||||
self.call_tellstick(TELLSTICK_TURNON)
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the switch off."""
|
||||
for _ in range(self.signal_repetitions):
|
||||
self.tellstick_device.turn_off()
|
||||
self.update_ha_state()
|
||||
from tellcore.constants import TELLSTICK_TURNOFF
|
||||
self.call_tellstick(TELLSTICK_TURNOFF)
|
||||
Reference in new issue
Block a user