Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion homeassistant/components/recovery_mode/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"name": "Recovery Mode",
"codeowners": ["@home-assistant/core"],
"config_flow": false,
"dependencies": ["persistent_notification"],
"documentation": "https://www.home-assistant.io/integrations/recovery_mode",
"integration_type": "system",
"quality_scale": "internal"
Expand Down
14 changes: 12 additions & 2 deletions script/hassfest/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from . import ast_parse_module
from .model import Config, Integration

# Duplicated from homeassistant.bootstrap to avoid importing bootstrap (and its
# eager component pre-imports) into hassfest. Kept in sync via test_dependencies.
CORE_INTEGRATIONS = {"homeassistant", "persistent_notification"}


class ImportCollector(ast.NodeVisitor):
"""Collect all integrations referenced."""
Expand Down Expand Up @@ -86,6 +90,7 @@ def visit_Import(self, node: ast.Import) -> None:


ALLOWED_USED_COMPONENTS = {
*CORE_INTEGRATIONS,
*{platform.value for platform in Platform},
# Internal integrations
"alert",
Expand All @@ -95,7 +100,6 @@ def visit_Import(self, node: ast.Import) -> None:
"device_automation",
"frontend",
"group",
"homeassistant",
"input_boolean",
"input_button",
"input_datetime",
Expand All @@ -106,7 +110,6 @@ def visit_Import(self, node: ast.Import) -> None:
"media_source",
"onboarding",
"panel_custom",
"persistent_notification",
"person",
"script",
"shopping_list",
Expand Down Expand Up @@ -332,6 +335,13 @@ def _validate_dependencies(
"dependencies", f"Dependency {dep} does not exist"
)

if dep in CORE_INTEGRATIONS:
integration.add_error(
"dependencies",
f"Dependency {dep} is a core integration and is "
"unconditionally loaded",
)


def validate(
integrations: dict[str, Integration],
Expand Down
54 changes: 53 additions & 1 deletion tests/hassfest/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

import pytest

from script.hassfest.dependencies import ImportCollector
from script.hassfest.dependencies import (
CORE_INTEGRATIONS,
ImportCollector,
_validate_dependencies,
)
from script.hassfest.model import Config

from . import get_integration


@pytest.fixture
Expand Down Expand Up @@ -90,3 +97,48 @@ def test_all_imports(mock_collector) -> None:
"child_import_field",
"renamed_absolute",
}


def test_dependency_on_core_integration_rejected(config: Config) -> None:
"""Test that depending on a core integration is rejected."""
consumer = get_integration("consumer", config)
consumer.manifest["dependencies"] = ["persistent_notification"]

integrations = {
"consumer": consumer,
"persistent_notification": get_integration("persistent_notification", config),
}

_validate_dependencies(integrations)

assert len(consumer.errors) == 1
assert (
"Dependency persistent_notification is a core integration"
in consumer.errors[0].error
)


def test_dependency_on_non_core_integration_allowed(config: Config) -> None:
"""Test that depending on a non-core integration is not rejected."""
consumer = get_integration("consumer", config)
consumer.manifest["dependencies"] = ["other"]

integrations = {
"consumer": consumer,
"other": get_integration("other", config),
}

_validate_dependencies(integrations)

assert consumer.errors == []


def test_core_integrations_in_sync_with_bootstrap() -> None:
"""Test the duplicated CORE_INTEGRATIONS stays aligned with bootstrap."""
# Imported here so the rest of the hassfest tests are not slowed down
# by bootstrap's eager component pre-imports.
from homeassistant.bootstrap import ( # noqa: PLC0415
CORE_INTEGRATIONS as bootstrap_core_integrations,
)

assert bootstrap_core_integrations == CORE_INTEGRATIONS