diff --git a/homeassistant/helpers/trigger_template_entity.py b/homeassistant/helpers/trigger_template_entity.py index 593f15b31ec..c1e76882765 100644 --- a/homeassistant/helpers/trigger_template_entity.py +++ b/homeassistant/helpers/trigger_template_entity.py @@ -186,11 +186,6 @@ class TriggerBaseEntity(Entity): variables, parse_result=key in self._parse_result, ) - elif key in self._to_render_complex: - rendered[key] = render_complex( - self._config[key], - variables, - ) except TemplateError as err: logging.getLogger(f"{__package__}.{self.entity_id.split('.')[0]}").error( "Error rendering %s template for %s: %s", key, self.entity_id, err diff --git a/tests/helpers/test_trigger_template_entity.py b/tests/helpers/test_trigger_template_entity.py index 0f7af9bdc46..a23fd5cc4bc 100644 --- a/tests/helpers/test_trigger_template_entity.py +++ b/tests/helpers/test_trigger_template_entity.py @@ -1,5 +1,9 @@ """Test template trigger entity.""" +from typing import Any + +import pytest + from homeassistant.core import HomeAssistant from homeassistant.helpers import template from homeassistant.helpers.trigger_template_entity import ManualTriggerEntity @@ -41,7 +45,7 @@ async def test_template_entity_requires_hass_set(hass: HomeAssistant) -> None: async def test_trigger_template_availability(hass: HomeAssistant) -> None: - """Test manual trigger template entity.""" + """Test manual trigger template entity availability template.""" config = { "name": template.Template("test_entity", hass), "icon": template.Template( @@ -86,3 +90,71 @@ async def test_trigger_template_availability(hass: HomeAssistant) -> None: assert entity.icon == "mdi:off" assert entity.entity_picture == "/local/picture_off" assert entity.available is False + + +async def test_trigger_template_availability_fails( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test manual trigger template entity when availability render fails.""" + config = { + "name": template.Template("test_entity", hass), + "icon": template.Template( + '{% if value=="on" %} mdi:on {% else %} mdi:off {% endif %}', hass + ), + "picture": template.Template( + '{% if value=="on" %} /local/picture_on {% else %} /local/picture_off {% endif %}', + hass, + ), + "availability": template.Template("{{ incorrect ", hass), + } + + entity = ManualTriggerEntity(hass, config) + entity.entity_id = "test.entity" + hass.states.async_set("test.entity", "on") + await entity.async_added_to_hass() + + entity._process_manual_data("on") + await hass.async_block_till_done() + + assert "Error rendering availability template for test.entity" in caplog.text + + +async def test_trigger_template_complex(hass: HomeAssistant) -> None: + """Test manual trigger template entity complex template.""" + complex_template = """ + {% set d = {'test_key':'test_data'} %} + {{ dict(d) }} + +""" + config = { + "name": template.Template("test_entity", hass), + "icon": template.Template( + '{% if value=="on" %} mdi:on {% else %} mdi:off {% endif %}', hass + ), + "picture": template.Template( + '{% if value=="on" %} /local/picture_on {% else %} /local/picture_off {% endif %}', + hass, + ), + "availability": template.Template('{{ has_value("test.entity") }}', hass), + "other_key": template.Template(complex_template, hass), + } + + class TestEntity(ManualTriggerEntity): + """Test entity class.""" + + extra_template_keys_complex = ("other_key",) + + @property + def some_other_key(self) -> dict[str, Any] | None: + """Return extra attributes.""" + return self._rendered.get("other_key") + + entity = TestEntity(hass, config) + entity.entity_id = "test.entity" + hass.states.async_set("test.entity", "on") + await entity.async_added_to_hass() + + entity._process_manual_data("on") + await hass.async_block_till_done() + + assert entity.some_other_key == {"test_key": "test_data"}