Add availability_template to Template Light platform (#26512)

* Added availability_template to Template Light platform

* Added to test for invalid values in availability_template

* Updated AVAILABILITY_TEMPLATE Rendering error

* Moved const to package Const.py

* Fix import order (pylint)

* Moved availability_template rendering to common loop

* Removed 'Magic' string

* Cleaned up const and compare lowercase result to 'true'

* reverted _available back to boolean

* Fixed tests (async, magic values and state checks)
pull/27034/head
Gil Peeters 2019-09-28 22:01:18 +10:00 committed by Charles Garwood
parent 74196eaf8b
commit ed82ec5d8e
2 changed files with 122 additions and 3 deletions

View File

@ -28,6 +28,7 @@ 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
from .const import CONF_AVAILABILITY_TEMPLATE
_LOGGER = logging.getLogger(__name__)
_VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"]
@ -44,6 +45,7 @@ LIGHT_SCHEMA = vol.Schema(
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,
vol.Optional(CONF_LEVEL_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_LEVEL_TEMPLATE): cv.template,
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
@ -65,6 +67,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
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)
on_action = device_config[CONF_ON_ACTION]
off_action = device_config[CONF_OFF_ACTION]
level_action = device_config.get(CONF_LEVEL_ACTION)
@ -92,6 +95,11 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
if str(temp_ids) != MATCH_ALL:
template_entity_ids |= set(temp_ids)
if availability_template is not None:
temp_ids = availability_template.extract_entities()
if str(temp_ids) != MATCH_ALL:
template_entity_ids |= set(temp_ids)
if not template_entity_ids:
template_entity_ids = MATCH_ALL
@ -105,6 +113,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
state_template,
icon_template,
entity_picture_template,
availability_template,
on_action,
off_action,
level_action,
@ -132,6 +141,7 @@ class LightTemplate(Light):
state_template,
icon_template,
entity_picture_template,
availability_template,
on_action,
off_action,
level_action,
@ -147,6 +157,7 @@ class LightTemplate(Light):
self._template = state_template
self._icon_template = icon_template
self._entity_picture_template = entity_picture_template
self._availability_template = availability_template
self._on_script = Script(hass, on_action)
self._off_script = Script(hass, off_action)
self._level_script = None
@ -159,6 +170,7 @@ class LightTemplate(Light):
self._entity_picture = None
self._brightness = None
self._entities = entity_ids
self._available = True
if self._template is not None:
self._template.hass = self.hass
@ -168,6 +180,8 @@ class LightTemplate(Light):
self._icon_template.hass = self.hass
if self._entity_picture_template is not None:
self._entity_picture_template.hass = self.hass
if self._availability_template is not None:
self._availability_template.hass = self.hass
@property
def brightness(self):
@ -207,6 +221,11 @@ class LightTemplate(Light):
"""Return the entity picture to use in the frontend, if any."""
return self._entity_picture
@property
def available(self) -> bool:
"""Return if the device is available."""
return self._available
async def async_added_to_hass(self):
"""Register callbacks."""
@ -218,7 +237,11 @@ class LightTemplate(Light):
@callback
def template_light_startup(event):
"""Update template on startup."""
if self._template is not None or self._level_template is not None:
if (
self._template is not None
or self._level_template is not None
or self._availability_template is not None
):
async_track_state_change(
self.hass, self._entities, template_light_state_listener
)
@ -298,12 +321,16 @@ class LightTemplate(Light):
for property_name, template in (
("_icon", self._icon_template),
("_entity_picture", self._entity_picture_template),
("_available", self._availability_template),
):
if template is None:
continue
try:
setattr(self, property_name, template.async_render())
value = template.async_render()
if property_name == "_available":
value = value.lower() == "true"
setattr(self, property_name, value)
except TemplateError as ex:
friendly_property_name = property_name[1:].replace("_", " ")
if ex.args and ex.args[0].startswith(

View File

@ -4,13 +4,16 @@ import logging
from homeassistant.core import callback
from homeassistant import setup
from homeassistant.components.light import ATTR_BRIGHTNESS
from homeassistant.const import STATE_ON, STATE_OFF
from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE
from tests.common import get_test_home_assistant, assert_setup_component
from tests.components.light import common
_LOGGER = logging.getLogger(__name__)
# Represent for light's availability
_STATE_AVAILABILITY_BOOLEAN = "availability_boolean.state"
class TestTemplateLight:
"""Test the Template light."""
@ -774,3 +777,92 @@ class TestTemplateLight:
state = self.hass.states.get("light.test_template_light")
assert state.attributes["entity_picture"] == "/local/light.png"
async def test_available_template_with_entities(hass):
"""Test availability templates with values from other entities."""
await setup.async_setup_component(
hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"availability_template": "{{ is_state('availability_boolean.state', 'on') }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
await hass.async_start()
await hass.async_block_till_done()
# When template returns true..
hass.states.async_set(_STATE_AVAILABILITY_BOOLEAN, STATE_ON)
await hass.async_block_till_done()
# Device State should not be unavailable
assert hass.states.get("light.test_template_light").state != STATE_UNAVAILABLE
# When Availability template returns false
hass.states.async_set(_STATE_AVAILABILITY_BOOLEAN, STATE_OFF)
await hass.async_block_till_done()
# device state should be unavailable
assert hass.states.get("light.test_template_light").state == STATE_UNAVAILABLE
async def test_invalid_availability_template_keeps_component_available(hass, caplog):
"""Test that an invalid availability keeps the device available."""
await setup.async_setup_component(
hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"availability_template": "{{ x - 12 }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.get("light.test_template_light").state != STATE_UNAVAILABLE
assert ("UndefinedError: 'x' is undefined") in caplog.text