Improve error message when template is rendered from wrong thread (#117822)

* Improve error message when template is rendered from wrong thread

* Improve error message when template is rendered from wrong thread
pull/117829/head
J. Nick Koston 2024-05-20 17:26:48 -10:00 committed by GitHub
parent 58210b1968
commit c9d1b127d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 1 deletions

View File

@ -688,10 +688,19 @@ class Template:
if self.hass and self.hass.config.debug:
self.hass.verify_event_loop_thread("async_render_to_info")
self._renders += 1
assert self.hass and _render_info.get() is None
render_info = RenderInfo(self)
if not self.hass:
raise RuntimeError(f"hass not set while rendering {self}")
if _render_info.get() is not None:
raise RuntimeError(
f"RenderInfo already set while rendering {self}, "
"this usually indicates the template is being rendered "
"in the wrong thread"
)
if self.is_static:
render_info._result = self.template.strip() # noqa: SLF001
render_info._freeze_static() # noqa: SLF001

View File

@ -119,6 +119,33 @@ def assert_result_info(
assert not hasattr(info, "_domains")
async def test_template_render_missing_hass(hass: HomeAssistant) -> None:
"""Test template render when hass is not set."""
hass.states.async_set("sensor.test", "23")
template_str = "{{ states('sensor.test') }}"
template_obj = template.Template(template_str, None)
template._render_info.set(template.RenderInfo(template_obj))
with pytest.raises(RuntimeError, match="hass not set while rendering"):
template_obj.async_render_to_info()
async def test_template_render_info_collision(hass: HomeAssistant) -> None:
"""Test template render info collision.
This usually means the template is being rendered
in the wrong thread.
"""
hass.states.async_set("sensor.test", "23")
template_str = "{{ states('sensor.test') }}"
template_obj = template.Template(template_str, None)
template_obj.hass = hass
template._render_info.set(template.RenderInfo(template_obj))
with pytest.raises(RuntimeError, match="RenderInfo already set while rendering"):
template_obj.async_render_to_info()
def test_template_equality() -> None:
"""Test template comparison and hashing."""
template_one = template.Template("{{ template_one }}")