Clean up template platforms
This commit is contained in:
1 parent
70ce179224
commit
41f205e09d
5 files changed
+82
-89
No files matched your search
@@ -6,7 +6,7 @@ https://home-assistant.io/components/switch.template/
|
||||
"""
|
||||
import logging
|
||||
|
||||
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
||||
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchDevice
|
||||
from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME, CONF_VALUE_TEMPLATE, STATE_OFF, STATE_ON)
|
||||
from homeassistant.core import EVENT_STATE_CHANGED
|
||||
@@ -16,17 +16,14 @@ from homeassistant.helpers.service import call_from_config
|
||||
from homeassistant.helpers import template
|
||||
from homeassistant.util import slugify
|
||||
|
||||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_SWITCHES = 'switches'
|
||||
|
||||
STATE_ERROR = 'error'
|
||||
|
||||
ON_ACTION = 'turn_on'
|
||||
OFF_ACTION = 'turn_off'
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
_VALID_STATES = [STATE_ON, STATE_OFF, 'true', 'false']
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
@@ -84,32 +81,44 @@ class SwitchTemplate(SwitchDevice):
|
||||
def __init__(self, hass, device_id, friendly_name, state_template,
|
||||
on_action, off_action):
|
||||
"""Initialize the Template switch."""
|
||||
self.hass = hass
|
||||
self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id,
|
||||
hass=hass)
|
||||
self.hass = hass
|
||||
self._name = friendly_name
|
||||
self._template = state_template
|
||||
self._on_action = on_action
|
||||
self._off_action = off_action
|
||||
self.update()
|
||||
self.hass.bus.listen(EVENT_STATE_CHANGED, self._event_listener)
|
||||
self._state = False
|
||||
|
||||
def _event_listener(self, event):
|
||||
"""Called when the target device changes state."""
|
||||
if not hasattr(self, 'hass') or self.hass is None:
|
||||
return
|
||||
self.update_ha_state(True)
|
||||
self.update()
|
||||
|
||||
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)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the switch."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if device is on."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""No polling needed."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""If switch is available."""
|
||||
return self._state is not None
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Fire the on action."""
|
||||
call_from_config(self.hass, self._on_action, True)
|
||||
@@ -118,30 +127,19 @@ class SwitchTemplate(SwitchDevice):
|
||||
"""Fire the off action."""
|
||||
call_from_config(self.hass, self._off_action, True)
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if device is on."""
|
||||
return self._value.lower() == 'true' or self._value == STATE_ON
|
||||
|
||||
@property
|
||||
def is_off(self):
|
||||
"""Return true if device is off."""
|
||||
return self._value.lower() == 'false' or self._value == STATE_OFF
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return true if entity is available."""
|
||||
return self.is_on or self.is_off
|
||||
|
||||
def update(self):
|
||||
"""Update the state from the template."""
|
||||
try:
|
||||
self._value = template.render(self.hass, self._template)
|
||||
if not self.available:
|
||||
state = template.render(self.hass, self._template).lower()
|
||||
|
||||
if state in _VALID_STATES:
|
||||
self._state = state in ('true', STATE_ON)
|
||||
else:
|
||||
_LOGGER.error(
|
||||
"`%s` is not a switch state, setting %s to unavailable",
|
||||
self._value, self.entity_id)
|
||||
'Received invalid switch is_on state: %s. Expected: %s',
|
||||
state, ', '.join(_VALID_STATES))
|
||||
self._state = None
|
||||
|
||||
except TemplateError as ex:
|
||||
self._value = STATE_ERROR
|
||||
_LOGGER.error(ex)
|
||||
self._state = None
|
||||
Reference in new issue
Block a user