mirror of
https://github.com/home-assistant/core.git
synced 2025-04-27 02:37:50 +00:00
Add shortcuts when we know template is static (#39208)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
e61f7f0274
commit
fefa1a7259
@ -213,6 +213,7 @@ class Template:
|
|||||||
self._compiled_code = None
|
self._compiled_code = None
|
||||||
self._compiled = None
|
self._compiled = None
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
|
self.is_static = not is_template_string(template)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _env(self):
|
def _env(self):
|
||||||
@ -237,10 +238,16 @@ class Template:
|
|||||||
self, variables: TemplateVarsType = None
|
self, variables: TemplateVarsType = None
|
||||||
) -> Union[str, List[str]]:
|
) -> Union[str, List[str]]:
|
||||||
"""Extract all entities for state_changed listener."""
|
"""Extract all entities for state_changed listener."""
|
||||||
|
if self.is_static:
|
||||||
|
return []
|
||||||
|
|
||||||
return extract_entities(self.hass, self.template, variables)
|
return extract_entities(self.hass, self.template, variables)
|
||||||
|
|
||||||
def render(self, variables: TemplateVarsType = None, **kwargs: Any) -> str:
|
def render(self, variables: TemplateVarsType = None, **kwargs: Any) -> str:
|
||||||
"""Render given template."""
|
"""Render given template."""
|
||||||
|
if self.is_static:
|
||||||
|
return self.template
|
||||||
|
|
||||||
if variables is not None:
|
if variables is not None:
|
||||||
kwargs.update(variables)
|
kwargs.update(variables)
|
||||||
|
|
||||||
@ -254,6 +261,9 @@ class Template:
|
|||||||
|
|
||||||
This method must be run in the event loop.
|
This method must be run in the event loop.
|
||||||
"""
|
"""
|
||||||
|
if self.is_static:
|
||||||
|
return self.template
|
||||||
|
|
||||||
compiled = self._compiled or self._ensure_compiled()
|
compiled = self._compiled or self._ensure_compiled()
|
||||||
|
|
||||||
if variables is not None:
|
if variables is not None:
|
||||||
@ -270,17 +280,22 @@ class Template:
|
|||||||
) -> RenderInfo:
|
) -> RenderInfo:
|
||||||
"""Render the template and collect an entity filter."""
|
"""Render the template and collect an entity filter."""
|
||||||
assert self.hass and _RENDER_INFO not in self.hass.data
|
assert self.hass and _RENDER_INFO not in self.hass.data
|
||||||
render_info = self.hass.data[_RENDER_INFO] = RenderInfo(self)
|
|
||||||
|
render_info = RenderInfo(self)
|
||||||
|
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
|
if self.is_static:
|
||||||
|
render_info._freeze_static()
|
||||||
|
return render_info
|
||||||
|
|
||||||
|
self.hass.data[_RENDER_INFO] = render_info
|
||||||
try:
|
try:
|
||||||
render_info._result = self.async_render(variables, **kwargs)
|
render_info._result = self.async_render(variables, **kwargs)
|
||||||
except TemplateError as ex:
|
except TemplateError as ex:
|
||||||
render_info.exception = ex
|
render_info.exception = ex
|
||||||
finally:
|
finally:
|
||||||
del self.hass.data[_RENDER_INFO]
|
del self.hass.data[_RENDER_INFO]
|
||||||
if not is_template_string(self.template):
|
|
||||||
render_info._freeze_static()
|
|
||||||
else:
|
|
||||||
render_info._freeze()
|
render_info._freeze()
|
||||||
return render_info
|
return render_info
|
||||||
|
|
||||||
@ -289,6 +304,9 @@ class Template:
|
|||||||
|
|
||||||
If valid JSON will expose value_json too.
|
If valid JSON will expose value_json too.
|
||||||
"""
|
"""
|
||||||
|
if self.is_static:
|
||||||
|
return self.template
|
||||||
|
|
||||||
return run_callback_threadsafe(
|
return run_callback_threadsafe(
|
||||||
self.hass.loop,
|
self.hass.loop,
|
||||||
self.async_render_with_possible_json_value,
|
self.async_render_with_possible_json_value,
|
||||||
@ -306,6 +324,9 @@ class Template:
|
|||||||
|
|
||||||
This method must be run in the event loop.
|
This method must be run in the event loop.
|
||||||
"""
|
"""
|
||||||
|
if self.is_static:
|
||||||
|
return self.template
|
||||||
|
|
||||||
if self._compiled is None:
|
if self._compiled is None:
|
||||||
self._ensure_compiled()
|
self._ensure_compiled()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user