2016-02-02 19:25:17 +00:00
|
|
|
"""
|
2016-02-23 20:06:50 +00:00
|
|
|
Support for switches which integrates with other components.
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/switch.template/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-03-25 06:07:19 +00:00
|
|
|
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchDevice
|
2016-02-02 19:25:17 +00:00
|
|
|
from homeassistant.const import (
|
2016-02-19 05:27:50 +00:00
|
|
|
ATTR_FRIENDLY_NAME, CONF_VALUE_TEMPLATE, STATE_OFF, STATE_ON)
|
|
|
|
from homeassistant.core import EVENT_STATE_CHANGED
|
2016-02-02 19:25:17 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import generate_entity_id
|
2016-05-28 20:56:19 +00:00
|
|
|
from homeassistant.helpers.script import call_from_config
|
2016-02-23 20:06:50 +00:00
|
|
|
from homeassistant.helpers import template
|
|
|
|
from homeassistant.util import slugify
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
CONF_SWITCHES = 'switches'
|
|
|
|
|
|
|
|
ON_ACTION = 'turn_on'
|
|
|
|
OFF_ACTION = 'turn_off'
|
|
|
|
|
2016-03-25 06:07:19 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
_VALID_STATES = [STATE_ON, STATE_OFF, 'true', 'false']
|
|
|
|
|
2016-02-03 14:30:58 +00:00
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Setup the Template switch."""
|
2016-02-02 19:25:17 +00:00
|
|
|
switches = []
|
|
|
|
if config.get(CONF_SWITCHES) is None:
|
|
|
|
_LOGGER.error("Missing configuration data for switch platform")
|
|
|
|
return False
|
|
|
|
|
|
|
|
for device, device_config in config[CONF_SWITCHES].items():
|
|
|
|
|
|
|
|
if device != slugify(device):
|
|
|
|
_LOGGER.error("Found invalid key for switch.template: %s. "
|
|
|
|
"Use %s instead", device, slugify(device))
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not isinstance(device_config, dict):
|
|
|
|
_LOGGER.error("Missing configuration data for switch %s", device)
|
|
|
|
continue
|
|
|
|
|
|
|
|
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
|
|
|
state_template = device_config.get(CONF_VALUE_TEMPLATE)
|
|
|
|
on_action = device_config.get(ON_ACTION)
|
|
|
|
off_action = device_config.get(OFF_ACTION)
|
|
|
|
if state_template is None:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Missing %s for switch %s", CONF_VALUE_TEMPLATE, device)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if on_action is None or off_action is None:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Missing action for switch %s", device)
|
|
|
|
continue
|
|
|
|
|
|
|
|
switches.append(
|
|
|
|
SwitchTemplate(
|
|
|
|
hass,
|
|
|
|
device,
|
|
|
|
friendly_name,
|
|
|
|
state_template,
|
|
|
|
on_action,
|
|
|
|
off_action)
|
|
|
|
)
|
|
|
|
if not switches:
|
|
|
|
_LOGGER.error("No switches added")
|
|
|
|
return False
|
|
|
|
add_devices(switches)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class SwitchTemplate(SwitchDevice):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of a Template switch."""
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
# pylint: disable=too-many-arguments
|
2016-03-08 12:35:39 +00:00
|
|
|
def __init__(self, hass, device_id, friendly_name, state_template,
|
|
|
|
on_action, off_action):
|
|
|
|
"""Initialize the Template switch."""
|
2016-03-25 06:07:19 +00:00
|
|
|
self.hass = hass
|
2016-03-08 12:35:39 +00:00
|
|
|
self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id,
|
|
|
|
hass=hass)
|
2016-02-02 19:25:17 +00:00
|
|
|
self._name = friendly_name
|
|
|
|
self._template = state_template
|
|
|
|
self._on_action = on_action
|
|
|
|
self._off_action = off_action
|
2016-03-25 06:07:19 +00:00
|
|
|
self._state = False
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
self.update()
|
|
|
|
|
2016-03-25 06:07:19 +00:00
|
|
|
def template_switch_event_listener(event):
|
|
|
|
"""Called when the target device changes state."""
|
|
|
|
self.update_ha_state(True)
|
|
|
|
|
|
|
|
hass.bus.listen(EVENT_STATE_CHANGED,
|
|
|
|
template_switch_event_listener)
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the name of the switch."""
|
2016-02-02 19:25:17 +00:00
|
|
|
return self._name
|
|
|
|
|
2016-03-25 06:07:19 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._state
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""No polling needed."""
|
2016-02-02 19:25:17 +00:00
|
|
|
return False
|
|
|
|
|
2016-03-25 06:07:19 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""If switch is available."""
|
|
|
|
return self._state is not None
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Fire the on action."""
|
2016-02-03 14:29:25 +00:00
|
|
|
call_from_config(self.hass, self._on_action, True)
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Fire the off action."""
|
2016-02-03 14:29:25 +00:00
|
|
|
call_from_config(self.hass, self._off_action, True)
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Update the state from the template."""
|
2016-02-02 19:25:17 +00:00
|
|
|
try:
|
2016-03-25 06:07:19 +00:00
|
|
|
state = template.render(self.hass, self._template).lower()
|
|
|
|
|
|
|
|
if state in _VALID_STATES:
|
|
|
|
self._state = state in ('true', STATE_ON)
|
|
|
|
else:
|
2016-02-05 11:18:50 +00:00
|
|
|
_LOGGER.error(
|
2016-03-25 06:07:19 +00:00
|
|
|
'Received invalid switch is_on state: %s. Expected: %s',
|
|
|
|
state, ', '.join(_VALID_STATES))
|
|
|
|
self._state = None
|
2016-02-05 11:18:50 +00:00
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
except TemplateError as ex:
|
|
|
|
_LOGGER.error(ex)
|
2016-03-25 06:07:19 +00:00
|
|
|
self._state = None
|