Make template states var callable
This commit is contained in:
1 parent
360b99be59
commit
f73f824e0e
2 files changed
+30
-1
No files matched your search
@@ -9,6 +9,7 @@ import json
|
||||
import logging
|
||||
import jinja2
|
||||
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.exceptions import TemplateError
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -60,6 +61,10 @@ class AllStates(object):
|
||||
return iter(sorted(self._hass.states.all(),
|
||||
key=lambda state: state.entity_id))
|
||||
|
||||
def __call__(self, entity_id):
|
||||
state = self._hass.states.get(entity_id)
|
||||
return STATE_UNKNOWN if state is None else state.state
|
||||
|
||||
|
||||
class DomainStates(object):
|
||||
""" Class to expose a specific HA domain as attributes. """
|
||||
@@ -96,6 +101,13 @@ def multiply(value, amount):
|
||||
# If value can't be converted to float
|
||||
return value
|
||||
|
||||
ENV = ImmutableSandboxedEnvironment()
|
||||
|
||||
class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
||||
""" Home Assistant template environment. """
|
||||
|
||||
def is_safe_callable(self, obj):
|
||||
return isinstance(obj, AllStates) or super().is_safe_callable(obj)
|
||||
|
||||
ENV = TemplateEnvironment()
|
||||
ENV.filters['round'] = forgiving_round
|
||||
ENV.filters['multiply'] = multiply
|
||||
Reference in new issue
Block a user