Set created field when creating db events and states (#41523)

Avoids thousands of utcnow calls when the session
is commited on busy systems to fill in the field
default.
pull/41535/head
J. Nick Koston 2020-10-09 02:31:17 -05:00 committed by GitHub
parent 617509413c
commit 1c431aa7bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 0 deletions

View File

@ -393,6 +393,7 @@ class Recorder(threading.Thread):
dbevent = Events.from_event(event, event_data="{}")
else:
dbevent = Events.from_event(event)
dbevent.created = event.time_fired
self.event_session.add(dbevent)
except (TypeError, ValueError):
_LOGGER.warning("Event is not JSON serializable: %s", event)
@ -409,6 +410,7 @@ class Recorder(threading.Thread):
if not has_new_state:
dbstate.state = None
dbstate.event = dbevent
dbstate.created = event.time_fired
self.event_session.add(dbstate)
if has_new_state:
self._old_states[dbstate.entity_id] = dbstate