Add optimistic mode to template switch (#31637)

pull/37591/head
akloeckner 2020-07-07 01:32:33 +02:00 committed by GitHub
parent f49ce5d1b4
commit d0668d3a6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 576 additions and 463 deletions

View File

@ -24,6 +24,7 @@ from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.event import async_track_state_change
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.script import Script
from . import extract_entities, initialise_templates
@ -37,7 +38,7 @@ OFF_ACTION = "turn_off"
SWITCH_SCHEMA = vol.Schema(
{
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template,
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
@ -59,7 +60,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
for device, device_config in config[CONF_SWITCHES].items():
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
state_template = device_config[CONF_VALUE_TEMPLATE]
state_template = device_config.get(CONF_VALUE_TEMPLATE)
icon_template = device_config.get(CONF_ICON_TEMPLATE)
entity_picture_template = device_config.get(CONF_ENTITY_PICTURE_TEMPLATE)
availability_template = device_config.get(CONF_AVAILABILITY_TEMPLATE)
@ -96,7 +97,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
async_add_entities(switches)
class SwitchTemplate(SwitchEntity):
class SwitchTemplate(SwitchEntity, RestoreEntity):
"""Representation of a Template switch."""
def __init__(
@ -133,6 +134,18 @@ class SwitchTemplate(SwitchEntity):
async def async_added_to_hass(self):
"""Register callbacks."""
if self._template is None:
# restore state after startup
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state:
self._state = state.state == STATE_ON
# no need to listen for events
return
# set up event listening
@callback
def template_switch_state_listener(entity, old_state, new_state):
"""Handle target device state changes."""
@ -184,13 +197,21 @@ class SwitchTemplate(SwitchEntity):
async def async_turn_on(self, **kwargs):
"""Fire the on action."""
await self._on_script.async_run(context=self._context)
if self._template is None:
self._state = True
self.async_write_ha_state()
async def async_turn_off(self, **kwargs):
"""Fire the off action."""
await self._off_script.async_run(context=self._context)
if self._template is None:
self._state = False
self.async_write_ha_state()
async def async_update(self):
"""Update the state from the template."""
if self._template is None:
return
try:
state = self._template.async_render().lower()
@ -243,3 +264,8 @@ class SwitchTemplate(SwitchEntity):
self._name,
ex,
)
@property
def assumed_state(self):
"""State is assumed, if no template given."""
return self._template is None

File diff suppressed because it is too large Load Diff