Always return True/False from is_state and is_state_attr (#7138)

* Always return True/False from is_state and is_state_attr

These functions are documented to always return True/False but the
short-circuit evaluation would return None if the entity_id did not exist.

* Reword into a single statement
This commit is contained in:
Anders Melchiorsen 2017-04-17 01:36:15 +02:00 committed by Paulus Schoutsen
parent 37f959eb02
commit 5fa8037231
2 changed files with 13 additions and 2 deletions

View File

@ -648,7 +648,7 @@ class StateMachine(object):
Async friendly.
"""
state_obj = self.get(entity_id)
return state_obj and state_obj.state == state
return state_obj is not None and state_obj.state == state
def is_state_attr(self, entity_id, name, value):
"""Test if entity exists and has a state attribute set to value.
@ -656,7 +656,8 @@ class StateMachine(object):
Async friendly.
"""
state_obj = self.get(entity_id)
return state_obj and state_obj.attributes.get(name, None) == value
return state_obj is not None and \
state_obj.attributes.get(name, None) == value
def remove(self, entity_id):
"""Remove the state of an entity.

View File

@ -314,6 +314,11 @@ class TestHelpersTemplate(unittest.TestCase):
""", self.hass)
self.assertEqual('yes', tpl.render())
tpl = template.Template("""
{{ is_state("test.noobject", "available") }}
""", self.hass)
self.assertEqual('False', tpl.render())
def test_is_state_attr(self):
"""Test is_state_attr method."""
self.hass.states.set('test.object', 'available', {'mode': 'on'})
@ -322,6 +327,11 @@ class TestHelpersTemplate(unittest.TestCase):
""", self.hass)
self.assertEqual('yes', tpl.render())
tpl = template.Template("""
{{ is_state_attr("test.noobject", "mode", "on") }}
""", self.hass)
self.assertEqual('False', tpl.render())
def test_states_function(self):
"""Test using states as a function."""
self.hass.states.set('test.object', 'available')