2019-04-03 15:40:03 +00:00
|
|
|
"""Support for covers which integrate with other components."""
|
2017-06-30 15:24:29 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.cover import (
|
2019-12-08 20:05:08 +00:00
|
|
|
ATTR_POSITION,
|
|
|
|
ATTR_TILT_POSITION,
|
|
|
|
DEVICE_CLASSES_SCHEMA,
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTITY_ID_FORMAT,
|
|
|
|
PLATFORM_SCHEMA,
|
2019-12-08 20:05:08 +00:00
|
|
|
SUPPORT_CLOSE,
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_CLOSE_TILT,
|
|
|
|
SUPPORT_OPEN,
|
2019-12-08 20:05:08 +00:00
|
|
|
SUPPORT_OPEN_TILT,
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_SET_POSITION,
|
2019-12-08 20:05:08 +00:00
|
|
|
SUPPORT_SET_TILT_POSITION,
|
|
|
|
SUPPORT_STOP,
|
|
|
|
SUPPORT_STOP_TILT,
|
|
|
|
CoverDevice,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-06-30 15:24:29 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DEVICE_CLASS,
|
2019-12-08 20:05:08 +00:00
|
|
|
CONF_ENTITY_ID,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ENTITY_PICTURE_TEMPLATE,
|
2019-12-08 20:05:08 +00:00
|
|
|
CONF_FRIENDLY_NAME,
|
|
|
|
CONF_ICON_TEMPLATE,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_OPTIMISTIC,
|
2019-12-08 20:05:08 +00:00
|
|
|
CONF_VALUE_TEMPLATE,
|
|
|
|
EVENT_HOMEASSISTANT_START,
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_CLOSED,
|
2019-12-08 20:05:08 +00:00
|
|
|
STATE_OPEN,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-12-08 20:05:08 +00:00
|
|
|
from homeassistant.core import callback
|
2017-06-30 15:24:29 +00:00
|
|
|
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.script import Script
|
2019-12-08 20:05:08 +00:00
|
|
|
|
2019-11-26 00:30:49 +00:00
|
|
|
from . import extract_entities, initialise_templates
|
2019-09-28 11:53:16 +00:00
|
|
|
from .const import CONF_AVAILABILITY_TEMPLATE
|
2017-06-30 15:24:29 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-07-31 19:25:30 +00:00
|
|
|
_VALID_STATES = [STATE_OPEN, STATE_CLOSED, "true", "false"]
|
|
|
|
|
|
|
|
CONF_COVERS = "covers"
|
|
|
|
|
|
|
|
CONF_POSITION_TEMPLATE = "position_template"
|
|
|
|
CONF_TILT_TEMPLATE = "tilt_template"
|
|
|
|
OPEN_ACTION = "open_cover"
|
|
|
|
CLOSE_ACTION = "close_cover"
|
|
|
|
STOP_ACTION = "stop_cover"
|
|
|
|
POSITION_ACTION = "set_cover_position"
|
|
|
|
TILT_ACTION = "set_cover_tilt_position"
|
|
|
|
CONF_TILT_OPTIMISTIC = "tilt_optimistic"
|
|
|
|
|
|
|
|
CONF_VALUE_OR_POSITION_TEMPLATE = "value_or_position"
|
|
|
|
CONF_OPEN_OR_CLOSE = "open_or_close"
|
|
|
|
|
|
|
|
TILT_FEATURES = (
|
|
|
|
SUPPORT_OPEN_TILT
|
|
|
|
| SUPPORT_CLOSE_TILT
|
|
|
|
| SUPPORT_STOP_TILT
|
|
|
|
| SUPPORT_SET_TILT_POSITION
|
|
|
|
)
|
|
|
|
|
|
|
|
COVER_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Inclusive(OPEN_ACTION, CONF_OPEN_OR_CLOSE): cv.SCRIPT_SCHEMA,
|
|
|
|
vol.Inclusive(CLOSE_ACTION, CONF_OPEN_OR_CLOSE): cv.SCRIPT_SCHEMA,
|
|
|
|
vol.Optional(STOP_ACTION): cv.SCRIPT_SCHEMA,
|
|
|
|
vol.Exclusive(
|
|
|
|
CONF_POSITION_TEMPLATE, CONF_VALUE_OR_POSITION_TEMPLATE
|
|
|
|
): cv.template,
|
|
|
|
vol.Exclusive(
|
|
|
|
CONF_VALUE_TEMPLATE, CONF_VALUE_OR_POSITION_TEMPLATE
|
|
|
|
): cv.template,
|
2019-09-28 11:53:16 +00:00
|
|
|
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_POSITION_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_TILT_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
|
|
|
vol.Optional(CONF_OPTIMISTIC): cv.boolean,
|
|
|
|
vol.Optional(CONF_TILT_OPTIMISTIC): cv.boolean,
|
|
|
|
vol.Optional(POSITION_ACTION): cv.SCRIPT_SCHEMA,
|
|
|
|
vol.Optional(TILT_ACTION): cv.SCRIPT_SCHEMA,
|
|
|
|
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_ENTITY_ID): cv.entity_ids,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_COVERS): cv.schema_with_slug_keys(COVER_SCHEMA)}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-06-30 15:24:29 +00:00
|
|
|
"""Set up the Template cover."""
|
|
|
|
covers = []
|
|
|
|
|
|
|
|
for device, device_config in config[CONF_COVERS].items():
|
|
|
|
state_template = device_config.get(CONF_VALUE_TEMPLATE)
|
|
|
|
position_template = device_config.get(CONF_POSITION_TEMPLATE)
|
|
|
|
tilt_template = device_config.get(CONF_TILT_TEMPLATE)
|
|
|
|
icon_template = device_config.get(CONF_ICON_TEMPLATE)
|
2019-09-28 11:53:16 +00:00
|
|
|
availability_template = device_config.get(CONF_AVAILABILITY_TEMPLATE)
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_picture_template = device_config.get(CONF_ENTITY_PICTURE_TEMPLATE)
|
2019-11-26 00:30:49 +00:00
|
|
|
|
|
|
|
friendly_name = device_config.get(CONF_FRIENDLY_NAME, device)
|
2019-04-29 12:38:59 +00:00
|
|
|
device_class = device_config.get(CONF_DEVICE_CLASS)
|
2017-07-07 05:35:59 +00:00
|
|
|
open_action = device_config.get(OPEN_ACTION)
|
|
|
|
close_action = device_config.get(CLOSE_ACTION)
|
|
|
|
stop_action = device_config.get(STOP_ACTION)
|
2017-06-30 15:24:29 +00:00
|
|
|
position_action = device_config.get(POSITION_ACTION)
|
|
|
|
tilt_action = device_config.get(TILT_ACTION)
|
2017-08-25 10:33:53 +00:00
|
|
|
optimistic = device_config.get(CONF_OPTIMISTIC)
|
|
|
|
tilt_optimistic = device_config.get(CONF_TILT_OPTIMISTIC)
|
2017-06-30 15:24:29 +00:00
|
|
|
|
2017-07-07 05:35:59 +00:00
|
|
|
if position_action is None and open_action is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Must specify at least one of %s" or "%s", OPEN_ACTION, POSITION_ACTION
|
|
|
|
)
|
2017-07-07 05:35:59 +00:00
|
|
|
continue
|
2019-11-26 00:30:49 +00:00
|
|
|
|
|
|
|
templates = {
|
|
|
|
CONF_VALUE_TEMPLATE: state_template,
|
|
|
|
CONF_POSITION_TEMPLATE: position_template,
|
|
|
|
CONF_TILT_TEMPLATE: tilt_template,
|
|
|
|
CONF_ICON_TEMPLATE: icon_template,
|
|
|
|
CONF_AVAILABILITY_TEMPLATE: availability_template,
|
|
|
|
CONF_ENTITY_PICTURE_TEMPLATE: entity_picture_template,
|
|
|
|
}
|
|
|
|
|
|
|
|
initialise_templates(hass, templates)
|
2020-01-24 11:40:00 +00:00
|
|
|
entity_ids = extract_entities(
|
|
|
|
device, "cover", device_config.get(CONF_ENTITY_ID), templates
|
|
|
|
)
|
2017-06-30 15:24:29 +00:00
|
|
|
|
|
|
|
covers.append(
|
|
|
|
CoverTemplate(
|
|
|
|
hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
device,
|
|
|
|
friendly_name,
|
|
|
|
device_class,
|
|
|
|
state_template,
|
|
|
|
position_template,
|
|
|
|
tilt_template,
|
|
|
|
icon_template,
|
|
|
|
entity_picture_template,
|
2019-09-28 11:53:16 +00:00
|
|
|
availability_template,
|
2019-07-31 19:25:30 +00:00
|
|
|
open_action,
|
|
|
|
close_action,
|
|
|
|
stop_action,
|
|
|
|
position_action,
|
|
|
|
tilt_action,
|
|
|
|
optimistic,
|
|
|
|
tilt_optimistic,
|
|
|
|
entity_ids,
|
2017-06-30 15:24:29 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
if not covers:
|
|
|
|
_LOGGER.error("No covers added")
|
|
|
|
return False
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(covers)
|
2017-06-30 15:24:29 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class CoverTemplate(CoverDevice):
|
|
|
|
"""Representation of a Template cover."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass,
|
|
|
|
device_id,
|
|
|
|
friendly_name,
|
|
|
|
device_class,
|
|
|
|
state_template,
|
|
|
|
position_template,
|
|
|
|
tilt_template,
|
|
|
|
icon_template,
|
|
|
|
entity_picture_template,
|
2019-09-28 11:53:16 +00:00
|
|
|
availability_template,
|
2019-07-31 19:25:30 +00:00
|
|
|
open_action,
|
|
|
|
close_action,
|
|
|
|
stop_action,
|
|
|
|
position_action,
|
|
|
|
tilt_action,
|
|
|
|
optimistic,
|
|
|
|
tilt_optimistic,
|
|
|
|
entity_ids,
|
|
|
|
):
|
2017-06-30 15:24:29 +00:00
|
|
|
"""Initialize the Template cover."""
|
|
|
|
self.hass = hass
|
|
|
|
self.entity_id = async_generate_entity_id(
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTITY_ID_FORMAT, device_id, hass=hass
|
|
|
|
)
|
2017-06-30 15:24:29 +00:00
|
|
|
self._name = friendly_name
|
|
|
|
self._template = state_template
|
|
|
|
self._position_template = position_template
|
|
|
|
self._tilt_template = tilt_template
|
|
|
|
self._icon_template = icon_template
|
2019-04-29 12:38:59 +00:00
|
|
|
self._device_class = device_class
|
2017-10-30 16:28:37 +00:00
|
|
|
self._entity_picture_template = entity_picture_template
|
2019-09-28 11:53:16 +00:00
|
|
|
self._availability_template = availability_template
|
2017-07-07 05:35:59 +00:00
|
|
|
self._open_script = None
|
|
|
|
if open_action is not None:
|
|
|
|
self._open_script = Script(hass, open_action)
|
|
|
|
self._close_script = None
|
|
|
|
if close_action is not None:
|
|
|
|
self._close_script = Script(hass, close_action)
|
|
|
|
self._stop_script = None
|
|
|
|
if stop_action is not None:
|
|
|
|
self._stop_script = Script(hass, stop_action)
|
2017-06-30 15:24:29 +00:00
|
|
|
self._position_script = None
|
|
|
|
if position_action is not None:
|
|
|
|
self._position_script = Script(hass, position_action)
|
|
|
|
self._tilt_script = None
|
|
|
|
if tilt_action is not None:
|
|
|
|
self._tilt_script = Script(hass, tilt_action)
|
2019-07-31 19:25:30 +00:00
|
|
|
self._optimistic = optimistic or (not state_template and not position_template)
|
2017-08-25 10:33:53 +00:00
|
|
|
self._tilt_optimistic = tilt_optimistic or not tilt_template
|
2017-06-30 15:24:29 +00:00
|
|
|
self._icon = None
|
2017-10-30 16:28:37 +00:00
|
|
|
self._entity_picture = None
|
2017-06-30 15:24:29 +00:00
|
|
|
self._position = None
|
|
|
|
self._tilt_value = None
|
|
|
|
self._entities = entity_ids
|
2019-09-28 11:53:16 +00:00
|
|
|
self._available = True
|
2017-06-30 15:24:29 +00:00
|
|
|
|
2018-06-30 16:10:59 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-06-30 15:24:29 +00:00
|
|
|
"""Register callbacks."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-06-30 15:24:29 +00:00
|
|
|
@callback
|
|
|
|
def template_cover_state_listener(entity, old_state, new_state):
|
|
|
|
"""Handle target device state changes."""
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state(True)
|
2017-06-30 15:24:29 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def template_cover_startup(event):
|
|
|
|
"""Update template on startup."""
|
|
|
|
async_track_state_change(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, self._entities, template_cover_state_listener
|
|
|
|
)
|
2017-06-30 15:24:29 +00:00
|
|
|
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state(True)
|
2017-06-30 15:24:29 +00:00
|
|
|
|
|
|
|
self.hass.bus.async_listen_once(
|
2019-07-31 19:25:30 +00:00
|
|
|
EVENT_HOMEASSISTANT_START, template_cover_startup
|
|
|
|
)
|
2017-06-30 15:24:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the cover."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closed(self):
|
|
|
|
"""Return if the cover is closed."""
|
|
|
|
return self._position == 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_cover_position(self):
|
|
|
|
"""Return current position of cover.
|
|
|
|
|
|
|
|
None is unknown, 0 is closed, 100 is fully open.
|
|
|
|
"""
|
2018-03-23 20:54:19 +00:00
|
|
|
if self._position_template or self._position_script:
|
|
|
|
return self._position
|
|
|
|
return None
|
2017-06-30 15:24:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def current_cover_tilt_position(self):
|
|
|
|
"""Return current position of cover tilt.
|
|
|
|
|
|
|
|
None is unknown, 0 is closed, 100 is fully open.
|
|
|
|
"""
|
|
|
|
return self._tilt_value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
|
|
|
return self._icon
|
|
|
|
|
2017-10-30 16:28:37 +00:00
|
|
|
@property
|
|
|
|
def entity_picture(self):
|
|
|
|
"""Return the entity picture to use in the frontend, if any."""
|
|
|
|
return self._entity_picture
|
|
|
|
|
2019-04-29 12:38:59 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the cover."""
|
|
|
|
return self._device_class
|
|
|
|
|
2017-06-30 15:24:29 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
2017-07-07 05:35:59 +00:00
|
|
|
supported_features = SUPPORT_OPEN | SUPPORT_CLOSE
|
|
|
|
|
|
|
|
if self._stop_script is not None:
|
|
|
|
supported_features |= SUPPORT_STOP
|
2017-06-30 15:24:29 +00:00
|
|
|
|
2017-07-07 05:35:59 +00:00
|
|
|
if self._position_script is not None:
|
2017-06-30 15:24:29 +00:00
|
|
|
supported_features |= SUPPORT_SET_POSITION
|
|
|
|
|
|
|
|
if self.current_cover_tilt_position is not None:
|
|
|
|
supported_features |= TILT_FEATURES
|
|
|
|
|
|
|
|
return supported_features
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return the polling state."""
|
|
|
|
return False
|
|
|
|
|
2019-09-28 11:53:16 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if the device is available."""
|
|
|
|
return self._available
|
|
|
|
|
2018-06-30 16:10:59 +00:00
|
|
|
async def async_open_cover(self, **kwargs):
|
2017-06-30 15:24:29 +00:00
|
|
|
"""Move the cover up."""
|
2017-07-07 05:35:59 +00:00
|
|
|
if self._open_script:
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._open_script.async_run(context=self._context)
|
2017-07-07 05:35:59 +00:00
|
|
|
elif self._position_script:
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._position_script.async_run(
|
2019-07-31 19:25:30 +00:00
|
|
|
{"position": 100}, context=self._context
|
|
|
|
)
|
2017-08-25 10:33:53 +00:00
|
|
|
if self._optimistic:
|
|
|
|
self._position = 100
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-06-30 15:24:29 +00:00
|
|
|
|
2018-06-30 16:10:59 +00:00
|
|
|
async def async_close_cover(self, **kwargs):
|
2017-06-30 15:24:29 +00:00
|
|
|
"""Move the cover down."""
|
2017-07-07 05:35:59 +00:00
|
|
|
if self._close_script:
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._close_script.async_run(context=self._context)
|
2017-07-07 05:35:59 +00:00
|
|
|
elif self._position_script:
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._position_script.async_run(
|
2019-07-31 19:25:30 +00:00
|
|
|
{"position": 0}, context=self._context
|
|
|
|
)
|
2017-08-25 10:33:53 +00:00
|
|
|
if self._optimistic:
|
|
|
|
self._position = 0
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-06-30 15:24:29 +00:00
|
|
|
|
2018-06-30 16:10:59 +00:00
|
|
|
async def async_stop_cover(self, **kwargs):
|
2017-06-30 15:24:29 +00:00
|
|
|
"""Fire the stop action."""
|
2017-07-07 05:35:59 +00:00
|
|
|
if self._stop_script:
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._stop_script.async_run(context=self._context)
|
2017-06-30 15:24:29 +00:00
|
|
|
|
2018-06-30 16:10:59 +00:00
|
|
|
async def async_set_cover_position(self, **kwargs):
|
2017-06-30 15:24:29 +00:00
|
|
|
"""Set cover position."""
|
|
|
|
self._position = kwargs[ATTR_POSITION]
|
2018-06-30 16:10:59 +00:00
|
|
|
await self._position_script.async_run(
|
2019-07-31 19:25:30 +00:00
|
|
|
{"position": self._position}, context=self._context
|
|
|
|
)
|
2017-08-25 10:33:53 +00:00
|
|
|
if self._optimistic:
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-06-30 15:24:29 +00:00
|
|
|
|
2018-06-30 16:10:59 +00:00
|
|
|
async def async_open_cover_tilt(self, **kwargs):
|
2017-06-30 15:24:29 +00:00
|
|
|
"""Tilt the cover open."""
|
|
|
|
self._tilt_value = 100
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._tilt_script.async_run(
|
2019-07-31 19:25:30 +00:00
|
|
|
{"tilt": self._tilt_value}, context=self._context
|
|
|
|
)
|
2017-08-25 10:33:53 +00:00
|
|
|
if self._tilt_optimistic:
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-06-30 15:24:29 +00:00
|
|
|
|
2018-06-30 16:10:59 +00:00
|
|
|
async def async_close_cover_tilt(self, **kwargs):
|
2017-06-30 15:24:29 +00:00
|
|
|
"""Tilt the cover closed."""
|
|
|
|
self._tilt_value = 0
|
2018-06-30 16:10:59 +00:00
|
|
|
await self._tilt_script.async_run(
|
2019-07-31 19:25:30 +00:00
|
|
|
{"tilt": self._tilt_value}, context=self._context
|
|
|
|
)
|
2017-08-25 10:33:53 +00:00
|
|
|
if self._tilt_optimistic:
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-06-30 15:24:29 +00:00
|
|
|
|
2018-06-30 16:10:59 +00:00
|
|
|
async def async_set_cover_tilt_position(self, **kwargs):
|
2017-06-30 15:24:29 +00:00
|
|
|
"""Move the cover tilt to a specific position."""
|
|
|
|
self._tilt_value = kwargs[ATTR_TILT_POSITION]
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._tilt_script.async_run(
|
2019-07-31 19:25:30 +00:00
|
|
|
{"tilt": self._tilt_value}, context=self._context
|
|
|
|
)
|
2017-08-25 10:33:53 +00:00
|
|
|
if self._tilt_optimistic:
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-06-30 15:24:29 +00:00
|
|
|
|
2018-06-30 16:10:59 +00:00
|
|
|
async def async_update(self):
|
2017-06-30 15:24:29 +00:00
|
|
|
"""Update the state from the template."""
|
|
|
|
if self._template is not None:
|
|
|
|
try:
|
|
|
|
state = self._template.async_render().lower()
|
|
|
|
if state in _VALID_STATES:
|
2019-07-31 19:25:30 +00:00
|
|
|
if state in ("true", STATE_OPEN):
|
2017-06-30 15:24:29 +00:00
|
|
|
self._position = 100
|
|
|
|
else:
|
|
|
|
self._position = 0
|
|
|
|
else:
|
|
|
|
_LOGGER.error(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Received invalid cover is_on state: %s. Expected: %s",
|
|
|
|
state,
|
|
|
|
", ".join(_VALID_STATES),
|
|
|
|
)
|
2017-06-30 15:24:29 +00:00
|
|
|
self._position = None
|
|
|
|
except TemplateError as ex:
|
|
|
|
_LOGGER.error(ex)
|
|
|
|
self._position = None
|
|
|
|
if self._position_template is not None:
|
|
|
|
try:
|
|
|
|
state = float(self._position_template.async_render())
|
|
|
|
if state < 0 or state > 100:
|
|
|
|
self._position = None
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Cover position value must be"
|
|
|
|
" between 0 and 100."
|
|
|
|
" Value was: %.2f",
|
|
|
|
state,
|
|
|
|
)
|
2017-06-30 15:24:29 +00:00
|
|
|
else:
|
|
|
|
self._position = state
|
2019-09-28 11:53:16 +00:00
|
|
|
except (TemplateError, ValueError) as err:
|
|
|
|
_LOGGER.error(err)
|
2017-06-30 15:24:29 +00:00
|
|
|
self._position = None
|
|
|
|
if self._tilt_template is not None:
|
|
|
|
try:
|
|
|
|
state = float(self._tilt_template.async_render())
|
|
|
|
if state < 0 or state > 100:
|
|
|
|
self._tilt_value = None
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
2020-01-02 19:17:10 +00:00
|
|
|
"Tilt value must be between 0 and 100. Value was: %.2f", state,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-06-30 15:24:29 +00:00
|
|
|
else:
|
|
|
|
self._tilt_value = state
|
2019-09-28 11:53:16 +00:00
|
|
|
except (TemplateError, ValueError) as err:
|
|
|
|
_LOGGER.error(err)
|
2017-06-30 15:24:29 +00:00
|
|
|
self._tilt_value = None
|
2017-10-30 16:28:37 +00:00
|
|
|
|
|
|
|
for property_name, template in (
|
2019-07-31 19:25:30 +00:00
|
|
|
("_icon", self._icon_template),
|
|
|
|
("_entity_picture", self._entity_picture_template),
|
2019-09-28 11:53:16 +00:00
|
|
|
("_available", self._availability_template),
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2017-10-30 16:28:37 +00:00
|
|
|
if template is None:
|
|
|
|
continue
|
|
|
|
|
2017-06-30 15:24:29 +00:00
|
|
|
try:
|
2019-09-28 11:53:16 +00:00
|
|
|
value = template.async_render()
|
|
|
|
if property_name == "_available":
|
|
|
|
value = value.lower() == "true"
|
|
|
|
setattr(self, property_name, value)
|
2017-06-30 15:24:29 +00:00
|
|
|
except TemplateError as ex:
|
2019-07-31 19:25:30 +00:00
|
|
|
friendly_property_name = property_name[1:].replace("_", " ")
|
2017-06-30 15:24:29 +00:00
|
|
|
if ex.args and ex.args[0].startswith(
|
2019-07-31 19:25:30 +00:00
|
|
|
"UndefinedError: 'None' has no attribute"
|
|
|
|
):
|
2017-06-30 15:24:29 +00:00
|
|
|
# Common during HA startup - so just a warning
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning(
|
2020-01-02 19:17:10 +00:00
|
|
|
"Could not render %s template %s, the state is unknown.",
|
2019-07-31 19:25:30 +00:00
|
|
|
friendly_property_name,
|
|
|
|
self._name,
|
|
|
|
)
|
2017-06-30 15:24:29 +00:00
|
|
|
return
|
2017-10-30 16:28:37 +00:00
|
|
|
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
setattr(self, property_name, getattr(super(), property_name))
|
2017-10-30 16:28:37 +00:00
|
|
|
except AttributeError:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Could not render %s template %s: %s",
|
|
|
|
friendly_property_name,
|
|
|
|
self._name,
|
|
|
|
ex,
|
|
|
|
)
|