mirror of
https://github.com/home-assistant/core.git
synced 2025-04-19 14:57:52 +00:00
Add State.last_updated to JSON obj
This commit is contained in:
parent
b02c11c31d
commit
00bbc17e11
@ -463,7 +463,8 @@ class State(object):
|
||||
__slots__ = ['entity_id', 'state', 'attributes',
|
||||
'last_changed', 'last_updated']
|
||||
|
||||
def __init__(self, entity_id, state, attributes=None, last_changed=None):
|
||||
def __init__(self, entity_id, state, attributes=None, last_changed=None,
|
||||
last_updated=None):
|
||||
if not ENTITY_ID_PATTERN.match(entity_id):
|
||||
raise InvalidEntityFormatError((
|
||||
"Invalid entity id encountered: {}. "
|
||||
@ -472,7 +473,7 @@ class State(object):
|
||||
self.entity_id = entity_id.lower()
|
||||
self.state = state
|
||||
self.attributes = attributes or {}
|
||||
self.last_updated = dt.datetime.now()
|
||||
self.last_updated = last_updated or dt.datetime.now()
|
||||
|
||||
# Strip microsecond from last_changed else we cannot guarantee
|
||||
# state == State.from_dict(state.as_dict())
|
||||
@ -510,7 +511,8 @@ class State(object):
|
||||
return {'entity_id': self.entity_id,
|
||||
'state': self.state,
|
||||
'attributes': self.attributes,
|
||||
'last_changed': util.datetime_to_str(self.last_changed)}
|
||||
'last_changed': util.datetime_to_str(self.last_changed),
|
||||
'last_updated': util.datetime_to_str(self.last_updated)}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, json_dict):
|
||||
@ -527,8 +529,13 @@ class State(object):
|
||||
if last_changed:
|
||||
last_changed = util.str_to_datetime(last_changed)
|
||||
|
||||
last_updated = json_dict.get('last_updated')
|
||||
|
||||
if last_updated:
|
||||
last_updated = util.str_to_datetime(last_updated)
|
||||
|
||||
return cls(json_dict['entity_id'], json_dict['state'],
|
||||
json_dict.get('attributes'), last_changed)
|
||||
json_dict.get('attributes'), last_changed, last_updated)
|
||||
|
||||
def __eq__(self, other):
|
||||
return (self.__class__ == other.__class__ and
|
||||
|
@ -60,7 +60,8 @@ def row_to_state(row):
|
||||
""" Convert a databsae row to a state. """
|
||||
try:
|
||||
return State(
|
||||
row[1], row[2], json.loads(row[3]), datetime.fromtimestamp(row[4]))
|
||||
row[1], row[2], json.loads(row[3]), datetime.fromtimestamp(row[4]),
|
||||
datetime.fromtimestamp(row[5]))
|
||||
except ValueError:
|
||||
# When json.loads fails
|
||||
_LOGGER.exception("Error converting row to state: %s", row)
|
||||
|
Loading…
x
Reference in New Issue
Block a user