Don't block on sevice call for alexa (#11358)

* Don't block on sevice call for alexa

* fix tests
This commit is contained in:
Pascal Vizeli 2017-12-29 18:44:06 +01:00 committed by Paulus Schoutsen
parent d3ed6d5242
commit e0a1b87296
2 changed files with 66 additions and 36 deletions

View file

@ -245,7 +245,7 @@ def async_api_turn_on(hass, config, request, entity):
yield from hass.services.async_call(domain, SERVICE_TURN_ON, {
ATTR_ENTITY_ID: entity.entity_id
}, blocking=True)
}, blocking=False)
return api_message(request)
@ -261,7 +261,7 @@ def async_api_turn_off(hass, config, request, entity):
yield from hass.services.async_call(domain, SERVICE_TURN_OFF, {
ATTR_ENTITY_ID: entity.entity_id
}, blocking=True)
}, blocking=False)
return api_message(request)
@ -276,7 +276,7 @@ def async_api_set_brightness(hass, config, request, entity):
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
ATTR_ENTITY_ID: entity.entity_id,
light.ATTR_BRIGHTNESS_PCT: brightness,
}, blocking=True)
}, blocking=False)
return api_message(request)
@ -300,7 +300,7 @@ def async_api_adjust_brightness(hass, config, request, entity):
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
ATTR_ENTITY_ID: entity.entity_id,
light.ATTR_BRIGHTNESS_PCT: brightness,
}, blocking=True)
}, blocking=False)
return api_message(request)
@ -321,14 +321,14 @@ def async_api_set_color(hass, config, request, entity):
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
ATTR_ENTITY_ID: entity.entity_id,
light.ATTR_RGB_COLOR: rgb,
}, blocking=True)
}, blocking=False)
else:
xyz = color_util.color_RGB_to_xy(*rgb)
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
ATTR_ENTITY_ID: entity.entity_id,
light.ATTR_XY_COLOR: (xyz[0], xyz[1]),
light.ATTR_BRIGHTNESS: xyz[2],
}, blocking=True)
}, blocking=False)
return api_message(request)
@ -343,7 +343,7 @@ def async_api_set_color_temperature(hass, config, request, entity):
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
ATTR_ENTITY_ID: entity.entity_id,
light.ATTR_KELVIN: kelvin,
}, blocking=True)
}, blocking=False)
return api_message(request)
@ -361,7 +361,7 @@ def async_api_decrease_color_temp(hass, config, request, entity):
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
ATTR_ENTITY_ID: entity.entity_id,
light.ATTR_COLOR_TEMP: value,
}, blocking=True)
}, blocking=False)
return api_message(request)
@ -379,7 +379,7 @@ def async_api_increase_color_temp(hass, config, request, entity):
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
ATTR_ENTITY_ID: entity.entity_id,
light.ATTR_COLOR_TEMP: value,
}, blocking=True)
}, blocking=False)
return api_message(request)
@ -391,7 +391,7 @@ def async_api_activate(hass, config, request, entity):
"""Process a activate request."""
yield from hass.services.async_call(entity.domain, SERVICE_TURN_ON, {
ATTR_ENTITY_ID: entity.entity_id
}, blocking=True)
}, blocking=False)
return api_message(request)
@ -421,8 +421,8 @@ def async_api_set_percentage(hass, config, request, entity):
service = SERVICE_SET_COVER_POSITION
data[cover.ATTR_POSITION] = percentage
yield from hass.services.async_call(entity.domain, service,
data, blocking=True)
yield from hass.services.async_call(
entity.domain, service, data, blocking=False)
return api_message(request)
@ -469,8 +469,8 @@ def async_api_adjust_percentage(hass, config, request, entity):
data[cover.ATTR_POSITION] = max(0, percentage_delta + current)
yield from hass.services.async_call(entity.domain, service,
data, blocking=True)
yield from hass.services.async_call(
entity.domain, service, data, blocking=False)
return api_message(request)
@ -482,7 +482,7 @@ def async_api_lock(hass, config, request, entity):
"""Process a lock request."""
yield from hass.services.async_call(entity.domain, SERVICE_LOCK, {
ATTR_ENTITY_ID: entity.entity_id
}, blocking=True)
}, blocking=False)
return api_message(request)
@ -495,7 +495,7 @@ def async_api_unlock(hass, config, request, entity):
"""Process a unlock request."""
yield from hass.services.async_call(entity.domain, SERVICE_UNLOCK, {
ATTR_ENTITY_ID: entity.entity_id
}, blocking=True)
}, blocking=False)
return api_message(request)
@ -512,8 +512,9 @@ def async_api_set_volume(hass, config, request, entity):
media_player.ATTR_MEDIA_VOLUME_LEVEL: volume,
}
yield from hass.services.async_call(entity.domain, SERVICE_VOLUME_SET,
data, blocking=True)
yield from hass.services.async_call(
entity.domain, SERVICE_VOLUME_SET,
data, blocking=False)
return api_message(request)
@ -540,9 +541,9 @@ def async_api_adjust_volume(hass, config, request, entity):
media_player.ATTR_MEDIA_VOLUME_LEVEL: volume,
}
yield from hass.services.async_call(entity.domain,
media_player.SERVICE_VOLUME_SET,
data, blocking=True)
yield from hass.services.async_call(
entity.domain, media_player.SERVICE_VOLUME_SET,
data, blocking=False)
return api_message(request)
@ -559,9 +560,9 @@ def async_api_set_mute(hass, config, request, entity):
media_player.ATTR_MEDIA_VOLUME_MUTED: mute,
}
yield from hass.services.async_call(entity.domain,
media_player.SERVICE_VOLUME_MUTE,
data, blocking=True)
yield from hass.services.async_call(
entity.domain, media_player.SERVICE_VOLUME_MUTE,
data, blocking=False)
return api_message(request)
@ -575,8 +576,9 @@ def async_api_play(hass, config, request, entity):
ATTR_ENTITY_ID: entity.entity_id
}
yield from hass.services.async_call(entity.domain, SERVICE_MEDIA_PLAY,
data, blocking=True)
yield from hass.services.async_call(
entity.domain, SERVICE_MEDIA_PLAY,
data, blocking=False)
return api_message(request)
@ -590,8 +592,9 @@ def async_api_pause(hass, config, request, entity):
ATTR_ENTITY_ID: entity.entity_id
}
yield from hass.services.async_call(entity.domain, SERVICE_MEDIA_PAUSE,
data, blocking=True)
yield from hass.services.async_call(
entity.domain, SERVICE_MEDIA_PAUSE,
data, blocking=False)
return api_message(request)
@ -605,8 +608,9 @@ def async_api_stop(hass, config, request, entity):
ATTR_ENTITY_ID: entity.entity_id
}
yield from hass.services.async_call(entity.domain, SERVICE_MEDIA_STOP,
data, blocking=True)
yield from hass.services.async_call(
entity.domain, SERVICE_MEDIA_STOP,
data, blocking=False)
return api_message(request)
@ -620,9 +624,9 @@ def async_api_next(hass, config, request, entity):
ATTR_ENTITY_ID: entity.entity_id
}
yield from hass.services.async_call(entity.domain,
SERVICE_MEDIA_NEXT_TRACK,
data, blocking=True)
yield from hass.services.async_call(
entity.domain, SERVICE_MEDIA_NEXT_TRACK,
data, blocking=False)
return api_message(request)
@ -636,8 +640,8 @@ def async_api_previous(hass, config, request, entity):
ATTR_ENTITY_ID: entity.entity_id
}
yield from hass.services.async_call(entity.domain,
SERVICE_MEDIA_PREVIOUS_TRACK,
data, blocking=True)
yield from hass.services.async_call(
entity.domain, SERVICE_MEDIA_PREVIOUS_TRACK,
data, blocking=False)
return api_message(request)

