2015-12-27 11:32:08 +00:00
|
|
|
"""
|
2016-03-08 12:35:39 +00:00
|
|
|
Support for Tellstick switches using Tellstick Net.
|
|
|
|
|
|
|
|
This platform uses the Telldus Live online service.
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/switch.tellduslive/
|
|
|
|
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components import tellduslive
|
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Setup Tellstick switches."""
|
2016-01-18 18:41:41 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
2016-02-03 21:31:28 +00:00
|
|
|
add_devices(TelldusLiveSwitch(switch) for switch in discovery_info)
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TelldusLiveSwitch(ToggleEntity):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of a Tellstick switch."""
|
2015-12-27 11:32:08 +00:00
|
|
|
|
2016-02-03 21:31:28 +00:00
|
|
|
def __init__(self, switch_id):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the switch."""
|
2015-12-27 11:32:08 +00:00
|
|
|
self._id = switch_id
|
|
|
|
self.update()
|
2016-02-03 21:31:28 +00:00
|
|
|
_LOGGER.debug("created switch %s", self)
|
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Get the latest date and update the state."""
|
2016-02-03 21:31:28 +00:00
|
|
|
tellduslive.NETWORK.update_switches()
|
|
|
|
self._switch = tellduslive.NETWORK.get_switch(self._id)
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Polling is needed."""
|
2016-02-03 21:31:28 +00:00
|
|
|
return True
|
2015-12-27 11:32:08 +00:00
|
|
|
|
2016-02-16 16:18:49 +00:00
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if unable to access real state of entity."""
|
2016-02-16 16:18:49 +00:00
|
|
|
return True
|
|
|
|
|
2015-12-27 11:32:08 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the name of the switch if any."""
|
2016-02-03 21:31:28 +00:00
|
|
|
return self._switch["name"]
|
2015-12-27 11:32:08 +00:00
|
|
|
|
2016-02-03 21:31:28 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the state of the switch."""
|
2016-02-03 21:31:28 +00:00
|
|
|
return not self._switch.get("offline", False)
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if switch is on."""
|
2016-02-03 21:31:28 +00:00
|
|
|
from tellive.live import const
|
|
|
|
return self._switch["state"] == const.TELLSTICK_TURNON
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch on."""
|
2016-02-03 21:31:28 +00:00
|
|
|
tellduslive.NETWORK.turn_switch_on(self._id)
|
2015-12-27 11:32:08 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch off."""
|
2016-02-03 21:31:28 +00:00
|
|
|
tellduslive.NETWORK.turn_switch_off(self._id)
|