Filter out forced updates in live logbook when the state has not changed (#73335)

This commit is contained in:
J. Nick Koston 2022-06-10 11:04:43 -10:00 committed by GitHub
parent 53b3d2ee87
commit e4f354998d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 126 additions and 8 deletions

View file

@ -189,9 +189,10 @@ def async_subscribe_events(
def _forward_state_events_filtered(event: Event) -> None:
if event.data.get("old_state") is None or event.data.get("new_state") is None:
return
state: State = event.data["new_state"]
if _is_state_filtered(ent_reg, state) or (
entities_filter and not entities_filter(state.entity_id)
new_state: State = event.data["new_state"]
old_state: State = event.data["old_state"]
if _is_state_filtered(ent_reg, new_state, old_state) or (
entities_filter and not entities_filter(new_state.entity_id)
):
return
target(event)
@ -229,17 +230,20 @@ def is_sensor_continuous(ent_reg: er.EntityRegistry, entity_id: str) -> bool:
)
def _is_state_filtered(ent_reg: er.EntityRegistry, state: State) -> bool:
def _is_state_filtered(
ent_reg: er.EntityRegistry, new_state: State, old_state: State
) -> bool:
"""Check if the logbook should filter a state.
Used when we are in live mode to ensure
we only get significant changes (state.last_changed != state.last_updated)
"""
return bool(
split_entity_id(state.entity_id)[0] in ALWAYS_CONTINUOUS_DOMAINS
or state.last_changed != state.last_updated
or ATTR_UNIT_OF_MEASUREMENT in state.attributes
or is_sensor_continuous(ent_reg, state.entity_id)
new_state.state == old_state.state
or split_entity_id(new_state.entity_id)[0] in ALWAYS_CONTINUOUS_DOMAINS
or new_state.last_changed != new_state.last_updated
or ATTR_UNIT_OF_MEASUREMENT in new_state.attributes
or is_sensor_continuous(ent_reg, new_state.entity_id)
)