Skip to content

Commit

Permalink
Update after service calls (#4795)
Browse files Browse the repository at this point in the history
* Update after service calls

* Service update: wrap async_update in create_task
  • Loading branch information
balloob committed Dec 7, 2016
1 parent 7c3ae88 commit 9bca3f3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
8 changes: 6 additions & 2 deletions homeassistant/components/alarm_control_panel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
17 changes: 12 additions & 5 deletions homeassistant/components/cover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
19 changes: 12 additions & 7 deletions homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions homeassistant/components/lock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
18 changes: 11 additions & 7 deletions homeassistant/components/remote/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions homeassistant/components/switch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 9bca3f3

Please sign in to comment.