mirror of
https://github.com/home-assistant/core.git
synced 2025-08-03 20:55:10 +02:00
last_changed value of State is now a string so it can be send over the API
This commit is contained in:
@@ -65,12 +65,9 @@ def create_state(state, attributes=None, last_changed=None):
|
|||||||
attributes = attributes or {}
|
attributes = attributes or {}
|
||||||
last_changed = last_changed or datetime.now()
|
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,
|
return {'state': state,
|
||||||
'attributes': attributes,
|
'attributes': attributes,
|
||||||
'last_changed': last_changed}
|
'last_changed': datetime_to_str(last_changed)}
|
||||||
|
|
||||||
def track_state_change(eventbus, category, from_state, to_state, action):
|
def track_state_change(eventbus, category, from_state, to_state, action):
|
||||||
""" Helper method to track specific state changes. """
|
""" Helper method to track specific state changes. """
|
||||||
|
@@ -128,7 +128,7 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||||||
"</tr>").
|
"</tr>").
|
||||||
format(category,
|
format(category,
|
||||||
state['state'],
|
state['state'],
|
||||||
ha.datetime_to_str(state['last_changed']),
|
state['last_changed'],
|
||||||
attributes))
|
attributes))
|
||||||
|
|
||||||
write("</table>")
|
write("</table>")
|
||||||
@@ -211,12 +211,6 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
state['category'] = category
|
state['category'] = category
|
||||||
|
|
||||||
state['last_changed'] = ha.datetime_to_str(
|
|
||||||
state['last_changed'])
|
|
||||||
|
|
||||||
|
|
||||||
print state
|
|
||||||
|
|
||||||
self._response(use_json, "State of {}".format(category),
|
self._response(use_json, "State of {}".format(category),
|
||||||
json_data=state)
|
json_data=state)
|
||||||
|
|
||||||
|
@@ -36,6 +36,7 @@ class TestHTTPInterface(unittest.TestCase):
|
|||||||
API_PASSWORD)
|
API_PASSWORD)
|
||||||
|
|
||||||
self.statemachine.set_state("test", "INIT_STATE")
|
self.statemachine.set_state("test", "INIT_STATE")
|
||||||
|
self.sm_with_remote_eb.set_state("test", "INIT_STATE")
|
||||||
|
|
||||||
self.eventbus.fire(ha.EVENT_START)
|
self.eventbus.fire(ha.EVENT_START)
|
||||||
|
|
||||||
@@ -49,6 +50,7 @@ class TestHTTPInterface(unittest.TestCase):
|
|||||||
cls.statemachine = ha.StateMachine(cls.eventbus)
|
cls.statemachine = ha.StateMachine(cls.eventbus)
|
||||||
cls.remote_sm = remote.StateMachine("127.0.0.1", API_PASSWORD)
|
cls.remote_sm = remote.StateMachine("127.0.0.1", API_PASSWORD)
|
||||||
cls.remote_eb = remote.EventBus("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):
|
def test_debug_interface(self):
|
||||||
""" Test if we can login by comparing not logged in screen to
|
""" 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['category'], "test")
|
||||||
self.assertEqual(data['state'], state['state'])
|
self.assertEqual(data['state'], state['state'])
|
||||||
self.assertEqual(ha.str_to_datetime(data['last_changed']),
|
self.assertEqual(data['last_changed'], state['last_changed'])
|
||||||
state['last_changed'])
|
|
||||||
self.assertEqual(data['attributes'], state['attributes'])
|
self.assertEqual(data['attributes'], state['attributes'])
|
||||||
|
|
||||||
|
|
||||||
@@ -316,3 +317,21 @@ class TestHTTPInterface(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(len(test_value), 1)
|
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)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user