Adds template function state_attr to get attribute from a state (#13378)

* Adds template function state_attr to get attribute from a state
Refactored is_state_attr to use new function
Adds tests for state_attr

* Fixes line too long and test bug

* Fixes pylint error

* Fixes tests and D401 lint error
This commit is contained in:
Mikael Svensson authored and Paulus Schoutsen committed 2018-03-28 00:04:18 -07:00
1 parent 00c6df54b2
commit bdb4d754ae
2 files changed
+23 -3

No files matched your search

+13
View File
@@ -397,6 +397,19 @@ class TestHelpersTemplate(unittest.TestCase):
""", self.hass)
self.assertEqual('False', tpl.render())
def test_state_attr(self):
"""Test state_attr method."""
self.hass.states.set('test.object', 'available', {'mode': 'on'})
tpl = template.Template("""
{% if state_attr("test.object", "mode") == "on" %}yes{% else %}no{% endif %}
""", self.hass)
self.assertEqual('yes', tpl.render())
tpl = template.Template("""
{{ state_attr("test.noobject", "mode") == None }}
""", self.hass)
self.assertEqual('True', tpl.render())
def test_states_function(self):
"""Test using states as a function."""
self.hass.states.set('test.object', 'available')