Optimize template 2 (#3521)

* Enforce compiling templates

* Refactor templates

* Add template validator to Logbook service

* Some more fixes

* Lint

* Allow easy skipping of rfxtrx tests

* Fix template bug in AND & OR conditions

* add entities extractor

Conflicts:
	tests/helpers/test_template.py

* fix unittest

* Convert template to be async

* Fix Farcy

* Lint fix

* Limit template updates to related entities

* Make template automation async
This commit is contained in:
Paulus Schoutsen 2016-09-27 21:29:55 -07:00 committed by GitHub
parent 6694b0470e
commit 00e298206e
52 changed files with 841 additions and 562 deletions

View file

@ -6,7 +6,6 @@ from urllib.parse import urlparse
from typing import Any, Union, TypeVar, Callable, Sequence, Dict
import jinja2
import voluptuous as vol
from homeassistant.loader import get_platform
@ -16,8 +15,10 @@ from homeassistant.const import (
CONF_CONDITION, CONF_BELOW, CONF_ABOVE, SUN_EVENT_SUNSET,
SUN_EVENT_SUNRISE, CONF_UNIT_SYSTEM_IMPERIAL, CONF_UNIT_SYSTEM_METRIC)
from homeassistant.core import valid_entity_id
from homeassistant.exceptions import TemplateError
import homeassistant.util.dt as dt_util
from homeassistant.util import slugify
from homeassistant.helpers import template as template_helper
# pylint: disable=invalid-name
@ -103,6 +104,11 @@ def entity_ids(value: Union[str, Sequence]) -> Sequence[str]:
return [entity_id(ent_id) for ent_id in value]
def enum(enumClass):
"""Create validator for specified enum."""
return vol.All(vol.In(enumClass.__members__), enumClass.__getitem__)
def icon(value):
"""Validate icon."""
value = str(value)
@ -234,14 +240,15 @@ def template(value):
"""Validate a jinja2 template."""
if value is None:
raise vol.Invalid('template value is None')
if isinstance(value, (list, dict)):
elif isinstance(value, (list, dict, template_helper.Template)):
raise vol.Invalid('template value should be a string')
value = str(value)
value = template_helper.Template(str(value))
try:
jinja2.Environment().parse(value)
value.ensure_valid()
return value
except jinja2.exceptions.TemplateSyntaxError as ex:
except TemplateError as ex:
raise vol.Invalid('invalid template ({})'.format(ex))