Prevent template loop detection from triggering when all templates are self referencing ()

This commit is contained in:
J. Nick Koston 2020-10-01 15:11:11 -05:00 committed by GitHub
parent 1219c95f63
commit deedd41d62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 10 deletions
homeassistant/components/template
tests/components/template

View file

@ -234,9 +234,7 @@ class TemplateEntity(Entity):
else:
self._self_ref_update_count = 0
# If we need to make this less sensitive in the future,
# change the '>=' to a '>' here.
if self._self_ref_update_count >= len(self._template_attrs):
if self._self_ref_update_count > len(self._template_attrs):
for update in updates:
_LOGGER.warning(
"Template loop detected while processing event: %s, skipping template render for Template[%s]",

View file

@ -1121,3 +1121,48 @@ async def test_state_gets_lowercased(hass):
hass.states.async_set("binary_sensor.garage_door_sensor", "on")
await hass.async_block_till_done()
assert hass.states.get("cover.garage_door").state == STATE_CLOSED
async def test_self_referencing_icon_with_no_template_is_not_a_loop(hass, caplog):
"""Test a self referencing icon with no value template is not a loop."""
icon_template_str = """{% if is_state('cover.office', 'open') %}
mdi:window-shutter-open
{% else %}
mdi:window-shutter
{% endif %}"""
await setup.async_setup_component(
hass,
"cover",
{
"cover": {
"platform": "template",
"covers": {
"office": {
"icon_template": icon_template_str,
"open_cover": {
"service": "switch.turn_on",
"entity_id": "switch.office_blinds_up",
},
"close_cover": {
"service": "switch.turn_on",
"entity_id": "switch.office_blinds_down",
},
"stop_cover": {
"service": "switch.turn_on",
"entity_id": "switch.office_blinds_up",
},
},
},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 1
assert "Template loop detected" not in caplog.text

View file

@ -797,9 +797,9 @@ async def test_self_referencing_sensor_loop(hass, caplog):
assert "Template loop detected" in caplog.text
state = hass.states.get("sensor.test")
assert int(state.state) == 1
assert int(state.state) == 2
await hass.async_block_till_done()
assert int(state.state) == 1
assert int(state.state) == 2
async def test_self_referencing_sensor_with_icon_loop(hass, caplog):
@ -833,11 +833,11 @@ async def test_self_referencing_sensor_with_icon_loop(hass, caplog):
assert "Template loop detected" in caplog.text
state = hass.states.get("sensor.test")
assert int(state.state) == 2
assert int(state.state) == 3
assert state.attributes[ATTR_ICON] == "mdi:greater"
await hass.async_block_till_done()
assert int(state.state) == 2
assert int(state.state) == 3
async def test_self_referencing_sensor_with_icon_and_picture_entity_loop(hass, caplog):
@ -872,12 +872,12 @@ async def test_self_referencing_sensor_with_icon_and_picture_entity_loop(hass, c
assert "Template loop detected" in caplog.text
state = hass.states.get("sensor.test")
assert int(state.state) == 3
assert int(state.state) == 4
assert state.attributes[ATTR_ICON] == "mdi:less"
assert state.attributes[ATTR_ENTITY_PICTURE] == "bigpic"
await hass.async_block_till_done()
assert int(state.state) == 3
assert int(state.state) == 4
async def test_self_referencing_entity_picture_loop(hass, caplog):
@ -917,7 +917,7 @@ async def test_self_referencing_entity_picture_loop(hass, caplog):
state = hass.states.get("sensor.test")
assert int(state.state) == 1
assert state.attributes[ATTR_ENTITY_PICTURE] == "1"
assert state.attributes[ATTR_ENTITY_PICTURE] == "2"
await hass.async_block_till_done()
assert int(state.state) == 1