From 3169c0416e37ea28db3df4a998c5a3b6dd3aeb0d Mon Sep 17 00:00:00 2001 From: Phil Frost Date: Mon, 29 Oct 2018 17:57:27 -0400 Subject: [PATCH] Update Alexa tests to async syntax (#17965) See https://github.com/home-assistant/home-assistant/issues/12614 --- tests/components/alexa/test_smart_home.py | 345 ++++++++++------------ 1 file changed, 152 insertions(+), 193 deletions(-) diff --git a/tests/components/alexa/test_smart_home.py b/tests/components/alexa/test_smart_home.py index 83aabb5dd4c..ab268fe860f 100644 --- a/tests/components/alexa/test_smart_home.py +++ b/tests/components/alexa/test_smart_home.py @@ -1,5 +1,4 @@ """Test for smart home alexa support.""" -import asyncio import json from uuid import uuid4 @@ -105,25 +104,23 @@ def test_create_api_message_special(): assert 'endpoint' not in msg -@asyncio.coroutine -def test_wrong_version(hass): +async def test_wrong_version(hass): """Test with wrong version.""" msg = get_new_request('Alexa.PowerController', 'TurnOn') msg['directive']['header']['payloadVersion'] = '2' with pytest.raises(AssertionError): - yield from smart_home.async_handle_message(hass, DEFAULT_CONFIG, msg) + await smart_home.async_handle_message(hass, DEFAULT_CONFIG, msg) -@asyncio.coroutine -def discovery_test(device, hass, expected_endpoints=1): +async def discovery_test(device, hass, expected_endpoints=1): """Test alexa discovery request.""" request = get_new_request('Alexa.Discovery', 'Discover') # setup test devices hass.states.async_set(*device) - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request) assert 'event' in msg @@ -156,54 +153,51 @@ def assert_endpoint_capabilities(endpoint, *interfaces): return capabilities -@asyncio.coroutine -def test_switch(hass, events): +async def test_switch(hass, events): """Test switch discovery.""" device = ('switch.test', 'on', {'friendly_name': "Test switch"}) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'switch#test' assert appliance['displayCategories'][0] == "SWITCH" assert appliance['friendlyName'] == "Test switch" assert_endpoint_capabilities(appliance, 'Alexa.PowerController') - yield from assert_power_controller_works( + await assert_power_controller_works( 'switch#test', 'switch.turn_on', 'switch.turn_off', hass) - properties = yield from reported_properties(hass, 'switch#test') + properties = await reported_properties(hass, 'switch#test') properties.assert_equal('Alexa.PowerController', 'powerState', 'ON') -@asyncio.coroutine -def test_light(hass): +async def test_light(hass): """Test light discovery.""" device = ('light.test_1', 'on', {'friendly_name': "Test light 1"}) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'light#test_1' assert appliance['displayCategories'][0] == "LIGHT" assert appliance['friendlyName'] == "Test light 1" assert_endpoint_capabilities(appliance, 'Alexa.PowerController') - yield from assert_power_controller_works( + await assert_power_controller_works( 'light#test_1', 'light.turn_on', 'light.turn_off', hass) -@asyncio.coroutine -def test_dimmable_light(hass): +async def test_dimmable_light(hass): """Test dimmable light discovery.""" device = ( 'light.test_2', 'on', { 'brightness': 128, 'friendly_name': "Test light 2", 'supported_features': 1 }) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'light#test_2' assert appliance['displayCategories'][0] == "LIGHT" @@ -215,11 +209,11 @@ def test_dimmable_light(hass): 'Alexa.PowerController', ) - properties = yield from reported_properties(hass, 'light#test_2') + properties = await reported_properties(hass, 'light#test_2') properties.assert_equal('Alexa.PowerController', 'powerState', 'ON') properties.assert_equal('Alexa.BrightnessController', 'brightness', 50) - call, _ = yield from assert_request_calls_service( + call, _ = await assert_request_calls_service( 'Alexa.BrightnessController', 'SetBrightness', 'light#test_2', 'light.turn_on', hass, @@ -227,8 +221,7 @@ def test_dimmable_light(hass): assert call.data['brightness_pct'] == 50 -@asyncio.coroutine -def test_color_light(hass): +async def test_color_light(hass): """Test color light discovery.""" device = ( 'light.test_3', @@ -240,7 +233,7 @@ def test_color_light(hass): 'color_temp': '333', } ) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'light#test_3' assert appliance['displayCategories'][0] == "LIGHT" @@ -258,11 +251,10 @@ def test_color_light(hass): # tests -@asyncio.coroutine -def test_script(hass): +async def test_script(hass): """Test script discovery.""" device = ('script.test', 'off', {'friendly_name': "Test script"}) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'script#test' assert appliance['displayCategories'][0] == "ACTIVITY_TRIGGER" @@ -273,22 +265,21 @@ def test_script(hass): 'Alexa.SceneController') assert not capability['supportsDeactivation'] - yield from assert_scene_controller_works( + await assert_scene_controller_works( 'script#test', 'script.turn_on', None, hass) -@asyncio.coroutine -def test_cancelable_script(hass): +async def test_cancelable_script(hass): """Test cancalable script discovery.""" device = ( 'script.test_2', 'off', {'friendly_name': "Test script 2", 'can_cancel': True}, ) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'script#test_2' (capability,) = assert_endpoint_capabilities( @@ -296,40 +287,38 @@ def test_cancelable_script(hass): 'Alexa.SceneController') assert capability['supportsDeactivation'] - yield from assert_scene_controller_works( + await assert_scene_controller_works( 'script#test_2', 'script.turn_on', 'script.turn_off', hass) -@asyncio.coroutine -def test_input_boolean(hass): +async def test_input_boolean(hass): """Test input boolean discovery.""" device = ( 'input_boolean.test', 'off', {'friendly_name': "Test input boolean"}, ) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'input_boolean#test' assert appliance['displayCategories'][0] == "OTHER" assert appliance['friendlyName'] == "Test input boolean" assert_endpoint_capabilities(appliance, 'Alexa.PowerController') - yield from assert_power_controller_works( + await assert_power_controller_works( 'input_boolean#test', 'input_boolean.turn_on', 'input_boolean.turn_off', hass) -@asyncio.coroutine -def test_scene(hass): +async def test_scene(hass): """Test scene discovery.""" device = ('scene.test', 'off', {'friendly_name': "Test scene"}) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'scene#test' assert appliance['displayCategories'][0] == "SCENE_TRIGGER" @@ -340,18 +329,17 @@ def test_scene(hass): 'Alexa.SceneController') assert not capability['supportsDeactivation'] - yield from assert_scene_controller_works( + await assert_scene_controller_works( 'scene#test', 'scene.turn_on', None, hass) -@asyncio.coroutine -def test_fan(hass): +async def test_fan(hass): """Test fan discovery.""" device = ('fan.test_1', 'off', {'friendly_name': "Test fan 1"}) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'fan#test_1' assert appliance['displayCategories'][0] == "OTHER" @@ -359,8 +347,7 @@ def test_fan(hass): assert_endpoint_capabilities(appliance, 'Alexa.PowerController') -@asyncio.coroutine -def test_variable_fan(hass): +async def test_variable_fan(hass): """Test fan discovery. This one has variable speed. @@ -374,7 +361,7 @@ def test_variable_fan(hass): 'speed': 'high', } ) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'fan#test_2' assert appliance['displayCategories'][0] == "OTHER" @@ -386,14 +373,14 @@ def test_variable_fan(hass): 'Alexa.PowerController', ) - call, _ = yield from assert_request_calls_service( + call, _ = await assert_request_calls_service( 'Alexa.PercentageController', 'SetPercentage', 'fan#test_2', 'fan.set_speed', hass, payload={'percentage': '50'}) assert call.data['speed'] == 'medium' - yield from assert_percentage_changes( + await assert_percentage_changes( hass, [('high', '-5'), ('off', '5'), ('low', '-80')], 'Alexa.PercentageController', 'AdjustPercentage', 'fan#test_2', @@ -402,18 +389,17 @@ def test_variable_fan(hass): 'speed') -@asyncio.coroutine -def test_lock(hass): +async def test_lock(hass): """Test lock discovery.""" device = ('lock.test', 'off', {'friendly_name': "Test lock"}) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'lock#test' assert appliance['displayCategories'][0] == "SMARTLOCK" assert appliance['friendlyName'] == "Test lock" assert_endpoint_capabilities(appliance, 'Alexa.LockController') - _, msg = yield from assert_request_calls_service( + _, msg = await assert_request_calls_service( 'Alexa.LockController', 'Lock', 'lock#test', 'lock.lock', hass) @@ -425,8 +411,7 @@ def test_lock(hass): assert properties['value'] == 'LOCKED' -@asyncio.coroutine -def test_media_player(hass): +async def test_media_player(hass): """Test media player discovery.""" device = ( 'media_player.test', @@ -436,7 +421,7 @@ def test_media_player(hass): 'volume_level': 0.75 } ) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'media_player#test' assert appliance['displayCategories'][0] == "TV" @@ -451,59 +436,59 @@ def test_media_player(hass): 'Alexa.PlaybackController', ) - yield from assert_power_controller_works( + await assert_power_controller_works( 'media_player#test', 'media_player.turn_on', 'media_player.turn_off', hass) - yield from assert_request_calls_service( + await assert_request_calls_service( 'Alexa.PlaybackController', 'Play', 'media_player#test', 'media_player.media_play', hass) - yield from assert_request_calls_service( + await assert_request_calls_service( 'Alexa.PlaybackController', 'Pause', 'media_player#test', 'media_player.media_pause', hass) - yield from assert_request_calls_service( + await assert_request_calls_service( 'Alexa.PlaybackController', 'Stop', 'media_player#test', 'media_player.media_stop', hass) - yield from assert_request_calls_service( + await assert_request_calls_service( 'Alexa.PlaybackController', 'Next', 'media_player#test', 'media_player.media_next_track', hass) - yield from assert_request_calls_service( + await assert_request_calls_service( 'Alexa.PlaybackController', 'Previous', 'media_player#test', 'media_player.media_previous_track', hass) - call, _ = yield from assert_request_calls_service( + call, _ = await assert_request_calls_service( 'Alexa.Speaker', 'SetVolume', 'media_player#test', 'media_player.volume_set', hass, payload={'volume': 50}) assert call.data['volume_level'] == 0.5 - call, _ = yield from assert_request_calls_service( + call, _ = await assert_request_calls_service( 'Alexa.Speaker', 'SetMute', 'media_player#test', 'media_player.volume_mute', hass, payload={'mute': True}) assert call.data['is_volume_muted'] - call, _, = yield from assert_request_calls_service( + call, _, = await assert_request_calls_service( 'Alexa.Speaker', 'SetMute', 'media_player#test', 'media_player.volume_mute', hass, payload={'mute': False}) assert not call.data['is_volume_muted'] - yield from assert_percentage_changes( + await assert_percentage_changes( hass, [(0.7, '-5'), (0.8, '5'), (0, '-80')], 'Alexa.Speaker', 'AdjustVolume', 'media_player#test', @@ -511,89 +496,85 @@ def test_media_player(hass): 'media_player.volume_set', 'volume_level') - call, _ = yield from assert_request_calls_service( + call, _ = await assert_request_calls_service( 'Alexa.StepSpeaker', 'SetMute', 'media_player#test', 'media_player.volume_mute', hass, payload={'mute': True}) assert call.data['is_volume_muted'] - call, _, = yield from assert_request_calls_service( + call, _, = await assert_request_calls_service( 'Alexa.StepSpeaker', 'SetMute', 'media_player#test', 'media_player.volume_mute', hass, payload={'mute': False}) assert not call.data['is_volume_muted'] - call, _ = yield from assert_request_calls_service( + call, _ = await assert_request_calls_service( 'Alexa.StepSpeaker', 'AdjustVolume', 'media_player#test', 'media_player.volume_up', hass, payload={'volumeSteps': 20}) - call, _ = yield from assert_request_calls_service( + call, _ = await assert_request_calls_service( 'Alexa.StepSpeaker', 'AdjustVolume', 'media_player#test', 'media_player.volume_down', hass, payload={'volumeSteps': -20}) -@asyncio.coroutine -def test_alert(hass): +async def test_alert(hass): """Test alert discovery.""" device = ('alert.test', 'off', {'friendly_name': "Test alert"}) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'alert#test' assert appliance['displayCategories'][0] == "OTHER" assert appliance['friendlyName'] == "Test alert" assert_endpoint_capabilities(appliance, 'Alexa.PowerController') - yield from assert_power_controller_works( + await assert_power_controller_works( 'alert#test', 'alert.turn_on', 'alert.turn_off', hass) -@asyncio.coroutine -def test_automation(hass): +async def test_automation(hass): """Test automation discovery.""" device = ('automation.test', 'off', {'friendly_name': "Test automation"}) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'automation#test' assert appliance['displayCategories'][0] == "OTHER" assert appliance['friendlyName'] == "Test automation" assert_endpoint_capabilities(appliance, 'Alexa.PowerController') - yield from assert_power_controller_works( + await assert_power_controller_works( 'automation#test', 'automation.turn_on', 'automation.turn_off', hass) -@asyncio.coroutine -def test_group(hass): +async def test_group(hass): """Test group discovery.""" device = ('group.test', 'off', {'friendly_name': "Test group"}) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'group#test' assert appliance['displayCategories'][0] == "OTHER" assert appliance['friendlyName'] == "Test group" assert_endpoint_capabilities(appliance, 'Alexa.PowerController') - yield from assert_power_controller_works( + await assert_power_controller_works( 'group#test', 'homeassistant.turn_on', 'homeassistant.turn_off', hass) -@asyncio.coroutine -def test_cover(hass): +async def test_cover(hass): """Test cover discovery.""" device = ( 'cover.test', @@ -603,7 +584,7 @@ def test_cover(hass): 'position': 30, } ) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'cover#test' assert appliance['displayCategories'][0] == "DOOR" @@ -615,20 +596,20 @@ def test_cover(hass): 'Alexa.PowerController', ) - yield from assert_power_controller_works( + await assert_power_controller_works( 'cover#test', 'cover.open_cover', 'cover.close_cover', hass) - call, _ = yield from assert_request_calls_service( + call, _ = await assert_request_calls_service( 'Alexa.PercentageController', 'SetPercentage', 'cover#test', 'cover.set_cover_position', hass, payload={'percentage': '50'}) assert call.data['position'] == 50 - yield from assert_percentage_changes( + await assert_percentage_changes( hass, [(25, '-5'), (35, '5'), (0, '-80')], 'Alexa.PercentageController', 'AdjustPercentage', 'cover#test', @@ -637,8 +618,7 @@ def test_cover(hass): 'position') -@asyncio.coroutine -def assert_percentage_changes( +async def assert_percentage_changes( hass, adjustments, namespace, @@ -657,15 +637,14 @@ def assert_percentage_changes( else: payload = {} - call, _ = yield from assert_request_calls_service( + call, _ = await assert_request_calls_service( namespace, name, endpoint, service, hass, payload=payload) assert call.data[changed_parameter] == result_volume -@asyncio.coroutine -def test_temp_sensor(hass): +async def test_temp_sensor(hass): """Test temperature sensor discovery.""" device = ( 'sensor.test_temp', @@ -675,7 +654,7 @@ def test_temp_sensor(hass): 'unit_of_measurement': TEMP_FAHRENHEIT, } ) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'sensor#test_temp' assert appliance['displayCategories'][0] == 'TEMPERATURE_SENSOR' @@ -689,13 +668,12 @@ def test_temp_sensor(hass): assert properties['retrievable'] is True assert {'name': 'temperature'} in properties['supported'] - properties = yield from reported_properties(hass, 'sensor#test_temp') + properties = await reported_properties(hass, 'sensor#test_temp') properties.assert_equal('Alexa.TemperatureSensor', 'temperature', {'value': 42.0, 'scale': 'FAHRENHEIT'}) -@asyncio.coroutine -def test_contact_sensor(hass): +async def test_contact_sensor(hass): """Test contact sensor discovery.""" device = ( 'binary_sensor.test_contact', @@ -705,7 +683,7 @@ def test_contact_sensor(hass): 'device_class': 'door', } ) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'binary_sensor#test_contact' assert appliance['displayCategories'][0] == 'CONTACT_SENSOR' @@ -719,14 +697,13 @@ def test_contact_sensor(hass): assert properties['retrievable'] is True assert {'name': 'detectionState'} in properties['supported'] - properties = yield from reported_properties(hass, - 'binary_sensor#test_contact') + properties = await reported_properties(hass, + 'binary_sensor#test_contact') properties.assert_equal('Alexa.ContactSensor', 'detectionState', 'DETECTED') -@asyncio.coroutine -def test_motion_sensor(hass): +async def test_motion_sensor(hass): """Test motion sensor discovery.""" device = ( 'binary_sensor.test_motion', @@ -736,7 +713,7 @@ def test_motion_sensor(hass): 'device_class': 'motion', } ) - appliance = yield from discovery_test(device, hass) + appliance = await discovery_test(device, hass) assert appliance['endpointId'] == 'binary_sensor#test_motion' assert appliance['displayCategories'][0] == 'MOTION_SENSOR' @@ -750,21 +727,20 @@ def test_motion_sensor(hass): assert properties['retrievable'] is True assert {'name': 'detectionState'} in properties['supported'] - properties = yield from reported_properties(hass, - 'binary_sensor#test_motion') + properties = await reported_properties(hass, + 'binary_sensor#test_motion') properties.assert_equal('Alexa.MotionSensor', 'detectionState', 'DETECTED') -@asyncio.coroutine -def test_unknown_sensor(hass): +async def test_unknown_sensor(hass): """Test sensors of unknown quantities are not discovered.""" device = ( 'sensor.test_sickness', '0.1', { 'friendly_name': "Test Space Sickness Sensor", 'unit_of_measurement': 'garn', }) - yield from discovery_test(device, hass, expected_endpoints=0) + await discovery_test(device, hass, expected_endpoints=0) async def test_thermostat(hass): @@ -911,8 +887,7 @@ async def test_thermostat(hass): assert call.data['operation_mode'] == 'off' -@asyncio.coroutine -def test_exclude_filters(hass): +async def test_exclude_filters(hass): """Test exclusion filters.""" request = get_new_request('Alexa.Discovery', 'Discover') @@ -933,16 +908,15 @@ def test_exclude_filters(hass): exclude_entities=['cover.deny'], )) - msg = yield from smart_home.async_handle_message(hass, config, request) - yield from hass.async_block_till_done() + msg = await smart_home.async_handle_message(hass, config, request) + await hass.async_block_till_done() msg = msg['event'] assert len(msg['payload']['endpoints']) == 1 -@asyncio.coroutine -def test_include_filters(hass): +async def test_include_filters(hass): """Test inclusion filters.""" request = get_new_request('Alexa.Discovery', 'Discover') @@ -966,24 +940,23 @@ def test_include_filters(hass): exclude_entities=[], )) - msg = yield from smart_home.async_handle_message(hass, config, request) - yield from hass.async_block_till_done() + msg = await smart_home.async_handle_message(hass, config, request) + await hass.async_block_till_done() msg = msg['event'] assert len(msg['payload']['endpoints']) == 3 -@asyncio.coroutine -def test_api_entity_not_exists(hass): +async def test_api_entity_not_exists(hass): """Test api turn on process without entity.""" request = get_new_request('Alexa.PowerController', 'TurnOn', 'switch#test') call_switch = async_mock_service(hass, 'switch', 'turn_on') - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request) - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert 'event' in msg msg = msg['event'] @@ -994,11 +967,10 @@ def test_api_entity_not_exists(hass): assert msg['payload']['type'] == 'NO_SUCH_ENDPOINT' -@asyncio.coroutine -def test_api_function_not_implemented(hass): +async def test_api_function_not_implemented(hass): """Test api call that is not implemented to us.""" request = get_new_request('Alexa.HAHAAH', 'Sweet') - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request) assert 'event' in msg @@ -1009,8 +981,7 @@ def test_api_function_not_implemented(hass): assert msg['payload']['type'] == 'INTERNAL_ERROR' -@asyncio.coroutine -def assert_request_fails( +async def assert_request_fails( namespace, name, endpoint, @@ -1025,9 +996,9 @@ def assert_request_fails( domain, service_name = service_not_called.split('.') call = async_mock_service(hass, domain, service_name) - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request) - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert not call assert 'event' in msg @@ -1036,8 +1007,7 @@ def assert_request_fails( return msg -@asyncio.coroutine -def assert_request_calls_service( +async def assert_request_calls_service( namespace, name, endpoint, @@ -1054,9 +1024,9 @@ def assert_request_calls_service( domain, service_name = service.split('.') calls = async_mock_service(hass, domain, service_name) - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request, context) - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert len(calls) == 1 call = calls[0] @@ -1068,26 +1038,29 @@ def assert_request_calls_service( return call, msg -@asyncio.coroutine -def assert_power_controller_works(endpoint, on_service, off_service, hass): +async def assert_power_controller_works( + endpoint, + on_service, + off_service, + hass +): """Assert PowerController API requests work.""" - yield from assert_request_calls_service( + await assert_request_calls_service( 'Alexa.PowerController', 'TurnOn', endpoint, on_service, hass) - yield from assert_request_calls_service( + await assert_request_calls_service( 'Alexa.PowerController', 'TurnOff', endpoint, off_service, hass) -@asyncio.coroutine -def assert_scene_controller_works( +async def assert_scene_controller_works( endpoint, activate_service, deactivate_service, hass): """Assert SceneController API requests work.""" - _, response = yield from assert_request_calls_service( + _, response = await assert_request_calls_service( 'Alexa.SceneController', 'Activate', endpoint, activate_service, hass, response_type='ActivationStarted') @@ -1095,7 +1068,7 @@ def assert_scene_controller_works( assert 'timestamp' in response['event']['payload'] if deactivate_service: - yield from assert_request_calls_service( + await assert_request_calls_service( 'Alexa.SceneController', 'Deactivate', endpoint, deactivate_service, hass, response_type='DeactivationStarted') @@ -1104,10 +1077,9 @@ def assert_scene_controller_works( assert 'timestamp' in response['event']['payload'] -@asyncio.coroutine @pytest.mark.parametrize( "result,adjust", [(25, '-5'), (35, '5'), (0, '-80')]) -def test_api_adjust_brightness(hass, result, adjust): +async def test_api_adjust_brightness(hass, result, adjust): """Test api adjust brightness process.""" request = get_new_request( 'Alexa.BrightnessController', 'AdjustBrightness', 'light#test') @@ -1123,9 +1095,9 @@ def test_api_adjust_brightness(hass, result, adjust): call_light = async_mock_service(hass, 'light', 'turn_on') - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request) - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert 'event' in msg msg = msg['event'] @@ -1136,8 +1108,7 @@ def test_api_adjust_brightness(hass, result, adjust): assert msg['header']['name'] == 'Response' -@asyncio.coroutine -def test_api_set_color_rgb(hass): +async def test_api_set_color_rgb(hass): """Test api set color process.""" request = get_new_request( 'Alexa.ColorController', 'SetColor', 'light#test') @@ -1158,9 +1129,9 @@ def test_api_set_color_rgb(hass): call_light = async_mock_service(hass, 'light', 'turn_on') - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request) - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert 'event' in msg msg = msg['event'] @@ -1171,8 +1142,7 @@ def test_api_set_color_rgb(hass): assert msg['header']['name'] == 'Response' -@asyncio.coroutine -def test_api_set_color_temperature(hass): +async def test_api_set_color_temperature(hass): """Test api set color temperature process.""" request = get_new_request( 'Alexa.ColorTemperatureController', 'SetColorTemperature', @@ -1187,9 +1157,9 @@ def test_api_set_color_temperature(hass): call_light = async_mock_service(hass, 'light', 'turn_on') - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request) - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert 'event' in msg msg = msg['event'] @@ -1200,9 +1170,8 @@ def test_api_set_color_temperature(hass): assert msg['header']['name'] == 'Response' -@asyncio.coroutine @pytest.mark.parametrize("result,initial", [(383, '333'), (500, '500')]) -def test_api_decrease_color_temp(hass, result, initial): +async def test_api_decrease_color_temp(hass, result, initial): """Test api decrease color temp process.""" request = get_new_request( 'Alexa.ColorTemperatureController', 'DecreaseColorTemperature', @@ -1217,9 +1186,9 @@ def test_api_decrease_color_temp(hass, result, initial): call_light = async_mock_service(hass, 'light', 'turn_on') - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request) - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert 'event' in msg msg = msg['event'] @@ -1230,9 +1199,8 @@ def test_api_decrease_color_temp(hass, result, initial): assert msg['header']['name'] == 'Response' -@asyncio.coroutine @pytest.mark.parametrize("result,initial", [(283, '333'), (142, '142')]) -def test_api_increase_color_temp(hass, result, initial): +async def test_api_increase_color_temp(hass, result, initial): """Test api increase color temp process.""" request = get_new_request( 'Alexa.ColorTemperatureController', 'IncreaseColorTemperature', @@ -1247,9 +1215,9 @@ def test_api_increase_color_temp(hass, result, initial): call_light = async_mock_service(hass, 'light', 'turn_on') - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request) - yield from hass.async_block_till_done() + await hass.async_block_till_done() assert 'event' in msg msg = msg['event'] @@ -1260,8 +1228,7 @@ def test_api_increase_color_temp(hass, result, initial): assert msg['header']['name'] == 'Response' -@asyncio.coroutine -def test_report_lock_state(hass): +async def test_report_lock_state(hass): """Test LockController implements lockState property.""" hass.states.async_set( 'lock.locked', STATE_LOCKED, {}) @@ -1270,18 +1237,17 @@ def test_report_lock_state(hass): hass.states.async_set( 'lock.unknown', STATE_UNKNOWN, {}) - properties = yield from reported_properties(hass, 'lock.locked') + properties = await reported_properties(hass, 'lock.locked') properties.assert_equal('Alexa.LockController', 'lockState', 'LOCKED') - properties = yield from reported_properties(hass, 'lock.unlocked') + properties = await reported_properties(hass, 'lock.unlocked') properties.assert_equal('Alexa.LockController', 'lockState', 'UNLOCKED') - properties = yield from reported_properties(hass, 'lock.unknown') + properties = await reported_properties(hass, 'lock.unknown') properties.assert_equal('Alexa.LockController', 'lockState', 'JAMMED') -@asyncio.coroutine -def test_report_dimmable_light_state(hass): +async def test_report_dimmable_light_state(hass): """Test BrightnessController reports brightness correctly.""" hass.states.async_set( 'light.test_on', 'on', {'friendly_name': "Test light On", @@ -1290,24 +1256,23 @@ def test_report_dimmable_light_state(hass): 'light.test_off', 'off', {'friendly_name': "Test light Off", 'supported_features': 1}) - properties = yield from reported_properties(hass, 'light.test_on') + properties = await reported_properties(hass, 'light.test_on') properties.assert_equal('Alexa.BrightnessController', 'brightness', 50) - properties = yield from reported_properties(hass, 'light.test_off') + properties = await reported_properties(hass, 'light.test_off') properties.assert_equal('Alexa.BrightnessController', 'brightness', 0) -@asyncio.coroutine -def reported_properties(hass, endpoint): +async def reported_properties(hass, endpoint): """Use ReportState to get properties and return them. The result is a _ReportedProperties instance, which has methods to make assertions about the properties. """ request = get_new_request('Alexa', 'ReportState', endpoint) - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request) - yield from hass.async_block_till_done() + await hass.async_block_till_done() return _ReportedProperties(msg['context']['properties']) @@ -1329,8 +1294,7 @@ class _ReportedProperties: ) -@asyncio.coroutine -def test_entity_config(hass): +async def test_entity_config(hass): """Test that we can configure things via entity config.""" request = get_new_request('Alexa.Discovery', 'Discover') @@ -1348,7 +1312,7 @@ def test_entity_config(hass): } ) - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, config, request) assert 'event' in msg @@ -1366,15 +1330,14 @@ def test_entity_config(hass): 'Alexa.PowerController' -@asyncio.coroutine -def test_unsupported_domain(hass): +async def test_unsupported_domain(hass): """Discovery ignores entities of unknown domains.""" request = get_new_request('Alexa.Discovery', 'Discover') hass.states.async_set( 'woz.boop', 'on', {'friendly_name': "Boop Woz"}) - msg = yield from smart_home.async_handle_message( + msg = await smart_home.async_handle_message( hass, DEFAULT_CONFIG, request) assert 'event' in msg @@ -1383,22 +1346,20 @@ def test_unsupported_domain(hass): assert not msg['payload']['endpoints'] -@asyncio.coroutine -def do_http_discovery(config, hass, aiohttp_client): +async def do_http_discovery(config, hass, aiohttp_client): """Submit a request to the Smart Home HTTP API.""" - yield from async_setup_component(hass, alexa.DOMAIN, config) - http_client = yield from aiohttp_client(hass.http.app) + await async_setup_component(hass, alexa.DOMAIN, config) + http_client = await aiohttp_client(hass.http.app) request = get_new_request('Alexa.Discovery', 'Discover') - response = yield from http_client.post( + response = await http_client.post( smart_home.SMART_HOME_HTTP_ENDPOINT, data=json.dumps(request), headers={'content-type': 'application/json'}) return response -@asyncio.coroutine -def test_http_api(hass, aiohttp_client): +async def test_http_api(hass, aiohttp_client): """With `smart_home:` HTTP API is exposed.""" config = { 'alexa': { @@ -1406,26 +1367,24 @@ def test_http_api(hass, aiohttp_client): } } - response = yield from do_http_discovery(config, hass, aiohttp_client) - response_data = yield from response.json() + response = await do_http_discovery(config, hass, aiohttp_client) + response_data = await response.json() # Here we're testing just the HTTP view glue -- details of discovery are # covered in other tests. assert response_data['event']['header']['name'] == 'Discover.Response' -@asyncio.coroutine -def test_http_api_disabled(hass, aiohttp_client): +async def test_http_api_disabled(hass, aiohttp_client): """Without `smart_home:`, the HTTP API is disabled.""" config = { 'alexa': {} } - response = yield from do_http_discovery(config, hass, aiohttp_client) + response = await do_http_discovery(config, hass, aiohttp_client) assert response.status == 404 -@asyncio.coroutine @pytest.mark.parametrize( "domain,payload,source_list,idx", [ ('media_player', 'GAME CONSOLE', ['tv', 'game console'], 1), @@ -1434,7 +1393,7 @@ def test_http_api_disabled(hass, aiohttp_client): ('media_player', 'BAD DEVICE', ['satellite_tv', 'game console'], None), ] ) -def test_api_select_input(hass, domain, payload, source_list, idx): +async def test_api_select_input(hass, domain, payload, source_list, idx): """Test api set input process.""" hass.states.async_set( 'media_player.test', 'off', { @@ -1445,14 +1404,14 @@ def test_api_select_input(hass, domain, payload, source_list, idx): # test where no source matches if idx is None: - yield from assert_request_fails( + await assert_request_fails( 'Alexa.InputController', 'SelectInput', 'media_player#test', 'media_player.select_source', hass, payload={'input': payload}) return - call, _ = yield from assert_request_calls_service( + call, _ = await assert_request_calls_service( 'Alexa.InputController', 'SelectInput', 'media_player#test', 'media_player.select_source', hass,