mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 02:37:08 +00:00
Add template is_state method
This commit is contained in:
parent
931f7e8615
commit
360b99be59
@ -41,8 +41,9 @@ def render(hass, template, variables=None, **kwargs):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
return ENV.from_string(template, {
|
return ENV.from_string(template, {
|
||||||
'states': AllStates(hass)
|
'states': AllStates(hass),
|
||||||
}).render(kwargs)
|
'is_state': hass.states.is_state
|
||||||
|
}).render(kwargs).strip()
|
||||||
except jinja2.TemplateError as err:
|
except jinja2.TemplateError as err:
|
||||||
raise TemplateError(err)
|
raise TemplateError(err)
|
||||||
|
|
||||||
|
@ -100,3 +100,11 @@ class TestUtilTemplate(unittest.TestCase):
|
|||||||
def test_raise_exception_on_error(self):
|
def test_raise_exception_on_error(self):
|
||||||
with self.assertRaises(TemplateError):
|
with self.assertRaises(TemplateError):
|
||||||
template.render(self.hass, '{{ invalid_syntax')
|
template.render(self.hass, '{{ invalid_syntax')
|
||||||
|
|
||||||
|
def test_is_state(self):
|
||||||
|
self.hass.states.set('test.object', 'available')
|
||||||
|
self.assertEqual(
|
||||||
|
'yes',
|
||||||
|
template.render(
|
||||||
|
self.hass,
|
||||||
|
'{% if is_state("test.object", "available") %}yes{% else %}no{% endif %}'))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user