Raise ConditionError for template errors (#46245)

This commit is contained in:
Anders Melchiorsen 2021-02-11 10:30:09 +01:00 committed by GitHub
parent e013ad2413
commit 1f5fb8f28a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 11 deletions

View file

@ -475,8 +475,7 @@ def async_template(
try: try:
value: str = value_template.async_render(variables, parse_result=False) value: str = value_template.async_render(variables, parse_result=False)
except TemplateError as ex: except TemplateError as ex:
_LOGGER.error("Error during template condition: %s", ex) raise ConditionError(f"Error in 'template' condition: {ex}") from ex
return False
return value.lower() == "true" return value.lower() == "true"

View file

@ -1,5 +1,5 @@
"""Test the condition helper.""" """Test the condition helper."""
from logging import ERROR, WARNING from logging import WARNING
from unittest.mock import patch from unittest.mock import patch
import pytest 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.""" """Test invalid template."""
caplog.set_level(ERROR)
test = await condition.async_from_config( test = await condition.async_from_config(
hass, {"condition": "template", "value_template": "{{ undefined.state }}"} hass, {"condition": "template", "value_template": "{{ undefined.state }}"}
) )
assert not test(hass) with pytest.raises(ConditionError, match="template"):
assert len(caplog.records) == 1 test(hass)
assert caplog.records[0].message.startswith(
"Error during template condition: UndefinedError:"
)
async def test_condition_template_invalid_results(hass): async def test_condition_template_invalid_results(hass):