mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 00:37:53 +00:00
Ensure static templates are still called back on first refresh (#39753)
This commit is contained in:
parent
fc2cddc452
commit
90d574e521
@ -245,7 +245,7 @@ class Template:
|
||||
def render(self, variables: TemplateVarsType = None, **kwargs: Any) -> str:
|
||||
"""Render given template."""
|
||||
if self.is_static:
|
||||
return self.template
|
||||
return self.template.strip()
|
||||
|
||||
if variables is not None:
|
||||
kwargs.update(variables)
|
||||
@ -261,7 +261,7 @@ class Template:
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
if self.is_static:
|
||||
return self.template
|
||||
return self.template.strip()
|
||||
|
||||
compiled = self._compiled or self._ensure_compiled()
|
||||
|
||||
@ -284,6 +284,7 @@ class Template:
|
||||
|
||||
# pylint: disable=protected-access
|
||||
if self.is_static:
|
||||
render_info._result = self.template.strip()
|
||||
render_info._freeze_static()
|
||||
return render_info
|
||||
|
||||
|
@ -1035,6 +1035,46 @@ async def test_track_template_result_errors(hass, caplog):
|
||||
assert isinstance(not_exist_runs[2][3], TemplateError)
|
||||
|
||||
|
||||
async def test_static_string(hass):
|
||||
"""Test a static string."""
|
||||
template_refresh = Template("{{ 'static' }}", hass)
|
||||
|
||||
refresh_runs = []
|
||||
|
||||
@ha.callback
|
||||
def refresh_listener(event, updates):
|
||||
refresh_runs.append(updates.pop().result)
|
||||
|
||||
info = async_track_template_result(
|
||||
hass, [TrackTemplate(template_refresh, None)], refresh_listener
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
info.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert refresh_runs == ["static"]
|
||||
|
||||
|
||||
async def test_string(hass):
|
||||
"""Test a string."""
|
||||
template_refresh = Template("no_template", hass)
|
||||
|
||||
refresh_runs = []
|
||||
|
||||
@ha.callback
|
||||
def refresh_listener(event, updates):
|
||||
refresh_runs.append(updates.pop().result)
|
||||
|
||||
info = async_track_template_result(
|
||||
hass, [TrackTemplate(template_refresh, None)], refresh_listener
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
info.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert refresh_runs == ["no_template"]
|
||||
|
||||
|
||||
async def test_track_template_result_refresh_cancel(hass):
|
||||
"""Test cancelling and refreshing result."""
|
||||
template_refresh = Template("{{states.switch.test.state == 'on' and now() }}", hass)
|
||||
|
Loading…
x
Reference in New Issue
Block a user