diff --git a/homeassistant/components/template/template_entity.py b/homeassistant/components/template/template_entity.py index 53d9d2f1a33..49b0edfab02 100644 --- a/homeassistant/components/template/template_entity.py +++ b/homeassistant/components/template/template_entity.py @@ -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]", diff --git a/tests/components/template/test_cover.py b/tests/components/template/test_cover.py index b068f04b87b..7202d8a3a1b 100644 --- a/tests/components/template/test_cover.py +++ b/tests/components/template/test_cover.py @@ -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 diff --git a/tests/components/template/test_sensor.py b/tests/components/template/test_sensor.py index 23d20250f30..8f582f01ef5 100644 --- a/tests/components/template/test_sensor.py +++ b/tests/components/template/test_sensor.py @@ -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