2016-03-11 20:54:43 +00:00
|
|
|
"""
|
|
|
|
Tellstick Component.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
2016-10-30 08:58:34 +00:00
|
|
|
https://home-assistant.io/components/tellstick/
|
2016-03-11 20:54:43 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import threading
|
2016-09-14 06:29:15 +00:00
|
|
|
|
2016-04-21 14:57:28 +00:00
|
|
|
import voluptuous as vol
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
from homeassistant.helpers import discovery
|
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
2016-03-11 20:54:43 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2016-09-14 06:29:15 +00:00
|
|
|
DOMAIN = 'tellstick'
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
REQUIREMENTS = ['tellcore-py==1.1.2']
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-09-14 06:29:15 +00:00
|
|
|
ATTR_SIGNAL_REPETITIONS = 'signal_repetitions'
|
2016-03-11 20:54:43 +00:00
|
|
|
DEFAULT_SIGNAL_REPETITIONS = 1
|
|
|
|
|
2016-09-14 06:29:15 +00:00
|
|
|
ATTR_DISCOVER_DEVICES = 'devices'
|
|
|
|
ATTR_DISCOVER_CONFIG = 'config'
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
# Use a global tellstick domain lock to avoid getting Tellcore errors when
|
|
|
|
# calling concurrently.
|
2016-03-11 20:54:43 +00:00
|
|
|
TELLSTICK_LOCK = threading.Lock()
|
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
# A TellstickRegistry that keeps a map from tellcore_id to the corresponding
|
|
|
|
# tellcore_device and HA device (entity).
|
2016-03-11 20:54:43 +00:00
|
|
|
TELLCORE_REGISTRY = None
|
|
|
|
|
2016-04-21 14:57:28 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Optional(ATTR_SIGNAL_REPETITIONS,
|
2016-09-14 06:29:15 +00:00
|
|
|
default=DEFAULT_SIGNAL_REPETITIONS): vol.Coerce(int),
|
2016-04-21 14:57:28 +00:00
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
def _discover(hass, config, component_name, found_tellcore_devices):
|
2016-03-11 20:54:43 +00:00
|
|
|
"""Setup and send the discovery event."""
|
2016-11-23 05:48:22 +00:00
|
|
|
if not len(found_tellcore_devices):
|
2016-03-11 20:54:43 +00:00
|
|
|
return
|
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
_LOGGER.info("Discovered %d new %s devices", len(found_tellcore_devices),
|
|
|
|
component_name)
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-04-21 14:57:28 +00:00
|
|
|
signal_repetitions = config[DOMAIN].get(ATTR_SIGNAL_REPETITIONS)
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
discovery.load_platform(hass, component_name, DOMAIN, {
|
2016-11-23 05:48:22 +00:00
|
|
|
ATTR_DISCOVER_DEVICES: found_tellcore_devices,
|
2016-06-12 00:43:13 +00:00
|
|
|
ATTR_DISCOVER_CONFIG: signal_repetitions}, config)
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
"""Setup the Tellstick component."""
|
2016-11-23 05:48:22 +00:00
|
|
|
from tellcore.constants import TELLSTICK_DIM
|
2016-03-11 20:54:43 +00:00
|
|
|
from tellcore.library import DirectCallbackDispatcher
|
2016-11-23 05:48:22 +00:00
|
|
|
from tellcore.telldus import TelldusCore
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
global TELLCORE_REGISTRY
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
try:
|
|
|
|
tellcore_lib = TelldusCore(
|
|
|
|
callback_dispatcher=DirectCallbackDispatcher())
|
|
|
|
except OSError:
|
|
|
|
_LOGGER.exception('Could not initialize Tellstick')
|
|
|
|
return False
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
# Get all devices, switches and lights alike
|
|
|
|
all_tellcore_devices = tellcore_lib.devices()
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
# Register devices
|
2016-11-23 05:48:22 +00:00
|
|
|
TELLCORE_REGISTRY = TellstickRegistry(hass, tellcore_lib)
|
|
|
|
TELLCORE_REGISTRY.register_tellcore_devices(all_tellcore_devices)
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
# Discover the switches
|
2016-11-23 05:48:22 +00:00
|
|
|
_discover(hass, config, 'switch',
|
|
|
|
[tellcore_device.id for tellcore_device in all_tellcore_devices
|
|
|
|
if not tellcore_device.methods(TELLSTICK_DIM)])
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
# Discover the lights
|
2016-11-23 05:48:22 +00:00
|
|
|
_discover(hass, config, 'light',
|
|
|
|
[tellcore_device.id for tellcore_device in all_tellcore_devices
|
|
|
|
if tellcore_device.methods(TELLSTICK_DIM)])
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2016-09-14 06:29:15 +00:00
|
|
|
class TellstickRegistry(object):
|
|
|
|
"""Handle everything around Tellstick callbacks.
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
Keeps a map device ids to the tellcore device object, and
|
|
|
|
another to the HA device objects (entities).
|
|
|
|
|
|
|
|
Also responsible for registering / cleanup of callbacks, and for
|
|
|
|
dispatching the callbacks to the corresponding HA device object.
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
All device specific logic should be elsewhere (Entities).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, hass, tellcore_lib):
|
2016-09-14 06:29:15 +00:00
|
|
|
"""Initialize the Tellstick mappings and callbacks."""
|
2016-03-11 20:54:43 +00:00
|
|
|
# used when map callback device id to ha entities.
|
2016-11-23 05:48:22 +00:00
|
|
|
self._id_to_ha_device_map = {}
|
|
|
|
self._id_to_tellcore_device_map = {}
|
|
|
|
self._setup_tellcore_callback(hass, tellcore_lib)
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
def _tellcore_event_callback(self, tellcore_id, tellcore_command,
|
|
|
|
tellcore_data, cid):
|
2016-09-14 06:29:15 +00:00
|
|
|
"""Handle the actual callback from Tellcore."""
|
2016-11-23 05:48:22 +00:00
|
|
|
ha_device = self._id_to_ha_device_map.get(tellcore_id, None)
|
|
|
|
if ha_device is not None:
|
|
|
|
# Pass it on to the HA device object
|
|
|
|
ha_device.update_from_tellcore(tellcore_command, tellcore_data)
|
|
|
|
ha_device.schedule_update_ha_state()
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
def _setup_tellcore_callback(self, hass, tellcore_lib):
|
2016-03-11 20:54:43 +00:00
|
|
|
"""Register the callback handler."""
|
2016-11-23 05:48:22 +00:00
|
|
|
callback_id = tellcore_lib.register_device_event(
|
|
|
|
self._tellcore_event_callback)
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
def clean_up_callback(event):
|
|
|
|
"""Unregister the callback bindings."""
|
|
|
|
if callback_id is not None:
|
|
|
|
tellcore_lib.unregister_callback(callback_id)
|
2016-11-23 05:48:22 +00:00
|
|
|
_LOGGER.debug("Tellstick callback unregistered")
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, clean_up_callback)
|
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
def register_ha_device(self, tellcore_id, ha_device):
|
|
|
|
"""Register a new HA device to receive callback updates."""
|
|
|
|
self._id_to_ha_device_map[tellcore_id] = ha_device
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
def register_tellcore_devices(self, tellcore_devices):
|
2016-03-11 20:54:43 +00:00
|
|
|
"""Register a list of devices."""
|
2016-11-23 05:48:22 +00:00
|
|
|
self._id_to_tellcore_device_map.update(
|
|
|
|
{tellcore_device.id: tellcore_device for tellcore_device
|
|
|
|
in tellcore_devices})
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
def get_tellcore_device(self, tellcore_id):
|
2016-03-11 20:54:43 +00:00
|
|
|
"""Return a device by tellcore_id."""
|
2016-11-23 05:48:22 +00:00
|
|
|
return self._id_to_tellcore_device_map.get(tellcore_id, None)
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TellstickDevice(Entity):
|
2016-09-14 06:29:15 +00:00
|
|
|
"""Representation of a Tellstick device.
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
Contains the common logic for all Tellstick devices.
|
|
|
|
"""
|
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
def __init__(self, tellcore_id, signal_repetitions):
|
2016-09-14 06:29:15 +00:00
|
|
|
"""Initalize the Tellstick device."""
|
2016-11-23 05:48:22 +00:00
|
|
|
self._signal_repetitions = signal_repetitions
|
2016-03-11 20:54:43 +00:00
|
|
|
self._state = None
|
2016-11-23 05:48:22 +00:00
|
|
|
# Look up our corresponding tellcore device
|
|
|
|
self._tellcore_device = TELLCORE_REGISTRY.get_tellcore_device(
|
|
|
|
tellcore_id)
|
2016-03-11 20:54:43 +00:00
|
|
|
# Query tellcore for the current state
|
|
|
|
self.update()
|
2016-11-23 05:48:22 +00:00
|
|
|
# Add ourselves to the mapping
|
|
|
|
TELLCORE_REGISTRY.register_ha_device(tellcore_id, self)
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-11-23 05:48:22 +00:00
|
|
|
"""Tell Home Assistant not to poll this device."""
|
2016-03-11 20:54:43 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
|
|
|
"""Tellstick devices are always assumed state."""
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-11-23 05:48:22 +00:00
|
|
|
"""Return the name of the device as reported by tellcore."""
|
|
|
|
return self._tellcore_device.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the device is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
def _parse_ha_data(self, kwargs):
|
|
|
|
"""Turn the value from HA into something useful."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def _parse_tellcore_data(self, tellcore_data):
|
|
|
|
"""Turn the value recieved from tellcore into something useful."""
|
|
|
|
raise NotImplementedError
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
def _update_model(self, new_state, data):
|
|
|
|
"""Update the device entity state to match the arguments."""
|
|
|
|
raise NotImplementedError
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
def _send_tellstick_command(self):
|
|
|
|
"""Let tellcore update the device to match the current state."""
|
|
|
|
raise NotImplementedError
|
2016-03-11 20:54:43 +00:00
|
|
|
|
2016-11-23 05:48:22 +00:00
|
|
|
def _do_action(self, new_state, data):
|
|
|
|
"""The logic for actually turning on or off the device."""
|
2016-03-11 20:54:43 +00:00
|
|
|
from tellcore.library import TelldusError
|
2016-11-23 05:48:22 +00:00
|
|
|
|
2016-03-11 20:54:43 +00:00
|
|
|
with TELLSTICK_LOCK:
|
2016-11-23 05:48:22 +00:00
|
|
|
# Update self with requested new state
|
|
|
|
self._update_model(new_state, data)
|
|
|
|
# ... and then send this new state to the Tellstick
|
2016-03-11 20:54:43 +00:00
|
|
|
try:
|
2016-11-23 05:48:22 +00:00
|
|
|
for _ in range(self._signal_repetitions):
|
|
|
|
self._send_tellstick_command()
|
2016-03-11 20:54:43 +00:00
|
|
|
except TelldusError:
|
|
|
|
_LOGGER.error(TelldusError)
|
2016-11-23 05:48:22 +00:00
|
|
|
self.update_ha_state()
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the switch on."""
|
|
|
|
self._do_action(True, self._parse_ha_data(kwargs))
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the switch off."""
|
|
|
|
self._do_action(False, None)
|
|
|
|
|
|
|
|
def update_from_tellcore(self, tellcore_command, tellcore_data):
|
|
|
|
"""Handle updates from the tellcore callback."""
|
|
|
|
from tellcore.constants import (TELLSTICK_TURNON, TELLSTICK_TURNOFF,
|
|
|
|
TELLSTICK_DIM)
|
|
|
|
|
|
|
|
if tellcore_command not in [TELLSTICK_TURNON, TELLSTICK_TURNOFF,
|
|
|
|
TELLSTICK_DIM]:
|
|
|
|
_LOGGER.debug("Unhandled tellstick command: %d",
|
|
|
|
tellcore_command)
|
|
|
|
return
|
|
|
|
|
|
|
|
self._update_model(tellcore_command != TELLSTICK_TURNOFF,
|
|
|
|
self._parse_tellcore_data(tellcore_data))
|
2016-03-11 20:54:43 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Poll the current state of the device."""
|
|
|
|
from tellcore.library import TelldusError
|
2016-11-23 05:48:22 +00:00
|
|
|
from tellcore.constants import (TELLSTICK_TURNON, TELLSTICK_TURNOFF,
|
|
|
|
TELLSTICK_DIM)
|
|
|
|
|
2016-03-11 20:54:43 +00:00
|
|
|
try:
|
2016-11-23 05:48:22 +00:00
|
|
|
last_tellcore_command = self._tellcore_device.last_sent_command(
|
|
|
|
TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM
|
2016-03-11 20:54:43 +00:00
|
|
|
)
|
2016-11-23 05:48:22 +00:00
|
|
|
last_tellcore_data = self._tellcore_device.last_sent_value()
|
|
|
|
|
|
|
|
self.update_from_tellcore(last_tellcore_command,
|
|
|
|
last_tellcore_data)
|
2016-03-11 20:54:43 +00:00
|
|
|
except TelldusError:
|
|
|
|
_LOGGER.error(TelldusError)
|