diff --git a/packages/pytest-home-assistant/pytest_homeassistant/plugins.py b/packages/pytest-home-assistant/pytest_homeassistant/plugins.py index 01e3bcc02587..c0b42e0966a8 100644 --- a/packages/pytest-home-assistant/pytest_homeassistant/plugins.py +++ b/packages/pytest-home-assistant/pytest_homeassistant/plugins.py @@ -709,6 +709,12 @@ def hass_config_dir() -> str: return get_test_config_dir() +@pytest.fixture +def custom_integrations_enabled() -> bool: + """Control custom integration loading.""" + return True + + @pytest.fixture async def hass( hass_fixture_setup: list[bool], @@ -717,6 +723,7 @@ async def hass( hass_storage: dict[str, Any], request: pytest.FixtureRequest, mock_recorder_before_hass: None, + custom_integrations_enabled: bool, ) -> AsyncGenerator[HomeAssistant]: """Create a test instance of Home Assistant.""" @@ -743,6 +750,8 @@ async def hass( async with async_test_home_assistant( loop, load_registries, config_dir=hass_config_dir ) as hass: + if custom_integrations_enabled: + hass.data.pop(loader.DATA_CUSTOM_COMPONENTS, None) orig_exception_handler = loop.get_exception_handler() loop.set_exception_handler(exc_handle) frame.async_setup(hass) @@ -1570,7 +1579,7 @@ def mock_async_zeroconf(mock_zeroconf: MagicMock) -> Generator[MagicMock]: @pytest.fixture def enable_custom_integrations(hass: HomeAssistant) -> None: """Enable custom integrations defined in the test dir.""" - hass.data.pop(loader.DATA_CUSTOM_COMPONENTS) + hass.data.pop(loader.DATA_CUSTOM_COMPONENTS, None) @pytest.fixture diff --git a/tests/conftest.py b/tests/conftest.py index fe4690bb8406..63a6dc4d527b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -250,3 +250,9 @@ from pytest_homeassistant.plugins import ( verify_cleanup as verify_cleanup, wait_for_stop_scripts_after_shutdown as wait_for_stop_scripts_after_shutdown, ) + + +@pytest.fixture +def custom_integrations_enabled() -> bool: + """Control custom integration loading.""" + return False diff --git a/tests/test_test_fixtures.py b/tests/test_test_fixtures.py index 1f66a872ee53..01572203c173 100644 --- a/tests/test_test_fixtures.py +++ b/tests/test_test_fixtures.py @@ -12,6 +12,7 @@ from pytest_homeassistant import _paths from pytest_homeassistant.common import get_fixture_path import pytest_socket +from homeassistant import loader from homeassistant.core import HomeAssistant, async_get_hass from homeassistant.helpers import translation from homeassistant.helpers.http import HomeAssistantView @@ -86,6 +87,20 @@ async def test_hass_cv(hass: HomeAssistant) -> None: assert async_get_hass() is hass +async def test_custom_integrations_disabled(hass: HomeAssistant) -> None: + """Test the default custom integration policy.""" + with pytest.raises(loader.IntegrationNotFound): + await loader.async_get_integration(hass, "test_package") + + +@pytest.mark.parametrize("custom_integrations_enabled", [True]) +async def test_custom_integrations_enabled(hass: HomeAssistant) -> None: + """Load a custom integration using the enabling policy.""" + integration = await loader.async_get_integration(hass, "test_package") + assert not integration.is_built_in + assert await async_setup_component(hass, "test_package", {}) + + def register_view(hass: HomeAssistant) -> None: """Register a view."""