Skip to content

Commit

Permalink
Only import color extractor when domain is in config (#101522)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored and frenck committed Oct 6, 2023
1 parent 42b53c6 commit 5925b6b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
14 changes: 9 additions & 5 deletions homeassistant/components/color_extractor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@

_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(
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/components/color_extractor/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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
Expand Down
10 changes: 2 additions & 8 deletions tests/components/color_extractor/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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
Expand Down

0 comments on commit 5925b6b

Please sign in to comment.