last_changed value of State is now a string so it can be send over the API

This commit is contained in:
Paulus Schoutsen 2013-10-28 18:45:35 -07:00
parent 308b49b585
commit 102690a770
3 changed files with 23 additions and 13 deletions

View File

@ -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. """

View File

@ -128,7 +128,7 @@ class RequestHandler(BaseHTTPRequestHandler):
"</tr>").
format(category,
state['state'],
ha.datetime_to_str(state['last_changed']),
state['last_changed'],
attributes))
write("</table>")
@ -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)

View File

@ -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)