"""Template helper methods for rendering strings with Home Assistant data.""" from __future__ import annotations from ast import literal_eval import asyncio import base64 import collections.abc from datetime import datetime, timedelta from functools import partial, wraps import json import logging import math from operator import attrgetter import random import re from typing import Any, Generator, Iterable, cast from urllib.parse import urlencode as urllib_urlencode import weakref import jinja2 from jinja2 import contextfilter, contextfunction from jinja2.sandbox import ImmutableSandboxedEnvironment from jinja2.utils import Namespace # type: ignore import voluptuous as vol from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_LATITUDE, ATTR_LONGITUDE, ATTR_UNIT_OF_MEASUREMENT, LENGTH_METERS, STATE_UNKNOWN, ) from homeassistant.core import State, callback, split_entity_id, valid_entity_id from homeassistant.exceptions import TemplateError from homeassistant.helpers import entity_registry, location as loc_helper from homeassistant.helpers.typing import HomeAssistantType, TemplateVarsType from homeassistant.loader import bind_hass from homeassistant.util import convert, dt as dt_util, location as loc_util from homeassistant.util.async_ import run_callback_threadsafe from homeassistant.util.thread import ThreadWithException # mypy: allow-untyped-defs, no-check-untyped-defs _LOGGER = logging.getLogger(__name__) _SENTINEL = object() DATE_STR_FORMAT = "%Y-%m-%d %H:%M:%S" _RENDER_INFO = "template.render_info" _ENVIRONMENT = "template.environment" _ENVIRONMENT_LIMITED = "template.environment_limited" _RE_JINJA_DELIMITERS = re.compile(r"\{%|\{\{|\{#") # Match "simple" ints and floats. -1.0, 1, +5, 5.0 _IS_NUMERIC = re.compile(r"^[+-]?(?!0\d)\d*(?:\.\d*)?$") _RESERVED_NAMES = {"contextfunction", "evalcontextfunction", "environmentfunction"} _GROUP_DOMAIN_PREFIX = "group." _COLLECTABLE_STATE_ATTRIBUTES = { "state", "attributes", "last_changed", "last_updated", "context", "domain", "object_id", "name", } ALL_STATES_RATE_LIMIT = timedelta(minutes=1) DOMAIN_STATES_RATE_LIMIT = timedelta(seconds=1) @bind_hass def attach(hass: HomeAssistantType, obj: Any) -> None: """Recursively attach hass to all template instances in list and dict.""" if isinstance(obj, list): for child in obj: attach(hass, child) elif isinstance(obj, collections.abc.Mapping): for child_key, child_value in obj.items(): attach(hass, child_key) attach(hass, child_value) elif isinstance(obj, Template): obj.hass = hass def render_complex( value: Any, variables: TemplateVarsType = None, limited: bool = False ) -> Any: """Recursive template creator helper function.""" if isinstance(value, list): return [render_complex(item, variables) for item in value] if isinstance(value, collections.abc.Mapping): return { render_complex(key, variables): render_complex(item, variables) for key, item in value.items() } if isinstance(value, Template): return value.async_render(variables, limited=limited) return value def is_complex(value: Any) -> bool: """Test if data structure is a complex template.""" if isinstance(value, Template): return True if isinstance(value, list): return any(is_complex(val) for val in value) if isinstance(value, collections.abc.Mapping): return any(is_complex(val) for val in value.keys()) or any( is_complex(val) for val in value.values() ) return False def is_template_string(maybe_template: str) -> bool: """Check if the input is a Jinja2 template.""" return _RE_JINJA_DELIMITERS.search(maybe_template) is not None class ResultWrapper: """Result wrapper class to store render result.""" render_result: str | None def gen_result_wrapper(kls): """Generate a result wrapper.""" class Wrapper(kls, ResultWrapper): """Wrapper of a kls that can store render_result.""" def __init__(self, *args: tuple, render_result: str | None = None) -> None: super().__init__(*args) self.render_result = render_result def __str__(self) -> str: if self.render_result is None: # Can't get set repr to work if kls is set: return str(set(self)) return cast(str, kls.__str__(self)) return self.render_result return Wrapper class TupleWrapper(tuple, ResultWrapper): """Wrap a tuple.""" # This is all magic to be allowed to subclass a tuple. def __new__(cls, value: tuple, *, render_result: str | None = None) -> TupleWrapper: """Create a new tuple class.""" return super().__new__(cls, tuple(value)) # pylint: disable=super-init-not-called def __init__(self, value: tuple, *, render_result: str | None = None): """Initialize a new tuple class.""" self.render_result = render_result def __str__(self) -> str: """Return string representation.""" if self.render_result is None: return super().__str__() return self.render_result RESULT_WRAPPERS: dict[type, type] = { kls: gen_result_wrapper(kls) # type: ignore[no-untyped-call] for kls in (list, dict, set) } RESULT_WRAPPERS[tuple] = TupleWrapper def _true(arg: Any) -> bool: return True def _false(arg: Any) -> bool: return False class RenderInfo: """Holds information about a template render.""" def __init__(self, template): """Initialise.""" self.template = template # Will be set sensibly once frozen. self.filter_lifecycle = _true self.filter = _true self._result: str | None = None self.is_static = False self.exception: TemplateError | None = None self.all_states = False self.all_states_lifecycle = False self.domains = set() self.domains_lifecycle = set() self.entities = set() self.rate_limit: timedelta | None = None self.has_time = False def __repr__(self) -> str: """Representation of RenderInfo.""" return f" has_time={self.has_time}" def _filter_domains_and_entities(self, entity_id: str) -> bool: """Template should re-render if the entity state changes when we match specific domains or entities.""" return ( split_entity_id(entity_id)[0] in self.domains or entity_id in self.entities ) def _filter_entities(self, entity_id: str) -> bool: """Template should re-render if the entity state changes when we match specific entities.""" return entity_id in self.entities def _filter_lifecycle_domains(self, entity_id: str) -> bool: """Template should re-render if the entity is added or removed with domains watched.""" return split_entity_id(entity_id)[0] in self.domains_lifecycle def result(self) -> str: """Results of the template computation.""" if self.exception is not None: raise self.exception return cast(str, self._result) def _freeze_static(self) -> None: self.is_static = True self._freeze_sets() self.all_states = False def _freeze_sets(self) -> None: self.entities = frozenset(self.entities) self.domains = frozenset(self.domains) self.domains_lifecycle = frozenset(self.domains_lifecycle) def _freeze(self) -> None: self._freeze_sets() if self.rate_limit is None: if self.all_states or self.exception: self.rate_limit = ALL_STATES_RATE_LIMIT elif self.domains or self.domains_lifecycle: self.rate_limit = DOMAIN_STATES_RATE_LIMIT if self.exception: return if not self.all_states_lifecycle: if self.domains_lifecycle: self.filter_lifecycle = self._filter_lifecycle_domains else: self.filter_lifecycle = _false if self.all_states: return if self.domains: self.filter = self._filter_domains_and_entities elif self.entities: self.filter = self._filter_entities else: self.filter = _false class Template: """Class to hold a template and manage caching and rendering.""" __slots__ = ( "__weakref__", "template", "hass", "is_static", "_compiled_code", "_compiled", "_limited", ) def __init__(self, template, hass=None): """Instantiate a template.""" if not isinstance(template, str): raise TypeError("Expected template to be a string") self.template: str = template.strip() self._compiled_code = None self._compiled: Template | None = None self.hass = hass self.is_static = not is_template_string(template) self._limited = None @property def _env(self) -> TemplateEnvironment: if self.hass is None: return _NO_HASS_ENV wanted_env = _ENVIRONMENT_LIMITED if self._limited else _ENVIRONMENT ret: TemplateEnvironment | None = self.hass.data.get(wanted_env) if ret is None: ret = self.hass.data[wanted_env] = TemplateEnvironment(self.hass, self._limited) # type: ignore[no-untyped-call] return ret def ensure_valid(self) -> None: """Return if template is valid.""" if self._compiled_code is not None: return try: self._compiled_code = self._env.compile(self.template) # type: ignore[no-untyped-call] except jinja2.TemplateError as err: raise TemplateError(err) from err def render( self, variables: TemplateVarsType = None, parse_result: bool = True, limited: bool = False, **kwargs: Any, ) -> Any: """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. """ if self.is_static: if self.hass.config.legacy_templates or not parse_result: return self.template return self._parse_result(self.template) return run_callback_threadsafe( self.hass.loop, partial(self.async_render, variables, parse_result, limited, **kwargs), ).result() @callback def async_render( self, variables: TemplateVarsType = None, parse_result: bool = True, limited: bool = False, **kwargs: Any, ) -> Any: """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. """ if self.is_static: if self.hass.config.legacy_templates or not parse_result: return self.template return self._parse_result(self.template) compiled = self._compiled or self._ensure_compiled(limited) if variables is not None: kwargs.update(variables) try: render_result = compiled.render(kwargs) except Exception as err: raise TemplateError(err) from err render_result = render_result.strip() if self.hass.config.legacy_templates or not parse_result: return render_result return self._parse_result(render_result) def _parse_result(self, render_result: str) -> Any: # pylint: disable=no-self-use """Parse the result.""" try: result = literal_eval(render_result) if type(result) in RESULT_WRAPPERS: result = RESULT_WRAPPERS[type(result)]( result, render_result=render_result ) # If the literal_eval result is a string, use the original # render, by not returning right here. The evaluation of strings # resulting in strings impacts quotes, to avoid unexpected # output; use the original render instead of the evaluated one. # Complex and scientific values are also unexpected. Filter them out. if ( # Filter out string and complex numbers not isinstance(result, (str, complex)) and ( # Pass if not numeric and not a boolean not isinstance(result, (int, float)) # Or it's a boolean (inherit from int) or isinstance(result, bool) # Or if it's a digit or _IS_NUMERIC.match(render_result) is not None ) ): return result except (ValueError, TypeError, SyntaxError, MemoryError): pass return render_result async def async_render_will_timeout( self, timeout: float, variables: TemplateVarsType = None, **kwargs: Any ) -> bool: """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. """ if self.is_static: return False compiled = self._compiled or self._ensure_compiled() if variables is not None: kwargs.update(variables) finish_event = asyncio.Event() def _render_template() -> None: try: compiled.render(kwargs) except TimeoutError: pass finally: run_callback_threadsafe(self.hass.loop, finish_event.set) try: template_render_thread = ThreadWithException(target=_render_template) template_render_thread.start() await asyncio.wait_for(finish_event.wait(), timeout=timeout) except asyncio.TimeoutError: template_render_thread.raise_exc(TimeoutError) return True finally: template_render_thread.join() return False @callback def async_render_to_info( self, variables: TemplateVarsType = None, **kwargs: Any ) -> RenderInfo: """Render the template and collect an entity filter.""" assert self.hass and _RENDER_INFO not in self.hass.data render_info = RenderInfo(self) # type: ignore[no-untyped-call] # pylint: disable=protected-access if self.is_static: render_info._result = self.template.strip() render_info._freeze_static() return render_info self.hass.data[_RENDER_INFO] = render_info try: render_info._result = self.async_render(variables, **kwargs) except TemplateError as ex: render_info.exception = ex finally: del self.hass.data[_RENDER_INFO] render_info._freeze() return render_info def render_with_possible_json_value(self, value, error_value=_SENTINEL): """Render template with value exposed. If valid JSON will expose value_json too. """ if self.is_static: return self.template return run_callback_threadsafe( self.hass.loop, self.async_render_with_possible_json_value, value, error_value, ).result() @callback def async_render_with_possible_json_value( self, value, error_value=_SENTINEL, variables=None ): """Render template with value exposed. If valid JSON will expose value_json too. This method must be run in the event loop. """ if self.is_static: return self.template if self._compiled is None: self._ensure_compiled() variables = dict(variables or {}) variables["value"] = value try: variables["value_json"] = json.loads(value) except (ValueError, TypeError): pass try: return self._compiled.render(variables).strip() except jinja2.TemplateError as ex: if error_value is _SENTINEL: _LOGGER.error( "Error parsing value: %s (value: %s, template: %s)", ex, value, self.template, ) return value if error_value is _SENTINEL else error_value def _ensure_compiled(self, limited: bool = False) -> Template: """Bind a template to a specific hass instance.""" self.ensure_valid() assert self.hass is not None, "hass variable not set on template" assert ( self._limited is None or self._limited == limited ), "can't change between limited and non limited template" self._limited = limited env = self._env self._compiled = cast( Template, jinja2.Template.from_code(env, self._compiled_code, env.globals, None), ) return self._compiled def __eq__(self, other): """Compare template with another.""" return ( self.__class__ == other.__class__ and self.template == other.template and self.hass == other.hass ) def __hash__(self) -> int: """Hash code for template.""" return hash(self.template) def __repr__(self) -> str: """Representation of Template.""" return 'Template("' + self.template + '")' class AllStates: """Class to expose all HA states as attributes.""" def __init__(self, hass: HomeAssistantType) -> None: """Initialize all states.""" self._hass = hass def __getattr__(self, name): """Return the domain state.""" if "." in name: return _get_state_if_valid(self._hass, name) if name in _RESERVED_NAMES: return None if not valid_entity_id(f"{name}.entity"): raise TemplateError(f"Invalid domain name '{name}'") return DomainStates(self._hass, name) # Jinja will try __getitem__ first and it avoids the need # to call is_safe_attribute __getitem__ = __getattr__ def _collect_all(self) -> None: render_info = self._hass.data.get(_RENDER_INFO) if render_info is not None: render_info.all_states = True def _collect_all_lifecycle(self) -> None: render_info = self._hass.data.get(_RENDER_INFO) if render_info is not None: render_info.all_states_lifecycle = True def __iter__(self): """Return all states.""" self._collect_all() return _state_generator(self._hass, None) def __len__(self) -> int: """Return number of states.""" self._collect_all_lifecycle() return self._hass.states.async_entity_ids_count() def __call__(self, entity_id): """Return the states.""" state = _get_state(self._hass, entity_id) return STATE_UNKNOWN if state is None else state.state def __repr__(self) -> str: """Representation of All States.""" return "