From 222d57bda78c3287a7424f1d85940394ac01ed46 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 8 Nov 2014 14:22:17 -0800 Subject: [PATCH] track_state_change now accepts a list of entity_ids --- homeassistant/__init__.py | 8 ++++++-- homeassistant/components/device_sun_light_trigger.py | 8 +++----- homeassistant/components/group.py | 3 +-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/homeassistant/__init__.py b/homeassistant/__init__.py index f111e4c050f..3d6d8536912 100644 --- a/homeassistant/__init__.py +++ b/homeassistant/__init__.py @@ -106,16 +106,20 @@ class HomeAssistant(object): else: return self.states.entity_ids - def track_state_change(self, entity_id, action, + def track_state_change(self, entity_ids, action, from_state=None, to_state=None): """ Track specific state changes. """ from_state = _process_match_param(from_state) to_state = _process_match_param(to_state) + # Ensure it is a list with entity ids we want to match on + if isinstance(entity_ids, str): + entity_ids = [entity_ids] + @ft.wraps(action) def state_listener(event): """ The listener that listens for specific state changes. """ - if entity_id == event.data['entity_id'] and \ + if event.data['entity_id'] in entity_ids and \ 'old_state' in event.data and \ _matcher(event.data['old_state'].state, from_state) and \ _matcher(event.data['new_state'].state, to_state): diff --git a/homeassistant/components/device_sun_light_trigger.py b/homeassistant/components/device_sun_light_trigger.py index 6ad2166c792..0fc2b99ee31 100644 --- a/homeassistant/components/device_sun_light_trigger.py +++ b/homeassistant/components/device_sun_light_trigger.py @@ -151,11 +151,9 @@ def setup(hass, config): light.turn_off(hass) - # Track home coming of each seperate device - for entity in device_entity_ids: - hass.track_state_change(entity, check_light_on_dev_state_change, - components.STATE_NOT_HOME, - components.STATE_HOME) + # Track home coming of each device + hass.track_state_change(device_entity_ids, check_light_on_dev_state_change, + components.STATE_NOT_HOME, components.STATE_HOME) # Track when all devices are gone to shut down lights hass.track_state_change(device_tracker.ENTITY_ID_ALL_DEVICES, diff --git a/homeassistant/components/group.py b/homeassistant/components/group.py index 511e5bbc05b..7880e197d28 100644 --- a/homeassistant/components/group.py +++ b/homeassistant/components/group.py @@ -186,8 +186,7 @@ def setup_group(hass, name, entity_ids, user_defined=True): if entity_id != ent_id]): hass.states.set(group_entity_id, group_off, state_attr) - for entity_id in entity_ids: - hass.track_state_change(entity_id, update_group_state) + hass.track_state_change(entity_ids, update_group_state) hass.states.set(group_entity_id, group_state, state_attr)