mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add type hints to template helper (#81308)
* Add type hints to template helper * Update homeassistant/helpers/template.py * Adjust use of ensure_compiled
This commit is contained in:
parent
9bc029000a
commit
5e05739019
@ -559,8 +559,11 @@ class Template:
|
|||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_render_with_possible_json_value(
|
def async_render_with_possible_json_value(
|
||||||
self, value, error_value=_SENTINEL, variables=None
|
self,
|
||||||
):
|
value: Any,
|
||||||
|
error_value: Any = _SENTINEL,
|
||||||
|
variables: dict[str, Any] | None = None,
|
||||||
|
) -> Any:
|
||||||
"""Render template with value exposed.
|
"""Render template with value exposed.
|
||||||
|
|
||||||
If valid JSON will expose value_json too.
|
If valid JSON will expose value_json too.
|
||||||
@ -570,8 +573,7 @@ class Template:
|
|||||||
if self.is_static:
|
if self.is_static:
|
||||||
return self.template
|
return self.template
|
||||||
|
|
||||||
if self._compiled is None:
|
compiled = self._compiled or self._ensure_compiled()
|
||||||
self._ensure_compiled()
|
|
||||||
|
|
||||||
variables = dict(variables or {})
|
variables = dict(variables or {})
|
||||||
variables["value"] = value
|
variables["value"] = value
|
||||||
@ -580,9 +582,7 @@ class Template:
|
|||||||
variables["value_json"] = json_loads(value)
|
variables["value_json"] = json_loads(value)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return _render_with_context(
|
return _render_with_context(self.template, compiled, **variables).strip()
|
||||||
self.template, self._compiled, **variables
|
|
||||||
).strip()
|
|
||||||
except jinja2.TemplateError as ex:
|
except jinja2.TemplateError as ex:
|
||||||
if error_value is _SENTINEL:
|
if error_value is _SENTINEL:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user