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 threadpull/117829/head
parent
58210b1968
commit
c9d1b127d8
|
@ -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
|
||||
|
|
|
@ -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 }}")
|
||||
|
|
Loading…
Reference in New Issue