diff --git a/homeassistant/remote.py b/homeassistant/remote.py index f3be9b8075b..6c48cb96b0a 100644 --- a/homeassistant/remote.py +++ b/homeassistant/remote.py @@ -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. """ diff --git a/homeassistant/test.py b/homeassistant/test.py index dd78a34024a..e60da7f67db 100644 --- a/homeassistant/test.py +++ b/homeassistant/test.py @@ -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')