From 590eead128835e1b693ebb6b3020f59ef19fc4bd Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 8 Apr 2019 23:16:55 -0700 Subject: [PATCH] Test fixes (#22911) * Fix light tests [skip ci] * Fix tests/common * Fix some mqtt tests [skip ci] * Fix tests and component manifests which have only one platform * Fix more tests and manifests * Fix demo/notify tests * Rollback test for demo.geo_location --- .../components/ffmpeg_motion/manifest.json | 2 +- .../components/ffmpeg_noise/manifest.json | 2 +- homeassistant/components/html5/manifest.json | 2 +- homeassistant/components/meraki/manifest.json | 2 +- .../microsoft_face_detect/manifest.json | 2 +- .../microsoft_face_identify/manifest.json | 2 +- homeassistant/components/push/manifest.json | 2 +- tests/common.py | 10 ++++---- tests/components/demo/test_geo_location.py | 4 +++- tests/components/demo/test_notify.py | 1 + tests/components/ffmpeg/test_sensor.py | 6 +++++ tests/components/geofency/test_init.py | 23 ++++++++++--------- tests/components/gpslogger/test_init.py | 22 ++++++++++-------- tests/components/html5/test_notify.py | 1 + tests/components/light/test_init.py | 8 +++---- tests/components/locative/test_init.py | 10 ++++---- tests/components/mqtt/test_config_flow.py | 1 + tests/components/mqtt/test_init.py | 6 ++--- tests/components/mqtt/test_server.py | 6 ++--- tests/components/tradfri/test_init.py | 4 ++++ 20 files changed, 68 insertions(+), 48 deletions(-) diff --git a/homeassistant/components/ffmpeg_motion/manifest.json b/homeassistant/components/ffmpeg_motion/manifest.json index bc5dfaa4209..e9a0e7b1014 100644 --- a/homeassistant/components/ffmpeg_motion/manifest.json +++ b/homeassistant/components/ffmpeg_motion/manifest.json @@ -3,6 +3,6 @@ "name": "Ffmpeg motion", "documentation": "https://www.home-assistant.io/components/ffmpeg_motion", "requirements": [], - "dependencies": [], + "dependencies": ["ffmpeg"], "codeowners": [] } diff --git a/homeassistant/components/ffmpeg_noise/manifest.json b/homeassistant/components/ffmpeg_noise/manifest.json index 6fdf07899fd..71600b31117 100644 --- a/homeassistant/components/ffmpeg_noise/manifest.json +++ b/homeassistant/components/ffmpeg_noise/manifest.json @@ -3,6 +3,6 @@ "name": "Ffmpeg noise", "documentation": "https://www.home-assistant.io/components/ffmpeg_noise", "requirements": [], - "dependencies": [], + "dependencies": ["ffmpeg"], "codeowners": [] } diff --git a/homeassistant/components/html5/manifest.json b/homeassistant/components/html5/manifest.json index 81e4072e629..7b43ec44ef3 100644 --- a/homeassistant/components/html5/manifest.json +++ b/homeassistant/components/html5/manifest.json @@ -5,7 +5,7 @@ "requirements": [ "pywebpush==1.9.2" ], - "dependencies": [], + "dependencies": ["frontend"], "codeowners": [ "@robbiet480" ] diff --git a/homeassistant/components/meraki/manifest.json b/homeassistant/components/meraki/manifest.json index d17e7c60525..d03679ed41e 100644 --- a/homeassistant/components/meraki/manifest.json +++ b/homeassistant/components/meraki/manifest.json @@ -3,6 +3,6 @@ "name": "Meraki", "documentation": "https://www.home-assistant.io/components/meraki", "requirements": [], - "dependencies": [], + "dependencies": ["http"], "codeowners": [] } diff --git a/homeassistant/components/microsoft_face_detect/manifest.json b/homeassistant/components/microsoft_face_detect/manifest.json index 955b67a0a76..b272a299cf5 100644 --- a/homeassistant/components/microsoft_face_detect/manifest.json +++ b/homeassistant/components/microsoft_face_detect/manifest.json @@ -3,6 +3,6 @@ "name": "Microsoft face detect", "documentation": "https://www.home-assistant.io/components/microsoft_face_detect", "requirements": [], - "dependencies": [], + "dependencies": ["microsoft_face"], "codeowners": [] } diff --git a/homeassistant/components/microsoft_face_identify/manifest.json b/homeassistant/components/microsoft_face_identify/manifest.json index f32b9220b3d..10e4bde103c 100644 --- a/homeassistant/components/microsoft_face_identify/manifest.json +++ b/homeassistant/components/microsoft_face_identify/manifest.json @@ -3,6 +3,6 @@ "name": "Microsoft face identify", "documentation": "https://www.home-assistant.io/components/microsoft_face_identify", "requirements": [], - "dependencies": [], + "dependencies": ["microsoft_face"], "codeowners": [] } diff --git a/homeassistant/components/push/manifest.json b/homeassistant/components/push/manifest.json index 96b9e647e14..278638caff8 100644 --- a/homeassistant/components/push/manifest.json +++ b/homeassistant/components/push/manifest.json @@ -3,7 +3,7 @@ "name": "Push", "documentation": "https://www.home-assistant.io/components/push", "requirements": [], - "dependencies": [], + "dependencies": ["webhook"], "codeowners": [ "@dgomes" ] diff --git a/tests/common.py b/tests/common.py index 9fe5375ad7c..e04c8347c09 100644 --- a/tests/common.py +++ b/tests/common.py @@ -244,7 +244,7 @@ def async_fire_mqtt_message(hass, topic, payload, qos=0, retain=False): if isinstance(payload, str): payload = payload.encode('utf-8') msg = mqtt.Message(topic, payload, qos, retain) - hass.async_run_job(hass.data['mqtt']._mqtt_on_message, None, None, msg) + hass.data['mqtt']._mqtt_handle_message(msg) fire_mqtt_message = threadsafe_callback_factory(async_fire_mqtt_message) @@ -287,8 +287,7 @@ def mock_state_change_event(hass, new_state, old_state=None): hass.bus.fire(EVENT_STATE_CHANGED, event_data, context=new_state.context) -@asyncio.coroutine -def async_mock_mqtt_component(hass, config=None): +async def async_mock_mqtt_component(hass, config=None): """Mock the MQTT component.""" if config is None: config = {mqtt.CONF_BROKER: 'mock-broker'} @@ -299,10 +298,11 @@ def async_mock_mqtt_component(hass, config=None): mock_client().unsubscribe.return_value = (0, 0) mock_client().publish.return_value = (0, 0) - result = yield from async_setup_component(hass, mqtt.DOMAIN, { + result = await async_setup_component(hass, mqtt.DOMAIN, { mqtt.DOMAIN: config }) assert result + await hass.async_block_till_done() hass.data['mqtt'] = MagicMock(spec_set=hass.data['mqtt'], wraps=hass.data['mqtt']) @@ -708,7 +708,7 @@ def assert_setup_component(count, domain=None): yield config if domain is None: - assert len(config) == 1, ('assert_setup_component requires DOMAIN: {}' + assert len(config) >= 1, ('assert_setup_component requires DOMAIN: {}' .format(list(config.keys()))) domain = list(config.keys())[0] diff --git a/tests/components/demo/test_geo_location.py b/tests/components/demo/test_geo_location.py index 5a46ca99839..c4d01b812f8 100644 --- a/tests/components/demo/test_geo_location.py +++ b/tests/components/demo/test_geo_location.py @@ -38,10 +38,12 @@ class TestDemoPlatform(unittest.TestCase): with patch('homeassistant.util.dt.utcnow', return_value=utcnow): with assert_setup_component(1, geo_location.DOMAIN): assert setup_component(self.hass, geo_location.DOMAIN, CONFIG) + self.hass.block_till_done() - # In this test, only entities of the geolocation domain have been + # In this test, five geolocation entities have been # generated. all_states = self.hass.states.all() + print(all_states) assert len(all_states) == NUMBER_OF_DEMO_DEVICES # Check a single device's attributes. diff --git a/tests/components/demo/test_notify.py b/tests/components/demo/test_notify.py index 35cf8abe6bd..964612cb977 100644 --- a/tests/components/demo/test_notify.py +++ b/tests/components/demo/test_notify.py @@ -45,6 +45,7 @@ class TestNotifyDemo(unittest.TestCase): with assert_setup_component(1) as config: assert setup_component(self.hass, notify.DOMAIN, CONFIG) assert config[notify.DOMAIN] + self.hass.block_till_done() def test_setup(self): """Test setup.""" diff --git a/tests/components/ffmpeg/test_sensor.py b/tests/components/ffmpeg/test_sensor.py index d1fd6124b4c..19c497514b7 100644 --- a/tests/components/ffmpeg/test_sensor.py +++ b/tests/components/ffmpeg/test_sensor.py @@ -29,6 +29,7 @@ class TestFFmpegNoiseSetup: """Set up ffmpeg component.""" with assert_setup_component(1, 'binary_sensor'): setup_component(self.hass, 'binary_sensor', self.config) + self.hass.block_till_done() assert self.hass.data['ffmpeg'].binary == 'ffmpeg' assert self.hass.states.get('binary_sensor.ffmpeg_noise') is not None @@ -39,6 +40,7 @@ class TestFFmpegNoiseSetup: """Set up ffmpeg component.""" with assert_setup_component(1, 'binary_sensor'): setup_component(self.hass, 'binary_sensor', self.config) + self.hass.block_till_done() assert self.hass.data['ffmpeg'].binary == 'ffmpeg' assert self.hass.states.get('binary_sensor.ffmpeg_noise') is not None @@ -54,6 +56,7 @@ class TestFFmpegNoiseSetup: """Set up ffmpeg component.""" with assert_setup_component(1, 'binary_sensor'): setup_component(self.hass, 'binary_sensor', self.config) + self.hass.block_till_done() assert self.hass.data['ffmpeg'].binary == 'ffmpeg' assert self.hass.states.get('binary_sensor.ffmpeg_noise') is not None @@ -92,6 +95,7 @@ class TestFFmpegMotionSetup: """Set up ffmpeg component.""" with assert_setup_component(1, 'binary_sensor'): setup_component(self.hass, 'binary_sensor', self.config) + self.hass.block_till_done() assert self.hass.data['ffmpeg'].binary == 'ffmpeg' assert self.hass.states.get('binary_sensor.ffmpeg_motion') is not None @@ -102,6 +106,7 @@ class TestFFmpegMotionSetup: """Set up ffmpeg component.""" with assert_setup_component(1, 'binary_sensor'): setup_component(self.hass, 'binary_sensor', self.config) + self.hass.block_till_done() assert self.hass.data['ffmpeg'].binary == 'ffmpeg' assert self.hass.states.get('binary_sensor.ffmpeg_motion') is not None @@ -117,6 +122,7 @@ class TestFFmpegMotionSetup: """Set up ffmpeg component.""" with assert_setup_component(1, 'binary_sensor'): setup_component(self.hass, 'binary_sensor', self.config) + self.hass.block_till_done() assert self.hass.data['ffmpeg'].binary == 'ffmpeg' assert self.hass.states.get('binary_sensor.ffmpeg_motion') is not None diff --git a/tests/components/geofency/test_init.py b/tests/components/geofency/test_init.py index dd87a6d9503..98edd8b3af1 100644 --- a/tests/components/geofency/test_init.py +++ b/tests/components/geofency/test_init.py @@ -113,34 +113,34 @@ def mock_dev_track(mock_device_tracker_conf): @pytest.fixture -def geofency_client(loop, hass, aiohttp_client): +async def geofency_client(loop, hass, aiohttp_client): """Geofency mock client (unauthenticated).""" - assert loop.run_until_complete(async_setup_component( - hass, 'persistent_notification', {})) + assert await async_setup_component( + hass, 'persistent_notification', {}) - assert loop.run_until_complete(async_setup_component( + assert await async_setup_component( hass, DOMAIN, { DOMAIN: { CONF_MOBILE_BEACONS: ['Car 1'] - }})) - - loop.run_until_complete(hass.async_block_till_done()) + }}) + await hass.async_block_till_done() with patch('homeassistant.components.device_tracker.update_config'): - yield loop.run_until_complete(aiohttp_client(hass.http.app)) + return await aiohttp_client(hass.http.app) @pytest.fixture(autouse=True) -def setup_zones(loop, hass): +async def setup_zones(loop, hass): """Set up Zone config in HA.""" - assert loop.run_until_complete(async_setup_component( + assert await async_setup_component( hass, zone.DOMAIN, { 'zone': { 'name': 'Home', 'latitude': HOME_LATITUDE, 'longitude': HOME_LONGITUDE, 'radius': 100, - }})) + }}) + await hass.async_block_till_done() @pytest.fixture @@ -156,6 +156,7 @@ async def webhook_id(hass, geofency_client): result['flow_id'], {}) assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + await hass.async_block_till_done() return result['result'].data['webhook_id'] diff --git a/tests/components/gpslogger/test_init.py b/tests/components/gpslogger/test_init.py index 577da5f33e6..fce93d0a774 100644 --- a/tests/components/gpslogger/test_init.py +++ b/tests/components/gpslogger/test_init.py @@ -26,31 +26,34 @@ def mock_dev_track(mock_device_tracker_conf): @pytest.fixture -def gpslogger_client(loop, hass, aiohttp_client): +async def gpslogger_client(loop, hass, aiohttp_client): """Mock client for GPSLogger (unauthenticated).""" - assert loop.run_until_complete(async_setup_component( - hass, 'persistent_notification', {})) + assert await async_setup_component( + hass, 'persistent_notification', {}) - assert loop.run_until_complete(async_setup_component( + assert await async_setup_component( hass, DOMAIN, { DOMAIN: {} - })) + }) + + await hass.async_block_till_done() with patch('homeassistant.components.device_tracker.update_config'): - yield loop.run_until_complete(aiohttp_client(hass.http.app)) + return await aiohttp_client(hass.http.app) @pytest.fixture(autouse=True) -def setup_zones(loop, hass): +async def setup_zones(loop, hass): """Set up Zone config in HA.""" - assert loop.run_until_complete(async_setup_component( + assert await async_setup_component( hass, zone.DOMAIN, { 'zone': { 'name': 'Home', 'latitude': HOME_LATITUDE, 'longitude': HOME_LONGITUDE, 'radius': 100, - }})) + }}) + await hass.async_block_till_done() @pytest.fixture @@ -66,6 +69,7 @@ async def webhook_id(hass, gpslogger_client): result['flow_id'], {}) assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + await hass.async_block_till_done() return result['result'].data['webhook_id'] diff --git a/tests/components/html5/test_notify.py b/tests/components/html5/test_notify.py index 140544bf9ea..cae4db6434a 100644 --- a/tests/components/html5/test_notify.py +++ b/tests/components/html5/test_notify.py @@ -61,6 +61,7 @@ async def mock_client(hass, hass_client, registrations=None): 'platform': 'html5' } }) + await hass.async_block_till_done() return await hass_client() diff --git a/tests/components/light/test_init.py b/tests/components/light/test_init.py index 0025e9bce66..90f2651080c 100644 --- a/tests/components/light/test_init.py +++ b/tests/components/light/test_init.py @@ -374,10 +374,10 @@ class TestLight(unittest.TestCase): return True return real_isfile(path) - def _mock_open(path): + def _mock_open(path, *args, **kwargs): if path == user_light_file: return StringIO(profile_data) - return real_open(path) + return real_open(path, *args, **kwargs) profile_data = "id,x,y,brightness\n" +\ "group.all_lights.default,.4,.6,99\n" @@ -412,10 +412,10 @@ class TestLight(unittest.TestCase): return True return real_isfile(path) - def _mock_open(path): + def _mock_open(path, *args, **kwargs): if path == user_light_file: return StringIO(profile_data) - return real_open(path) + return real_open(path, *args, **kwargs) profile_data = "id,x,y,brightness\n" +\ "group.all_lights.default,.3,.5,200\n" +\ diff --git a/tests/components/locative/test_init.py b/tests/components/locative/test_init.py index f757080eadc..6d541cac653 100644 --- a/tests/components/locative/test_init.py +++ b/tests/components/locative/test_init.py @@ -22,15 +22,16 @@ def mock_dev_track(mock_device_tracker_conf): @pytest.fixture -def locative_client(loop, hass, hass_client): +async def locative_client(loop, hass, hass_client): """Locative mock client.""" - assert loop.run_until_complete(async_setup_component( + assert await async_setup_component( hass, DOMAIN, { DOMAIN: {} - })) + }) + await hass.async_block_till_done() with patch('homeassistant.components.device_tracker.update_config'): - yield loop.run_until_complete(hass_client()) + return await hass_client() @pytest.fixture @@ -45,6 +46,7 @@ async def webhook_id(hass, locative_client): result = await hass.config_entries.flow.async_configure( result['flow_id'], {}) assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + await hass.async_block_till_done() return result['result'].data['webhook_id'] diff --git a/tests/components/mqtt/test_config_flow.py b/tests/components/mqtt/test_config_flow.py index 9d822ba854b..64196c9febd 100644 --- a/tests/components/mqtt/test_config_flow.py +++ b/tests/components/mqtt/test_config_flow.py @@ -81,6 +81,7 @@ async def test_manual_config_set(hass, mock_try_connection, """Test we ignore entry if manual config available.""" assert await async_setup_component( hass, 'mqtt', {'mqtt': {'broker': 'bla'}}) + await hass.async_block_till_done() assert len(mock_finish_setup.mock_calls) == 1 mock_try_connection.return_value = True diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 144ee9c43d8..dc9299e4a35 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -28,8 +28,7 @@ def mock_MQTT(): yield mock_MQTT -@asyncio.coroutine -def async_mock_mqtt_client(hass, config=None): +async def async_mock_mqtt_client(hass, config=None): """Mock the MQTT paho client.""" if config is None: config = {mqtt.CONF_BROKER: 'mock-broker'} @@ -39,10 +38,11 @@ def async_mock_mqtt_client(hass, config=None): mock_client().subscribe.return_value = (0, 0) mock_client().unsubscribe.return_value = (0, 0) mock_client().publish.return_value = (0, 0) - result = yield from async_setup_component(hass, mqtt.DOMAIN, { + result = await async_setup_component(hass, mqtt.DOMAIN, { mqtt.DOMAIN: config }) assert result + await hass.async_block_till_done() return mock_client() diff --git a/tests/components/mqtt/test_server.py b/tests/components/mqtt/test_server.py index 71ef1dc1e43..ba05459185d 100644 --- a/tests/components/mqtt/test_server.py +++ b/tests/components/mqtt/test_server.py @@ -36,9 +36,8 @@ class TestMQTT: assert setup_component(self.hass, mqtt.DOMAIN, { mqtt.DOMAIN: {CONF_PASSWORD: password}, }) + self.hass.block_till_done() assert mock_mqtt.called - from pprint import pprint - pprint(mock_mqtt.mock_calls) assert mock_mqtt.mock_calls[1][2]['username'] == 'homeassistant' assert mock_mqtt.mock_calls[1][2]['password'] == password @@ -61,9 +60,8 @@ class TestMQTT: 'http': {'api_password': 'http_secret'}, mqtt.DOMAIN: {CONF_PASSWORD: password}, }) + self.hass.block_till_done() assert mock_mqtt.called - from pprint import pprint - pprint(mock_mqtt.mock_calls) assert mock_mqtt.mock_calls[1][2]['username'] == 'homeassistant' assert mock_mqtt.mock_calls[1][2]['password'] == password diff --git a/tests/components/tradfri/test_init.py b/tests/components/tradfri/test_init.py index 800c7b72ee6..4c2ad9d57c9 100644 --- a/tests/components/tradfri/test_init.py +++ b/tests/components/tradfri/test_init.py @@ -21,6 +21,7 @@ async def test_config_yaml_host_not_imported(hass): 'host': 'mock-host' } }) + await hass.async_block_till_done() assert len(mock_init.mock_calls) == 0 @@ -34,6 +35,7 @@ async def test_config_yaml_host_imported(hass): 'host': 'mock-host' } }) + await hass.async_block_till_done() progress = hass.config_entries.flow.async_progress() assert len(progress) == 1 @@ -54,6 +56,7 @@ async def test_config_json_host_not_imported(hass): assert await async_setup_component(hass, 'tradfri', { 'tradfri': {} }) + await hass.async_block_till_done() assert len(mock_init.mock_calls) == 0 @@ -65,6 +68,7 @@ async def test_config_json_host_imported(hass, mock_gateway_info): assert await async_setup_component(hass, 'tradfri', { 'tradfri': {} }) + await hass.async_block_till_done() progress = hass.config_entries.flow.async_progress() assert len(progress) == 1