From eff7a12557f53b95f23b3cd88eadc10146d0b6ce Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 3 Mar 2022 15:03:03 -0800 Subject: [PATCH] Highlight in logs it is a custom component when setup fails (#67559) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Joakim Sørensen --- homeassistant/setup.py | 18 +++++++++++++----- tests/test_setup.py | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/homeassistant/setup.py b/homeassistant/setup.py index 36292989dce..2599b4b3c85 100644 --- a/homeassistant/setup.py +++ b/homeassistant/setup.py @@ -149,10 +149,17 @@ async def _async_setup_component( This method is a coroutine. """ + integration: loader.Integration | None = None - def log_error(msg: str, link: str | None = None) -> None: + def log_error(msg: str) -> None: """Log helper.""" - _LOGGER.error("Setup failed for %s: %s", domain, msg) + if integration is None: + custom = "" + link = None + else: + custom = "" if integration.is_built_in else "custom integration " + link = integration.documentation + _LOGGER.error("Setup failed for %s%s: %s", custom, domain, msg) async_notify_setup_error(hass, domain, link) try: @@ -174,7 +181,7 @@ async def _async_setup_component( try: await async_process_deps_reqs(hass, config, integration) except HomeAssistantError as err: - log_error(str(err), integration.documentation) + log_error(str(err)) return False # Some integrations fail on import because they call functions incorrectly. @@ -182,7 +189,7 @@ async def _async_setup_component( try: component = integration.get_component() except ImportError as err: - log_error(f"Unable to import component: {err}", integration.documentation) + log_error(f"Unable to import component: {err}") return False processed_config = await conf_util.async_process_component_config( @@ -190,7 +197,7 @@ async def _async_setup_component( ) if processed_config is None: - log_error("Invalid config.", integration.documentation) + log_error("Invalid config.") return False start = timer() @@ -287,6 +294,7 @@ async def async_prepare_setup_platform( def log_error(msg: str) -> None: """Log helper.""" + _LOGGER.error("Unable to prepare setup for platform %s: %s", platform_path, msg) async_notify_setup_error(hass, platform_path) diff --git a/tests/test_setup.py b/tests/test_setup.py index f71ba01410b..04924344c2b 100644 --- a/tests/test_setup.py +++ b/tests/test_setup.py @@ -11,6 +11,7 @@ import voluptuous as vol from homeassistant import config_entries, setup from homeassistant.const import EVENT_COMPONENT_LOADED, EVENT_HOMEASSISTANT_START from homeassistant.core import callback +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import discovery from homeassistant.helpers.config_validation import ( PLATFORM_SCHEMA, @@ -621,6 +622,22 @@ async def test_integration_disabled(hass, caplog): assert disabled_reason in caplog.text +async def test_integration_logs_is_custom(hass, caplog): + """Test we highlight it's a custom component when errors happen.""" + mock_integration( + hass, + MockModule("test_component1"), + built_in=False, + ) + with patch( + "homeassistant.setup.async_process_deps_reqs", + side_effect=HomeAssistantError("Boom"), + ): + result = await setup.async_setup_component(hass, "test_component1", {}) + assert not result + assert "Setup failed for custom integration test_component1: Boom" in caplog.text + + async def test_async_get_loaded_integrations(hass): """Test we can enumerate loaded integations.""" hass.config.components.add("notbase")