diff --git a/homeassistant/setup.py b/homeassistant/setup.py index 07de3b2942d..314938feeed 100644 --- a/homeassistant/setup.py +++ b/homeassistant/setup.py @@ -132,6 +132,17 @@ async def _async_setup_component( log_error(str(err)) return False + # Some integrations fail on import because they call functions incorrectly. + # So we do it before validating config to catch these errors. + try: + component = integration.get_component() + except ImportError: + log_error("Unable to import component", False) + return False + except Exception: # pylint: disable=broad-except + _LOGGER.exception("Setup failed for %s: unknown error", domain) + return False + processed_config = await conf_util.async_process_component_config( hass, config, integration ) @@ -143,12 +154,6 @@ async def _async_setup_component( start = timer() _LOGGER.info("Setting up %s", domain) - try: - component = integration.get_component() - except ImportError: - log_error("Unable to import component", False) - return False - if hasattr(component, "PLATFORM_SCHEMA"): # Entity components have their own warning warn_task = None diff --git a/tests/test_setup.py b/tests/test_setup.py index 9612c1784ef..8fd25091eb6 100644 --- a/tests/test_setup.py +++ b/tests/test_setup.py @@ -527,3 +527,11 @@ async def test_when_setup_already_loaded(hass): setup.async_when_setup(hass, "test", mock_callback) await hass.async_block_till_done() assert calls == ["test", "test"] + + +async def test_setup_import_blows_up(hass): + """Test that we handle it correctly when importing integration blows up.""" + with mock.patch( + "homeassistant.loader.Integration.get_component", side_effect=ValueError + ): + assert not await setup.async_setup_component(hass, "sun", {})