diff --git a/homeassistant/components/logbook.py b/homeassistant/components/logbook.py index ada8bf78ab0..c7a37411f1e 100644 --- a/homeassistant/components/logbook.py +++ b/homeassistant/components/logbook.py @@ -391,9 +391,9 @@ def _get_events(hass, config, start_day, end_day, entity_id=None): .filter(Events.event_type.in_(ALL_EVENT_TYPES)) \ .filter((Events.time_fired > start_day) & (Events.time_fired < end_day)) \ - .filter((States.last_updated == States.last_changed) - | (States.state_id.is_(None))) \ - .filter(States.entity_id.in_(entity_ids)) + .filter(((States.last_updated == States.last_changed) & + States.entity_id.in_(entity_ids)) + | (States.state_id.is_(None))) events = execute(query) diff --git a/tests/components/test_logbook.py b/tests/components/test_logbook.py index 5229d34b74c..ae1e3d1d51a 100644 --- a/tests/components/test_logbook.py +++ b/tests/components/test_logbook.py @@ -62,6 +62,12 @@ class TestComponentLogbook(unittest.TestCase): # Our service call will unblock when the event listeners have been # scheduled. This means that they may not have been processed yet. self.hass.block_till_done() + self.hass.data[recorder.DATA_INSTANCE].block_till_done() + + events = list(logbook._get_events( + self.hass, {}, dt_util.utcnow() - timedelta(hours=1), + dt_util.utcnow() + timedelta(hours=1))) + assert len(events) == 2 assert 1 == len(calls) last_call = calls[-1]