Ensure removed entities are not displayed in logbook (#37395)

This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-07-03 23:08:46 -07:00
1 parent 045cdee30c
commit 4b2ebf5487
5 files changed
+121 -98

No files matched your search

+8 -9
View File
@@ -355,7 +355,7 @@ def _get_events(
"""Yield Events that are not filtered away."""
for row in query.yield_per(1000):
event = LazyEventPartialState(row)
if _keep_event(hass, event, entities_filter, entity_attr_cache):
if _keep_event(hass, event, entities_filter):
yield event
with session_scope(hass=hass) as session:
@@ -375,12 +375,10 @@ def _get_events(
Events.event_data,
Events.time_fired,
Events.context_user_id,
States.state_id,
States.state,
States.entity_id,
States.domain,
States.attributes,
old_state.state_id.label("old_state_id"),
)
.order_by(Events.time_fired)
.outerjoin(States, (Events.event_id == States.event_id))
@@ -400,6 +398,7 @@ def _get_events(
| (
(States.state_id.isnot(None))
& (old_state.state_id.isnot(None))
& (States.state.isnot(None))
& (States.state != old_state.state)
)
)
@@ -444,12 +443,9 @@ def _get_events(
return list(humanify(hass, yield_events(query), entity_attr_cache, prev_states))
def _keep_event(hass, event, entities_filter, entity_attr_cache):
def _keep_event(hass, event, entities_filter):
if event.event_type == EVENT_STATE_CHANGED:
entity_id = event.entity_id
if entity_id is None:
return False
# Do not report on new entities
# Do not report on entity removal
if not event.has_old_and_new_state:
@@ -650,9 +646,12 @@ class LazyEventPartialState:
# Delete this check once all states are saved in the v8 schema
# format or later (they have the old_state_id column).
# New events in v8 schema format
# New events in v8+ schema format
if self._row.event_data == EMPTY_JSON_OBJECT:
return self._row.state_id is not None and self._row.old_state_id is not None
# Events are already pre-filtered in sql
# to exclude missing old and new state
# if they are in v8+ format
return True
# Old events not in v8 schema format
return (
@@ -386,11 +386,14 @@ class Recorder(threading.Thread):
if dbevent and event.event_type == EVENT_STATE_CHANGED:
try:
dbstate = States.from_event(event)
has_new_state = event.data.get("new_state")
dbstate.old_state_id = self._old_state_ids.get(dbstate.entity_id)
if not has_new_state:
dbstate.state = None
dbstate.event_id = dbevent.event_id
self.event_session.add(dbstate)
self.event_session.flush()
if "new_state" in event.data:
if has_new_state:
self._old_state_ids[dbstate.entity_id] = dbstate.state_id
elif dbstate.entity_id in self._old_state_ids:
del self._old_state_ids[dbstate.entity_id]