Fire event when we remove a state

This commit is contained in:
Paulus Schoutsen 2016-02-13 22:57:40 -08:00
parent e170484f16
commit ada2fb4ec0
4 changed files with 67 additions and 23 deletions

View file

@ -438,7 +438,20 @@ class StateMachine(object):
entity_id = entity_id.lower()
with self._lock:
return self._states.pop(entity_id, None) is not None
old_state = self._states.pop(entity_id, None)
if old_state is None:
return False
event_data = {
'entity_id': entity_id,
'old_state': old_state,
'new_state': None,
}
self._bus.fire(EVENT_STATE_CHANGED, event_data)
return True
def set(self, entity_id, new_state, attributes=None):
"""Set the state of an entity, add entity if it does not exist.
@ -468,10 +481,11 @@ class StateMachine(object):
state = State(entity_id, new_state, attributes, last_changed)
self._states[entity_id] = state
event_data = {'entity_id': entity_id, 'new_state': state}
if old_state:
event_data['old_state'] = old_state
event_data = {
'entity_id': entity_id,
'old_state': old_state,
'new_state': state,
}
self._bus.fire(EVENT_STATE_CHANGED, event_data)