From 102690a7707d3fc7e4d52b9623611da25365f162 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 28 Oct 2013 18:45:35 -0700 Subject: [PATCH] last_changed value of State is now a string so it can be send over the API --- homeassistant/__init__.py | 5 +---- homeassistant/httpinterface.py | 8 +------- homeassistant/test.py | 23 +++++++++++++++++++++-- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/homeassistant/__init__.py b/homeassistant/__init__.py index 44793a938a6..ad974331673 100644 --- a/homeassistant/__init__.py +++ b/homeassistant/__init__.py @@ -65,12 +65,9 @@ def create_state(state, attributes=None, last_changed=None): attributes = attributes or {} last_changed = last_changed or datetime.now() - # We do not want microseconds, as they get lost when we do datetime_to_str - last_changed = last_changed.replace(microsecond=0) - return {'state': state, 'attributes': attributes, - 'last_changed': last_changed} + 'last_changed': datetime_to_str(last_changed)} def track_state_change(eventbus, category, from_state, to_state, action): """ Helper method to track specific state changes. """ diff --git a/homeassistant/httpinterface.py b/homeassistant/httpinterface.py index 2e65ef37058..68a1e3086b0 100644 --- a/homeassistant/httpinterface.py +++ b/homeassistant/httpinterface.py @@ -128,7 +128,7 @@ class RequestHandler(BaseHTTPRequestHandler): ""). format(category, state['state'], - ha.datetime_to_str(state['last_changed']), + state['last_changed'], attributes)) write("") @@ -211,12 +211,6 @@ class RequestHandler(BaseHTTPRequestHandler): state['category'] = category - state['last_changed'] = ha.datetime_to_str( - state['last_changed']) - - - print state - self._response(use_json, "State of {}".format(category), json_data=state) diff --git a/homeassistant/test.py b/homeassistant/test.py index 80164bc897f..1c45e1114fd 100644 --- a/homeassistant/test.py +++ b/homeassistant/test.py @@ -36,6 +36,7 @@ class TestHTTPInterface(unittest.TestCase): API_PASSWORD) self.statemachine.set_state("test", "INIT_STATE") + self.sm_with_remote_eb.set_state("test", "INIT_STATE") self.eventbus.fire(ha.EVENT_START) @@ -49,6 +50,7 @@ class TestHTTPInterface(unittest.TestCase): cls.statemachine = ha.StateMachine(cls.eventbus) cls.remote_sm = remote.StateMachine("127.0.0.1", API_PASSWORD) cls.remote_eb = remote.EventBus("127.0.0.1", API_PASSWORD) + cls.sm_with_remote_eb = ha.StateMachine(cls.remote_eb) def test_debug_interface(self): """ Test if we can login by comparing not logged in screen to @@ -106,8 +108,7 @@ class TestHTTPInterface(unittest.TestCase): self.assertEqual(data['category'], "test") self.assertEqual(data['state'], state['state']) - self.assertEqual(ha.str_to_datetime(data['last_changed']), - state['last_changed']) + self.assertEqual(data['last_changed'], state['last_changed']) self.assertEqual(data['attributes'], state['attributes']) @@ -316,3 +317,21 @@ class TestHTTPInterface(unittest.TestCase): self.assertEqual(len(test_value), 1) + def test_local_sm_with_remote_eb(self): + """ Test if we get the event if we change a state on a + StateMachine connected to a remote eventbus. """ + test_value = [] + + def listener(event): # pylint: disable=unused-argument + """ Helper method that will verify our event got called. """ + test_value.append(1) + + self.eventbus.listen_once(ha.EVENT_STATE_CHANGED, listener) + + self.sm_with_remote_eb.set_state("test", "local sm with remote eb") + + # Allow the event to take place + time.sleep(1) + + self.assertEqual(len(test_value), 1) +