Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter out duplicate logbook states #32427

Merged
merged 1 commit into from Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion homeassistant/components/logbook/__init__.py
Expand Up @@ -199,6 +199,9 @@ def humanify(hass, events):
"""
domain_prefixes = tuple(f"{dom}." for dom in CONTINUOUS_DOMAINS)

# Track last states to filter out duplicates
last_state = {}

# Group events in batches of GROUP_BY_MINUTES
for _, g_events in groupby(
events, lambda event: event.time_fired.minute // GROUP_BY_MINUTES
Expand Down Expand Up @@ -236,9 +239,15 @@ def humanify(hass, events):
# Yield entries
for event in events_batch:
if event.event_type == EVENT_STATE_CHANGED:

to_state = State.from_dict(event.data.get("new_state"))

# Filter out states that become same state again (force_update=True)
# or light becoming different color
if last_state.get(to_state.entity_id) == to_state.state:
continue

last_state[to_state.entity_id] = to_state.state

domain = to_state.domain

# Skip all but the last sensor state
Expand Down
33 changes: 33 additions & 0 deletions tests/components/logbook/test_init.py
Expand Up @@ -1484,3 +1484,36 @@ async def test_humanify_script_started_event(hass):
assert event2["domain"] == "script"
assert event2["message"] == "started"
assert event2["entity_id"] == "script.bye"


async def test_humanify_same_state(hass):
"""Test humanifying Script Run event."""
state_50 = ha.State("light.kitchen", "on", {"brightness": 50}).as_dict()
state_100 = ha.State("light.kitchen", "on", {"brightness": 100}).as_dict()
state_200 = ha.State("light.kitchen", "on", {"brightness": 200}).as_dict()

events = list(
logbook.humanify(
hass,
[
ha.Event(
EVENT_STATE_CHANGED,
{
"entity_id": "light.kitchen",
"old_state": state_50,
"new_state": state_100,
},
),
ha.Event(
EVENT_STATE_CHANGED,
{
"entity_id": "light.kitchen",
"old_state": state_100,
"new_state": state_200,
},
),
],
)
)

assert len(events) == 1