View file

@ -346,6 +346,7 @@ def test_exclude_filters(hass):
))
msg = yield from smart_home.async_handle_message(hass, config, request)
yield from hass.async_block_till_done()
msg = msg['event']
@ -378,6 +379,7 @@ def test_include_filters(hass):
))
msg = yield from smart_home.async_handle_message(hass, config, request)
yield from hass.async_block_till_done()
msg = msg['event']
@ -393,6 +395,7 @@ def test_api_entity_not_exists(hass):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -442,6 +445,7 @@ def test_api_turn_on(hass, domain):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -475,6 +479,7 @@ def test_api_turn_off(hass, domain):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -501,6 +506,7 @@ def test_api_set_brightness(hass):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -532,6 +538,7 @@ def test_api_adjust_brightness(hass, result, adjust):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -566,6 +573,7 @@ def test_api_set_color_rgb(hass):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -600,6 +608,7 @@ def test_api_set_color_xy(hass):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -629,6 +638,7 @@ def test_api_set_color_temperature(hass):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -658,6 +668,7 @@ def test_api_decrease_color_temp(hass, result, initial):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -687,6 +698,7 @@ def test_api_increase_color_temp(hass, result, initial):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -714,6 +726,7 @@ def test_api_activate(hass, domain):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -740,6 +753,7 @@ def test_api_set_percentage_fan(hass):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -769,6 +783,7 @@ def test_api_set_percentage_cover(hass):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -800,6 +815,7 @@ def test_api_adjust_percentage_fan(hass, result, adjust):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -832,6 +848,7 @@ def test_api_adjust_percentage_cover(hass, result, adjust):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -859,6 +876,7 @@ def test_api_lock(hass, domain):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -885,6 +903,7 @@ def test_api_play(hass, domain):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -911,6 +930,7 @@ def test_api_pause(hass, domain):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -937,6 +957,7 @@ def test_api_stop(hass, domain):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -963,6 +984,7 @@ def test_api_next(hass, domain):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -989,6 +1011,7 @@ def test_api_previous(hass, domain):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -1017,6 +1040,7 @@ def test_api_set_volume(hass):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -1048,6 +1072,7 @@ def test_api_adjust_volume(hass, result, adjust):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']
@ -1077,6 +1102,7 @@ def test_api_mute(hass, domain):
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
yield from hass.async_block_till_done()
assert 'event' in msg
msg = msg['event']