mirror of
https://github.com/home-assistant/core.git
synced 2025-08-07 14:45:09 +02:00
Add template is_state method
This commit is contained in:
@@ -41,8 +41,9 @@ def render(hass, template, variables=None, **kwargs):
|
||||
|
||||
try:
|
||||
return ENV.from_string(template, {
|
||||
'states': AllStates(hass)
|
||||
}).render(kwargs)
|
||||
'states': AllStates(hass),
|
||||
'is_state': hass.states.is_state
|
||||
}).render(kwargs).strip()
|
||||
except jinja2.TemplateError as err:
|
||||
raise TemplateError(err)
|
||||
|
||||
|
@@ -100,3 +100,11 @@ class TestUtilTemplate(unittest.TestCase):
|
||||
def test_raise_exception_on_error(self):
|
||||
with self.assertRaises(TemplateError):
|
||||
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 %}'))
|
||||
|
Reference in New Issue
Block a user