From 1f5fb8f28aefebe0ae0cf42f0da5a901c2f994e8 Mon Sep 17 00:00:00 2001
From: Anders Melchiorsen <amelchio@nogoto.net>
Date: Thu, 11 Feb 2021 10:30:09 +0100
Subject: [PATCH] Raise ConditionError for template errors (#46245)

---
 homeassistant/helpers/condition.py |  3 +--
 tests/helpers/test_condition.py    | 13 ++++---------
 2 files changed, 5 insertions(+), 11 deletions(-)

diff --git a/homeassistant/helpers/condition.py b/homeassistant/helpers/condition.py
index 126513608c7..da4fdab07d7 100644
--- a/homeassistant/helpers/condition.py
+++ b/homeassistant/helpers/condition.py
@@ -475,8 +475,7 @@ def async_template(
     try:
         value: str = value_template.async_render(variables, parse_result=False)
     except TemplateError as ex:
-        _LOGGER.error("Error during template condition: %s", ex)
-        return False
+        raise ConditionError(f"Error in 'template' condition: {ex}") from ex
 
     return value.lower() == "true"
 
diff --git a/tests/helpers/test_condition.py b/tests/helpers/test_condition.py
index 485c51a8bb7..dd1abdecb72 100644
--- a/tests/helpers/test_condition.py
+++ b/tests/helpers/test_condition.py
@@ -1,5 +1,5 @@
 """Test the condition helper."""
-from logging import ERROR, WARNING
+from logging import WARNING
 from unittest.mock import patch
 
 import pytest
@@ -1041,19 +1041,14 @@ async def test_extract_devices():
     )
 
 
-async def test_condition_template_error(hass, caplog):
+async def test_condition_template_error(hass):
     """Test invalid template."""
-    caplog.set_level(ERROR)
-
     test = await condition.async_from_config(
         hass, {"condition": "template", "value_template": "{{ undefined.state }}"}
     )
 
-    assert not test(hass)
-    assert len(caplog.records) == 1
-    assert caplog.records[0].message.startswith(
-        "Error during template condition: UndefinedError:"
-    )
+    with pytest.raises(ConditionError, match="template"):
+        test(hass)
 
 
 async def test_condition_template_invalid_results(hass):
-- 
GitLab