From eb98ac9415bd4b4c57270c0872f1581b64886054 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 17 Sep 2021 19:25:50 -1000 Subject: [PATCH] Allow IntegrationNotFound when checking config in safe mode (#56283) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Joakim Sørensen --- homeassistant/helpers/check_config.py | 13 +++++-- tests/helpers/test_check_config.py | 54 ++++++++++++++++++++++++++- tests/scripts/test_check_config.py | 2 +- 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/homeassistant/helpers/check_config.py b/homeassistant/helpers/check_config.py index 26e063ae1f2..00f952013b5 100644 --- a/homeassistant/helpers/check_config.py +++ b/homeassistant/helpers/check_config.py @@ -125,8 +125,12 @@ async def async_check_ha_config_file( # noqa: C901 for domain in components: try: integration = await async_get_integration_with_requirements(hass, domain) - except (RequirementsNotFound, loader.IntegrationNotFound) as ex: - result.add_error(f"Component error: {domain} - {ex}") + except loader.IntegrationNotFound as ex: + if not hass.config.safe_mode: + result.add_error(f"Integration error: {domain} - {ex}") + continue + except RequirementsNotFound as ex: + result.add_error(f"Integration error: {domain} - {ex}") continue try: @@ -210,8 +214,11 @@ async def async_check_ha_config_file( # noqa: C901 hass, p_name ) platform = p_integration.get_platform(domain) + except loader.IntegrationNotFound as ex: + if not hass.config.safe_mode: + result.add_error(f"Platform error {domain}.{p_name} - {ex}") + continue except ( - loader.IntegrationNotFound, RequirementsNotFound, ImportError, ) as ex: diff --git a/tests/helpers/test_check_config.py b/tests/helpers/test_check_config.py index c5b75b84342..e79250a084d 100644 --- a/tests/helpers/test_check_config.py +++ b/tests/helpers/test_check_config.py @@ -7,6 +7,7 @@ from homeassistant.helpers.check_config import ( CheckConfigError, async_check_ha_config_file, ) +from homeassistant.requirements import RequirementsNotFound from tests.common import mock_platform, patch_yaml_files @@ -75,7 +76,7 @@ async def test_component_platform_not_found(hass): assert res.keys() == {"homeassistant"} assert res.errors[0] == CheckConfigError( - "Component error: beer - Integration 'beer' not found.", None, None + "Integration error: beer - Integration 'beer' not found.", None, None ) # Only 1 error expected @@ -83,6 +84,42 @@ async def test_component_platform_not_found(hass): assert not res.errors +async def test_component_requirement_not_found(hass): + """Test errors if component with a requirement not found not found.""" + # Make sure they don't exist + files = {YAML_CONFIG_FILE: BASE_CONFIG + "test_custom_component:"} + with patch( + "homeassistant.helpers.check_config.async_get_integration_with_requirements", + side_effect=RequirementsNotFound("test_custom_component", ["any"]), + ), patch("os.path.isfile", return_value=True), patch_yaml_files(files): + res = await async_check_ha_config_file(hass) + log_ha_config(res) + + assert res.keys() == {"homeassistant"} + assert res.errors[0] == CheckConfigError( + "Integration error: test_custom_component - Requirements for test_custom_component not found: ['any'].", + None, + None, + ) + + # Only 1 error expected + res.errors.pop(0) + assert not res.errors + + +async def test_component_not_found_safe_mode(hass): + """Test no errors if component not found in safe mode.""" + # Make sure they don't exist + files = {YAML_CONFIG_FILE: BASE_CONFIG + "beer:"} + hass.config.safe_mode = True + with patch("os.path.isfile", return_value=True), patch_yaml_files(files): + res = await async_check_ha_config_file(hass) + log_ha_config(res) + + assert res.keys() == {"homeassistant"} + assert not res.errors + + async def test_component_platform_not_found_2(hass): """Test errors if component or platform not found.""" # Make sure they don't exist @@ -103,6 +140,21 @@ async def test_component_platform_not_found_2(hass): assert not res.errors +async def test_platform_not_found_safe_mode(hass): + """Test no errors if platform not found in safe_mode.""" + # Make sure they don't exist + files = {YAML_CONFIG_FILE: BASE_CONFIG + "light:\n platform: beer"} + hass.config.safe_mode = True + with patch("os.path.isfile", return_value=True), patch_yaml_files(files): + res = await async_check_ha_config_file(hass) + log_ha_config(res) + + assert res.keys() == {"homeassistant", "light"} + assert res["light"] == [] + + assert not res.errors + + async def test_package_invalid(hass): """Test a valid platform setup.""" files = { diff --git a/tests/scripts/test_check_config.py b/tests/scripts/test_check_config.py index 1a96568f8ef..05ebb8fb0e5 100644 --- a/tests/scripts/test_check_config.py +++ b/tests/scripts/test_check_config.py @@ -74,7 +74,7 @@ def test_component_platform_not_found(mock_is_file, loop): assert res["components"].keys() == {"homeassistant"} assert res["except"] == { check_config.ERROR_STR: [ - "Component error: beer - Integration 'beer' not found." + "Integration error: beer - Integration 'beer' not found." ] } assert res["secret_cache"] == {}