Skip to content

Commit

Permalink
Add State.last_updated to JSON obj
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Apr 1, 2015
1 parent b02c11c commit 00bbc17
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
15 changes: 11 additions & 4 deletions homeassistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}. "
Expand All @@ -472,7 +473,7 @@ def __init__(self, entity_id, state, attributes=None, last_changed=None):
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())
Expand Down Expand Up @@ -510,7 +511,8 @@ def as_dict(self):
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):
Expand All @@ -527,8 +529,13 @@ def from_dict(cls, json_dict):
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
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 00bbc17

Please sign in to comment.