Add type hints to template helper (#81308)

* Add type hints to template helper

* Update homeassistant/helpers/template.py

* Adjust use of ensure_compiled
pull/81352/head^2
epenet 2022-11-07 09:41:53 +01:00 committed by GitHub
parent 9bc029000a
commit 5e05739019
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 7 deletions

View File

@ -559,8 +559,11 @@ class Template:
@callback
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.
If valid JSON will expose value_json too.
@ -570,8 +573,7 @@ class Template:
if self.is_static:
return self.template
if self._compiled is None:
self._ensure_compiled()
compiled = self._compiled or self._ensure_compiled()
variables = dict(variables or {})
variables["value"] = value
@ -580,9 +582,7 @@ class Template:
variables["value_json"] = json_loads(value)
try:
return _render_with_context(
self.template, self._compiled, **variables
).strip()
return _render_with_context(self.template, compiled, **variables).strip()
except jinja2.TemplateError as ex:
if error_value is _SENTINEL:
_LOGGER.error(