Sensor template don't exit early on TemplateError (#13041)

* Sensor template don't exit early on TemplateError

* Add friendly name unknown state test

* Also track entites from attribute templates

* Use set instead of list
pull/13079/head
Otto Winter 2018-03-11 05:45:32 +01:00 committed by Paulus Schoutsen
parent 458598546d
commit d74a2b68c1
2 changed files with 53 additions and 15 deletions

View File

@ -14,7 +14,8 @@ from homeassistant.components.sensor import ENTITY_ID_FORMAT, PLATFORM_SCHEMA
from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE,
CONF_ICON_TEMPLATE, CONF_ENTITY_PICTURE_TEMPLATE, ATTR_ENTITY_ID,
CONF_SENSORS, EVENT_HOMEASSISTANT_START, CONF_FRIENDLY_NAME_TEMPLATE)
CONF_SENSORS, EVENT_HOMEASSISTANT_START, CONF_FRIENDLY_NAME_TEMPLATE,
MATCH_ALL)
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity, async_generate_entity_id
@ -48,22 +49,32 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
icon_template = device_config.get(CONF_ICON_TEMPLATE)
entity_picture_template = device_config.get(
CONF_ENTITY_PICTURE_TEMPLATE)
entity_ids = (device_config.get(ATTR_ENTITY_ID) or
state_template.extract_entities())
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
friendly_name_template = device_config.get(CONF_FRIENDLY_NAME_TEMPLATE)
unit_of_measurement = device_config.get(ATTR_UNIT_OF_MEASUREMENT)
state_template.hass = hass
entity_ids = set()
manual_entity_ids = device_config.get(ATTR_ENTITY_ID)
if icon_template is not None:
icon_template.hass = hass
for template in (state_template, icon_template,
entity_picture_template, friendly_name_template):
if template is None:
continue
template.hass = hass
if entity_picture_template is not None:
entity_picture_template.hass = hass
if entity_ids == MATCH_ALL or manual_entity_ids is not None:
continue
if friendly_name_template is not None:
friendly_name_template.hass = hass
template_entity_ids = template.extract_entities()
if template_entity_ids == MATCH_ALL:
entity_ids = MATCH_ALL
else:
entity_ids |= set(template_entity_ids)
if manual_entity_ids is not None:
entity_ids = manual_entity_ids
elif entity_ids != MATCH_ALL:
entity_ids = list(entity_ids)
sensors.append(
SensorTemplate(
@ -166,10 +177,10 @@ class SensorTemplate(Entity):
# Common during HA startup - so just a warning
_LOGGER.warning('Could not render template %s,'
' the state is unknown.', self._name)
return
self._state = None
_LOGGER.error('Could not render template %s: %s', self._name, ex)
else:
self._state = None
_LOGGER.error('Could not render template %s: %s', self._name,
ex)
for property_name, template in (
('_icon', self._icon_template),
('_entity_picture', self._entity_picture_template),
@ -187,7 +198,7 @@ class SensorTemplate(Entity):
_LOGGER.warning('Could not render %s template %s,'
' the state is unknown.',
friendly_property_name, self._name)
return
continue
try:
setattr(self, property_name,

View File

@ -131,6 +131,33 @@ class TestTemplateSensor:
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes['friendly_name'] == 'It Works.'
def test_friendly_name_template_with_unknown_state(self):
"""Test friendly_name template with an unknown value_template."""
with assert_setup_component(1):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test_template_sensor': {
'value_template': "{{ states.fourohfour.state }}",
'friendly_name_template':
"It {{ states.sensor.test_state.state }}."
}
}
}
})
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes['friendly_name'] == 'It .'
self.hass.states.set('sensor.test_state', 'Works')
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes['friendly_name'] == 'It Works.'
def test_template_syntax_error(self):
"""Test templating syntax error."""
with assert_setup_component(0):