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.
This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-10-09 09:31:17 +02:00
1 parent 617509413c
commit 1c431aa7bd
1 file changed
+2
@@ -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