Update after service calls (#4795)

* Update after service calls

* Service update: wrap async_update in create_task
This commit is contained in:
Paulus Schoutsen 2016-12-06 22:30:47 -08:00 committed by GitHub
parent 37e3c2a133
commit 98fe50d5ad
6 changed files with 57 additions and 30 deletions

View File

@ -59,8 +59,12 @@ def setup(hass, config):
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'))

View File

@ -135,12 +135,19 @@ def setup(hass, config):
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'))

View File

@ -236,7 +236,6 @@ def async_setup(hass, config):
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_setup(hass, config):
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)

View File

@ -85,8 +85,11 @@ def setup(hass, config):
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'))

View File

@ -98,7 +98,6 @@ def async_setup(hass, config):
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_setup(hass, config):
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)

View File

@ -82,7 +82,6 @@ def async_setup(hass, config):
"""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_setup(hass, config):
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)