remote.set_state would log error even if not

This commit is contained in:
Paulus Schoutsen 2014-11-22 17:10:55 -08:00
parent a391bc3d3f
commit 2866437a1f
2 changed files with 13 additions and 5 deletions

View File

@ -351,7 +351,10 @@ def get_states(api):
def set_state(api, entity_id, new_state, attributes=None):
""" Tells API to update state for entity_id. """
"""
Tells API to update state for entity_id.
Returns True if success.
"""
attributes = attributes or {}
@ -363,13 +366,18 @@ def set_state(api, entity_id, new_state, attributes=None):
URL_API_STATES_ENTITY.format(entity_id),
data)
if req.status_code != 201:
if req.status_code not in (200, 201):
_LOGGER.error("Error changing state: %d - %s",
req.status_code, req.text)
return False
else:
return True
except ha.HomeAssistantError:
_LOGGER.exception("Error setting state")
return False
def is_state(api, entity_id, state):
""" Queries API to see if entity_id is specified state. """

View File

@ -217,10 +217,10 @@ class TestHTTP(unittest.TestCase):
""" Helper method that will verify our event got called. """
test_value.append(1)
self.hass.listen_once_event("test_event_with_bad_data", listener)
self.hass.listen_once_event("test_event_bad_data", listener)
req = requests.post(
_url(remote.URL_API_EVENTS_EVENT.format("test_event")),
_url(remote.URL_API_EVENTS_EVENT.format("test_event_bad_data")),
data=json.dumps('not an object'),
headers=HA_HEADERS)
@ -351,7 +351,7 @@ class TestRemoteMethods(unittest.TestCase):
def test_set_state(self):
""" Test Python API set_state. """
remote.set_state(self.api, 'test.test', 'set_test')
self.assertTrue(remote.set_state(self.api, 'test.test', 'set_test'))
self.assertEqual(self.hass.states.get('test.test').state, 'set_test')