Allow jinja namespace command to work. (#18011)
This commit is contained in:
parent
4d9ef9e795
commit
865ea82432
2 changed files with 23 additions and 0 deletions
|
@ -9,6 +9,7 @@ import re
|
||||||
import jinja2
|
import jinja2
|
||||||
from jinja2 import contextfilter
|
from jinja2 import contextfilter
|
||||||
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
||||||
|
from jinja2.utils import Namespace
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_LATITUDE, ATTR_LONGITUDE, ATTR_UNIT_OF_MEASUREMENT, MATCH_ALL,
|
ATTR_LATITUDE, ATTR_LONGITUDE, ATTR_UNIT_OF_MEASUREMENT, MATCH_ALL,
|
||||||
|
@ -618,6 +619,11 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
||||||
"""Test if callback is safe."""
|
"""Test if callback is safe."""
|
||||||
return isinstance(obj, AllStates) or super().is_safe_callable(obj)
|
return isinstance(obj, AllStates) or super().is_safe_callable(obj)
|
||||||
|
|
||||||
|
def is_safe_attribute(self, obj, attr, value):
|
||||||
|
"""Test if attribute is safe."""
|
||||||
|
return isinstance(obj, Namespace) or \
|
||||||
|
super().is_safe_attribute(obj, attr, value)
|
||||||
|
|
||||||
|
|
||||||
ENV = TemplateEnvironment()
|
ENV = TemplateEnvironment()
|
||||||
ENV.filters['round'] = forgiving_round
|
ENV.filters['round'] = forgiving_round
|
||||||
|
|
|
@ -963,6 +963,23 @@ class TestHelpersTemplate(unittest.TestCase):
|
||||||
"{{ is_state('media_player.' ~ where , 'playing') }}",
|
"{{ is_state('media_player.' ~ where , 'playing') }}",
|
||||||
{'where': 'livingroom'})
|
{'where': 'livingroom'})
|
||||||
|
|
||||||
|
def test_jinja_namespace(self):
|
||||||
|
"""Test Jinja's namespace command can be used."""
|
||||||
|
test_template = template.Template(
|
||||||
|
(
|
||||||
|
"{% set ns = namespace(a_key='') %}"
|
||||||
|
"{% set ns.a_key = states.sensor.dummy.state %}"
|
||||||
|
"{{ ns.a_key }}"
|
||||||
|
),
|
||||||
|
self.hass
|
||||||
|
)
|
||||||
|
|
||||||
|
self.hass.states.set('sensor.dummy', 'a value')
|
||||||
|
assert 'a value' == test_template.render()
|
||||||
|
|
||||||
|
self.hass.states.set('sensor.dummy', 'another value')
|
||||||
|
assert 'another value' == test_template.render()
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_state_with_unit(hass):
|
def test_state_with_unit(hass):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue