diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index ac326711581..c771992caa4 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -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 diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index 41d252177a4..cc06c0fd19c 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -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)