Improve websocket throughput of state changes (#86855)
After the start event we tend to get an event storm of state changes which can get the websocket behind. #86854 will help with that a bit, but we can reduce the overhead to build a state diff when the attributes have not changedpull/86876/head
parent
799edd90aa
commit
ec3475910f
|
@ -161,12 +161,14 @@ def _state_diff(
|
|||
additions[COMPRESSED_STATE_CONTEXT]["id"] = new_state.context.id
|
||||
else:
|
||||
additions[COMPRESSED_STATE_CONTEXT] = new_state.context.id
|
||||
old_attributes = old_state.attributes
|
||||
for key, value in new_state.attributes.items():
|
||||
if old_attributes.get(key) != value:
|
||||
additions.setdefault(COMPRESSED_STATE_ATTRIBUTES, {})[key] = value
|
||||
if removed := set(old_attributes).difference(new_state.attributes):
|
||||
diff[STATE_DIFF_REMOVALS] = {COMPRESSED_STATE_ATTRIBUTES: removed}
|
||||
if (old_attributes := old_state.attributes) != (
|
||||
new_attributes := new_state.attributes
|
||||
):
|
||||
for key, value in new_attributes.items():
|
||||
if old_attributes.get(key) != value:
|
||||
additions.setdefault(COMPRESSED_STATE_ATTRIBUTES, {})[key] = value
|
||||
if removed := set(old_attributes).difference(new_attributes):
|
||||
diff[STATE_DIFF_REMOVALS] = {COMPRESSED_STATE_ATTRIBUTES: removed}
|
||||
return {ENTITY_EVENT_CHANGE: {new_state.entity_id: diff}}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue