From 5622e459805a7edca2cbcbc177cc5309fc08213a Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 24 Jan 2022 16:40:50 +0100 Subject: [PATCH] Render icon and picture templates at setup (#64838) --- .../components/template/template_entity.py | 14 +++++ .../components/template/test_binary_sensor.py | 61 +++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/homeassistant/components/template/template_entity.py b/homeassistant/components/template/template_entity.py index 5eddcd5798c..768d662b4ea 100644 --- a/homeassistant/components/template/template_entity.py +++ b/homeassistant/components/template/template_entity.py @@ -250,6 +250,20 @@ class TemplateEntity(Entity): parse_result=False ) + # Templates will not render while the entity is unavailable, try to render the + # icon and picture templates. + if self._entity_picture_template: + self._entity_picture_template.hass = hass + with contextlib.suppress(TemplateError): + self._attr_entity_picture = self._entity_picture_template.async_render( + parse_result=False + ) + + if self._icon_template: + self._icon_template.hass = hass + with contextlib.suppress(TemplateError): + self._attr_icon = self._icon_template.async_render(parse_result=False) + @callback def _update_available(self, result): if isinstance(result, TemplateError): diff --git a/tests/components/template/test_binary_sensor.py b/tests/components/template/test_binary_sensor.py index 344ae27fcaa..8f76caa2cb9 100644 --- a/tests/components/template/test_binary_sensor.py +++ b/tests/components/template/test_binary_sensor.py @@ -853,6 +853,67 @@ async def test_template_validation_error(hass, caplog, start_ha): assert state.attributes.get("icon") is None +@pytest.mark.parametrize("count", [1]) +@pytest.mark.parametrize( + "config,domain,entity_id", + [ + ( + { + "binary_sensor": { + "platform": "template", + "sensors": { + "test": { + "availability_template": "{{ is_state('sensor.bla', 'available') }}", + "entity_picture_template": "{{ 'blib' + 'blub' }}", + "icon_template": "mdi:{{ 1+2 }}", + "friendly_name": "{{ 'My custom ' + 'sensor' }}", + "value_template": "{{ true }}", + }, + }, + }, + }, + binary_sensor.DOMAIN, + "binary_sensor.test", + ), + ( + { + "template": { + "binary_sensor": { + "availability": "{{ is_state('sensor.bla', 'available') }}", + "picture": "{{ 'blib' + 'blub' }}", + "icon": "mdi:{{ 1+2 }}", + "name": "{{ 'My custom ' + 'sensor' }}", + "state": "{{ true }}", + }, + }, + }, + template.DOMAIN, + "binary_sensor.my_custom_sensor", + ), + ], +) +async def test_availability_icon_picture(hass, start_ha, entity_id): + """Test name, icon and picture templates are rendered at setup.""" + state = hass.states.get(entity_id) + assert state.state == "unavailable" + assert state.attributes == { + "entity_picture": "blibblub", + "friendly_name": "My custom sensor", + "icon": "mdi:3", + } + + hass.states.async_set("sensor.bla", "available") + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state.state == "on" + assert state.attributes == { + "entity_picture": "blibblub", + "friendly_name": "My custom sensor", + "icon": "mdi:3", + } + + @pytest.mark.parametrize("count,domain", [(2, "template")]) @pytest.mark.parametrize( "config",