From 5925b6b9125b9126bac4f7231b5f795e7ef2896c Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Fri, 6 Oct 2023 13:18:44 +0200 Subject: [PATCH] Only import color extractor when domain is in config (#101522) --- .../components/color_extractor/__init__.py | 14 +++++++++----- tests/components/color_extractor/test_init.py | 2 +- tests/components/color_extractor/test_service.py | 10 ++-------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/color_extractor/__init__.py b/homeassistant/components/color_extractor/__init__.py index af460f819cd..2cc3e206958 100644 --- a/homeassistant/components/color_extractor/__init__.py +++ b/homeassistant/components/color_extractor/__init__.py @@ -24,7 +24,10 @@ from .const import ATTR_PATH, ATTR_URL, DOMAIN, SERVICE_TURN_ON _LOGGER = logging.getLogger(__name__) -CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) +CONFIG_SCHEMA = vol.Schema( + {vol.Optional(DOMAIN): {}}, + extra=vol.ALLOW_EXTRA, +) # Extend the existing light.turn_on service schema SERVICE_SCHEMA = vol.All( @@ -62,11 +65,12 @@ def _get_color(file_handler) -> tuple: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the Color extractor component.""" - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_IMPORT}, data={} + if DOMAIN in config: + hass.async_create_task( + hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_IMPORT}, data={} + ) ) - ) return True diff --git a/tests/components/color_extractor/test_init.py b/tests/components/color_extractor/test_init.py index 797eaf291fe..b4874b575e8 100644 --- a/tests/components/color_extractor/test_init.py +++ b/tests/components/color_extractor/test_init.py @@ -8,7 +8,7 @@ from homeassistant.setup import async_setup_component async def test_legacy_migration(hass: HomeAssistant) -> None: """Test migration from yaml to config flow.""" - assert await async_setup_component(hass, DOMAIN, {}) + assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await hass.async_block_till_done() entries = hass.config_entries.async_entries(DOMAIN) assert len(entries) == 1 diff --git a/tests/components/color_extractor/test_service.py b/tests/components/color_extractor/test_service.py index 361127c332b..647d945f158 100644 --- a/tests/components/color_extractor/test_service.py +++ b/tests/components/color_extractor/test_service.py @@ -254,7 +254,7 @@ def _get_file_mock(file_path): @patch("os.path.isfile", Mock(return_value=True)) @patch("os.access", Mock(return_value=True)) -async def test_file(hass: HomeAssistant) -> None: +async def test_file(hass: HomeAssistant, setup_integration) -> None: """Test that the file only service reads a file and translates to light RGB.""" service_data = { ATTR_PATH: "/opt/image.png", @@ -266,9 +266,6 @@ async def test_file(hass: HomeAssistant) -> None: # Add our /opt/ path to the allowed list of paths hass.config.allowlist_external_dirs.add("/opt/") - await async_setup_component(hass, DOMAIN, {}) - await hass.async_block_till_done() - # Verify pre service check state = hass.states.get(LIGHT_ENTITY) assert state @@ -295,7 +292,7 @@ async def test_file(hass: HomeAssistant) -> None: @patch("os.path.isfile", Mock(return_value=True)) @patch("os.access", Mock(return_value=True)) -async def test_file_denied_dir(hass: HomeAssistant) -> None: +async def test_file_denied_dir(hass: HomeAssistant, setup_integration) -> None: """Test that the file only service fails to read an image in a dir not explicitly allowed.""" service_data = { ATTR_PATH: "/path/to/a/dir/not/allowed/image.png", @@ -304,9 +301,6 @@ async def test_file_denied_dir(hass: HomeAssistant) -> None: ATTR_BRIGHTNESS_PCT: 100, } - await async_setup_component(hass, DOMAIN, {}) - await hass.async_block_till_done() - # Verify pre service check state = hass.states.get(LIGHT_ENTITY) assert state