Home Assistant Unofficial Reference 2024.12.1
homeassistant.helpers.template.Template Class Reference

Public Member Functions

def __eq__ (self, other)
 
int __hash__ (self)
 
None __init__ (self, str template, HomeAssistant|None hass=None)
 
str __repr__ (self)
 
Any async_render (self, TemplateVarsType variables=None, bool parse_result=True, bool limited=False, bool strict=False, Callable[[int, str], None]|None log_fn=None, **Any kwargs)
 
RenderInfo async_render_to_info (self, TemplateVarsType variables=None, bool strict=False, Callable[[int, str], None]|None log_fn=None, **Any kwargs)
 
bool async_render_will_timeout (self, float timeout, TemplateVarsType variables=None, bool strict=False, Callable[[int, str], None]|None log_fn=None, **Any kwargs)
 
Any async_render_with_possible_json_value (self, Any value, Any error_value=_SENTINEL, dict[str, Any]|None variables=None, bool parse_result=False)
 
None ensure_valid (self)
 
Any render (self, TemplateVarsType variables=None, bool parse_result=True, bool limited=False, **Any kwargs)
 
def render_with_possible_json_value (self, value, error_value=_SENTINEL)
 

Public Attributes

 hass
 
 is_static
 
 template
 

Private Member Functions

jinja2.Template _ensure_compiled (self, bool limited=False, bool strict=False, Callable[[int, str], None]|None log_fn=None)
 
TemplateEnvironment _env (self)
 
Any _parse_result (self, str render_result)
 

Private Attributes

 __class__
 
 _compiled
 
 _compiled_code
 
 _exc_info
 
 _limited
 
 _log_fn
 
 _strict
 

Static Private Attributes

tuple __slots__
 

Detailed Description

Class to hold a template and manage caching and rendering.

Definition at line 493 of file template.py.

Constructor & Destructor Documentation

◆ __init__()

None homeassistant.helpers.template.Template.__init__ (   self,
str  template,
HomeAssistant | None   hass = None 
)
Instantiate a template.

Note: A valid hass instance should always be passed in. The hass parameter
will be non optional in Home Assistant Core 2025.10.

Definition at line 511 of file template.py.

Member Function Documentation

◆ __eq__()

def homeassistant.helpers.template.Template.__eq__ (   self,
  other 
)
Compare template with another.

Definition at line 855 of file template.py.

◆ __hash__()

int homeassistant.helpers.template.Template.__hash__ (   self)
Hash code for template.

Definition at line 863 of file template.py.

◆ __repr__()

str homeassistant.helpers.template.Template.__repr__ (   self)
Representation of Template.

Definition at line 867 of file template.py.

◆ _ensure_compiled()

jinja2.Template homeassistant.helpers.template.Template._ensure_compiled (   self,
bool   limited = False,
bool   strict = False,
Callable[[int, str], None] | None   log_fn = None 
)
private
Bind a template to a specific hass instance.

Definition at line 822 of file template.py.

◆ _env()

TemplateEnvironment homeassistant.helpers.template.Template._env (   self)
private

Definition at line 543 of file template.py.

◆ _parse_result()

Any homeassistant.helpers.template.Template._parse_result (   self,
str  render_result 
)
private
Parse the result.

Definition at line 647 of file template.py.

◆ async_render()

Any homeassistant.helpers.template.Template.async_render (   self,
TemplateVarsType   variables = None,
bool   parse_result = True,
bool   limited = False,
bool   strict = False,
Callable[[int, str], None] | None   log_fn = None,
**Any  kwargs 
)
Render given template.

This method must be run in the event loop.

If limited is True, the template is not allowed to access any function
or filter depending on hass or the state machine.

Definition at line 602 of file template.py.

◆ async_render_to_info()

RenderInfo homeassistant.helpers.template.Template.async_render_to_info (   self,
TemplateVarsType   variables = None,
bool   strict = False,
Callable[[int, str], None] | None   log_fn = None,
**Any  kwargs 
)
Render the template and collect an entity filter.

Definition at line 717 of file template.py.

◆ async_render_will_timeout()

bool homeassistant.helpers.template.Template.async_render_will_timeout (   self,
float  timeout,
TemplateVarsType   variables = None,
bool   strict = False,
Callable[[int, str], None] | None   log_fn = None,
**Any  kwargs 
)
Check to see if rendering a template will timeout during render.

This is intended to check for expensive templates
that will make the system unstable.  The template
is rendered in the executor to ensure it does not
tie up the event loop.

This function is not a security control and is only
intended to be used as a safety check when testing
templates.

This method must be run in the event loop.

Definition at line 656 of file template.py.

◆ async_render_with_possible_json_value()

Any homeassistant.helpers.template.Template.async_render_with_possible_json_value (   self,
Any  value,
Any   error_value = _SENTINEL,
dict[str, Any] | None   variables = None,
bool   parse_result = False 
)
Render template with value exposed.

If valid JSON will expose value_json too.

This method must be run in the event loop.

Definition at line 775 of file template.py.

◆ ensure_valid()

None homeassistant.helpers.template.Template.ensure_valid (   self)
Return if template is valid.

Definition at line 563 of file template.py.

◆ render()

Any homeassistant.helpers.template.Template.render (   self,
TemplateVarsType   variables = None,
bool   parse_result = True,
bool   limited = False,
**Any  kwargs 
)
Render given template.

If limited is True, the template is not allowed to access any function
or filter depending on hass or the state machine.

Definition at line 579 of file template.py.

◆ render_with_possible_json_value()

def homeassistant.helpers.template.Template.render_with_possible_json_value (   self,
  value,
  error_value = _SENTINEL 
)
Render template with value exposed.

If valid JSON will expose value_json too.

Definition at line 759 of file template.py.

Member Data Documentation

◆ __class__

homeassistant.helpers.template.Template.__class__
private

Definition at line 858 of file template.py.

◆ __slots__

tuple homeassistant.helpers.template.Template.__slots__
staticprivate
Initial value:
= (
"__weakref__",
"template",
"hass",
"is_static",
"_compiled_code",
"_compiled",
"_exc_info",
"_limited",
"_strict",
"_log_fn",
"_hash_cache",
"_renders",
)

Definition at line 496 of file template.py.

◆ _compiled

homeassistant.helpers.template.Template._compiled
private

Definition at line 849 of file template.py.

◆ _compiled_code

homeassistant.helpers.template.Template._compiled_code
private

Definition at line 569 of file template.py.

◆ _exc_info

homeassistant.helpers.template.Template._exc_info
private

Definition at line 687 of file template.py.

◆ _limited

homeassistant.helpers.template.Template._limited
private

Definition at line 833 of file template.py.

◆ _log_fn

homeassistant.helpers.template.Template._log_fn
private

Definition at line 840 of file template.py.

◆ _strict

homeassistant.helpers.template.Template._strict
private

Definition at line 836 of file template.py.

◆ hass

homeassistant.helpers.template.Template.hass

Definition at line 533 of file template.py.

◆ is_static

homeassistant.helpers.template.Template.is_static

Definition at line 534 of file template.py.

◆ template

homeassistant.helpers.template.Template.template

Definition at line 859 of file template.py.


The documentation for this class was generated from the following file: