Add template is_state method

This commit is contained in:
Paulus Schoutsen 2015-12-12 22:19:12 -08:00
parent 931f7e8615
commit 360b99be59
2 changed files with 11 additions and 2 deletions

View File

@ -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)

View File

@ -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 %}'))