From 9bca3f31037d6ea292395a17b47208c63b800088 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 6 Dec 2016 22:30:47 -0800 Subject: [PATCH] Update after service calls (#4795) * Update after service calls * Service update: wrap async_update in create_task --- .../alarm_control_panel/__init__.py | 8 ++++++-- homeassistant/components/cover/__init__.py | 17 ++++++++++++----- homeassistant/components/light/__init__.py | 19 ++++++++++++------- homeassistant/components/lock/__init__.py | 7 +++++-- homeassistant/components/remote/__init__.py | 18 +++++++++++------- homeassistant/components/switch/__init__.py | 18 +++++++++++------- 6 files changed, 57 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/alarm_control_panel/__init__.py b/homeassistant/components/alarm_control_panel/__init__.py index 2030c8f88d803f..49decfc62fe868 100644 --- a/homeassistant/components/alarm_control_panel/__init__.py +++ b/homeassistant/components/alarm_control_panel/__init__.py @@ -59,8 +59,12 @@ def alarm_service_handler(service): for alarm in target_alarms: getattr(alarm, method)(code) - if alarm.should_poll: - alarm.update_ha_state(True) + + for alarm in target_alarms: + if not alarm.should_poll: + continue + + alarm.update_ha_state(True) descriptions = load_yaml_config_file( os.path.join(os.path.dirname(__file__), 'services.yaml')) diff --git a/homeassistant/components/cover/__init__.py b/homeassistant/components/cover/__init__.py index db517aec9785af..6c268e49be6b49 100644 --- a/homeassistant/components/cover/__init__.py +++ b/homeassistant/components/cover/__init__.py @@ -135,12 +135,19 @@ def handle_cover_service(service): params = service.data.copy() params.pop(ATTR_ENTITY_ID, None) - if method: - for cover in component.extract_from_service(service): - getattr(cover, method['method'])(**params) + if not method: + return - if cover.should_poll: - cover.update_ha_state(True) + covers = component.extract_from_service(service) + + for cover in covers: + getattr(cover, method['method'])(**params) + + for cover in covers: + if not cover.should_poll: + continue + + cover.update_ha_state(True) descriptions = load_yaml_config_file( os.path.join(os.path.dirname(__file__), 'services.yaml')) diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 04eb8fabc687af..869bbd90e7d00d 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -236,7 +236,6 @@ def async_handle_light_service(service): if color_name is not None: params[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name) - update_tasks = [] for light in target_lights: if service.service == SERVICE_TURN_ON: yield from light.async_turn_on(**params) @@ -245,12 +244,18 @@ def async_handle_light_service(service): else: yield from light.async_toggle(**params) - if light.should_poll: - update_coro = light.async_update_ha_state(True) - if hasattr(light, 'async_update'): - update_tasks.append(hass.loop.create_task(update_coro)) - else: - yield from update_coro + update_tasks = [] + + for light in target_lights: + if not light.should_poll: + continue + + update_coro = hass.loop.create_task( + light.async_update_ha_state(True)) + if hasattr(light, 'async_update'): + update_tasks.append(hass.loop.create_task(update_coro)) + else: + yield from update_coro if update_tasks: yield from asyncio.wait(update_tasks, loop=hass.loop) diff --git a/homeassistant/components/lock/__init__.py b/homeassistant/components/lock/__init__.py index 95db9d2b33a760..e74b675733b21e 100644 --- a/homeassistant/components/lock/__init__.py +++ b/homeassistant/components/lock/__init__.py @@ -85,8 +85,11 @@ def handle_lock_service(service): else: item.unlock(code=code) - if item.should_poll: - item.update_ha_state(True) + for item in target_locks: + if not item.should_poll: + continue + + item.update_ha_state(True) descriptions = load_yaml_config_file( os.path.join(os.path.dirname(__file__), 'services.yaml')) diff --git a/homeassistant/components/remote/__init__.py b/homeassistant/components/remote/__init__.py index d6f534eae5bf22..3a481e8383073a 100755 --- a/homeassistant/components/remote/__init__.py +++ b/homeassistant/components/remote/__init__.py @@ -98,7 +98,6 @@ def async_handle_remote_service(service): device = service.data.get(ATTR_DEVICE) command = service.data.get(ATTR_COMMAND) - update_tasks = [] for remote in target_remotes: if service.service == SERVICE_TURN_ON: yield from remote.async_turn_on(activity=activity_id) @@ -108,12 +107,17 @@ def async_handle_remote_service(service): else: yield from remote.async_turn_off() - if remote.should_poll: - update_coro = remote.async_update_ha_state(True) - if hasattr(remote, 'async_update'): - update_tasks.append(hass.loop.create_task(update_coro)) - else: - yield from update_coro + update_tasks = [] + for remote in target_remotes: + if not remote.should_poll: + continue + + update_coro = hass.loop.create_task( + remote.async_update_ha_state(True)) + if hasattr(remote, 'async_update'): + update_tasks.append(hass.loop.create_task(update_coro)) + else: + yield from update_coro if update_tasks: yield from asyncio.wait(update_tasks, loop=hass.loop) diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index 02a313c675f706..846a87f5067933 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -82,7 +82,6 @@ def async_handle_switch_service(service): """Handle calls to the switch services.""" target_switches = component.async_extract_from_service(service) - update_tasks = [] for switch in target_switches: if service.service == SERVICE_TURN_ON: yield from switch.async_turn_on() @@ -91,12 +90,17 @@ def async_handle_switch_service(service): else: yield from switch.async_turn_off() - if switch.should_poll: - update_coro = switch.async_update_ha_state(True) - if hasattr(switch, 'async_update'): - update_tasks.append(hass.loop.create_task(update_coro)) - else: - yield from update_coro + update_tasks = [] + for switch in target_switches: + if not switch.should_poll: + continue + + update_coro = hass.loop.create_task( + switch.async_update_ha_state(True)) + if hasattr(switch, 'async_update'): + update_tasks.append(hass.loop.create_task(update_coro)) + else: + yield from update_coro if update_tasks: yield from asyncio.wait(update_tasks, loop=hass.loop)