From f91e250e902fcfcc7b0de6853f5e5c0a354c18a3 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 23 Nov 2022 20:30:32 +0100 Subject: [PATCH] Fix TemplateError definition (#82570) --- homeassistant/exceptions.py | 7 +++++-- homeassistant/helpers/template.py | 2 +- tests/components/mqtt/test_trigger.py | 2 +- tests/test_exceptions.py | 20 +++++++++++++++++++- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/homeassistant/exceptions.py b/homeassistant/exceptions.py index 052d3de4768..4f2599aa2da 100644 --- a/homeassistant/exceptions.py +++ b/homeassistant/exceptions.py @@ -25,9 +25,12 @@ class NoEntitySpecifiedError(HomeAssistantError): class TemplateError(HomeAssistantError): """Error during template rendering.""" - def __init__(self, exception: Exception) -> None: + def __init__(self, exception: Exception | str) -> None: """Init the error.""" - super().__init__(f"{exception.__class__.__name__}: {exception}") + if isinstance(exception, str): + super().__init__(exception) + else: + super().__init__(f"{exception.__class__.__name__}: {exception}") @attr.s diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 87240d35c8f..3c76c96ec5b 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -918,7 +918,7 @@ def _state_generator( def _get_state_if_valid(hass: HomeAssistant, entity_id: str) -> TemplateState | None: state = hass.states.get(entity_id) if state is None and not valid_entity_id(entity_id): - raise TemplateError(f"Invalid entity ID '{entity_id}'") # type: ignore[arg-type] + raise TemplateError(f"Invalid entity ID '{entity_id}'") return _get_template_state_from_state(hass, entity_id, state) diff --git a/tests/components/mqtt/test_trigger.py b/tests/components/mqtt/test_trigger.py index 4c0a70707eb..bebf319d36f 100644 --- a/tests/components/mqtt/test_trigger.py +++ b/tests/components/mqtt/test_trigger.py @@ -194,7 +194,7 @@ async def test_non_allowed_templates(hass, calls, caplog): ) assert ( - "Got error 'TemplateError: str: Use of 'states' is not supported in limited templates' when setting up triggers" + "Got error 'TemplateError: Use of 'states' is not supported in limited templates' when setting up triggers" in caplog.text ) diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index e353c0dd82d..acd11caece2 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -1,12 +1,17 @@ """Test to verify that Home Assistant exceptions work.""" +from __future__ import annotations + +import pytest + from homeassistant.exceptions import ( ConditionErrorContainer, ConditionErrorIndex, ConditionErrorMessage, + TemplateError, ) -def test_conditionerror_format(): +def test_conditionerror_format() -> None: """Test ConditionError stringifiers.""" error1 = ConditionErrorMessage("test", "A test error") assert str(error1) == "In 'test' condition: A test error" @@ -43,3 +48,16 @@ In 'box' (item 2 of 2): == """In 'box': In 'test' condition: A test error""" ) + + +@pytest.mark.parametrize( + "arg, expected", + [ + ("message", "message"), + (Exception("message"), "Exception: message"), + ], +) +def test_template_message(arg: str | Exception, expected: str) -> None: + """Ensure we can create TemplateError.""" + template_error = TemplateError(arg) + assert str(template_error) == expected