mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
StateMachine.set_state now optimized for setting existing states
This commit is contained in:
parent
e9e1b007ed
commit
cd58147fa7
@ -379,24 +379,27 @@ class StateMachine(object):
|
|||||||
|
|
||||||
attributes = attributes or {}
|
attributes = attributes or {}
|
||||||
|
|
||||||
with self.lock:
|
state = State(new_state, attributes)
|
||||||
# Add entity if it does not exist
|
|
||||||
if entity_id not in self.states:
|
|
||||||
self.states[entity_id] = State(new_state, attributes)
|
|
||||||
|
|
||||||
|
with self.lock:
|
||||||
# Change state and fire listeners
|
# Change state and fire listeners
|
||||||
else:
|
try:
|
||||||
old_state = self.states[entity_id]
|
old_state = self.states[entity_id]
|
||||||
|
|
||||||
|
except KeyError:
|
||||||
|
# If state did not exist yet
|
||||||
|
pass
|
||||||
|
|
||||||
|
else:
|
||||||
if old_state.state != new_state or \
|
if old_state.state != new_state or \
|
||||||
old_state.attributes != attributes:
|
old_state.attributes != attributes:
|
||||||
|
|
||||||
self.states[entity_id] = State(new_state, attributes)
|
|
||||||
|
|
||||||
self.bus.fire_event(EVENT_STATE_CHANGED,
|
self.bus.fire_event(EVENT_STATE_CHANGED,
|
||||||
{'entity_id': entity_id,
|
{'entity_id': entity_id,
|
||||||
'old_state': old_state,
|
'old_state': old_state,
|
||||||
'new_state': self.states[entity_id]})
|
'new_state': state})
|
||||||
|
|
||||||
|
self.states[entity_id] = State(new_state, attributes)
|
||||||
|
|
||||||
def get_state(self, entity_id):
|
def get_state(self, entity_id):
|
||||||
""" Returns a dict (state, last_changed, attributes) describing
|
""" Returns a dict (state, last_changed, attributes) describing
|
||||||
|
Loading…
x
Reference in New Issue
Block a user