mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 01:08:12 +00:00
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:
parent
37f959eb02
commit
5fa8037231
@ -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.
|
||||
|
@ -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')
|
||||
|
Loading…
x
Reference in New Issue
Block a user