Ensure static templates are still called back on first refresh (#39753)

This commit is contained in:
J. Nick Koston 2020-09-07 10:19:39 -05:00 committed by GitHub
parent fc2cddc452
commit 90d574e521
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 2 deletions

View File

@ -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

View File

@ -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)