diff --git a/tests/entity_stublib.py b/tests/entity_stublib.py index 7c795ce..2cee30d 100644 --- a/tests/entity_stublib.py +++ b/tests/entity_stublib.py @@ -113,12 +113,6 @@ def install() -> None: """Register the stub modules (idempotent).""" if "homeassistant.components.media_player" in sys.modules: return - # honor per-file flow stub versions already in place (e.g. the - # config-flow/pairing/protocol files) — the hub's base would strip the - # base plumbing the flow tests already installed (t30 leak). - ce = sys.modules.get("homeassistant.config_entries") - if ce is not None and hasattr(ce, "OptionsFlowWithReload"): - return vol = types.ModuleType("voluptuous") vol.Required = lambda key, default=None: key diff --git a/tests/test_init.py b/tests/test_init.py index 50c83b3..c0386ea 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -13,17 +13,45 @@ from types import SimpleNamespace import entity_stublib import pytest +# Scoped-stub hygiene (mirrors tests/test_config_flow_pairing.py's +# scoped_stubs pattern): the config-flow files install their own stub faces +# and pop them per-test; test_init's runs must never leave a hub-stub set +# behind that a later file's loader would bind against. +_INIT_STUB_NAMES = ( + "voluptuous", + "homeassistant", + "homeassistant.config_entries", + "homeassistant.core", + "homeassistant.helpers", + "homeassistant.helpers.entity_registry", + "homeassistant.helpers.device_registry", + "homeassistant.helpers.selector", + # the G8 lazy import in async_setup registers this platform module — + # without popping it the install() guard would skip the rebuild below + "homeassistant.components.media_player", + "nsh.helpers", + "nsh.config_flow", + "nsh.init", + "nsh.media_player", +) + @pytest.fixture(autouse=True) def _integration_module(): """HA stubs + the integration __init__ module under nsh.init.""" + snapshot = {name: sys.modules.get(name) for name in _INIT_STUB_NAMES} entity_stublib.install() if "nsh.init" not in sys.modules: import conftest conftest._load_module("nsh.init", conftest._SHIELD_DIR / "__init__.py") yield sys.modules["nsh.init"] - sys.modules.pop("nsh.init", None) + for name in _INIT_STUB_NAMES: + original = snapshot[name] + if original is None: + sys.modules.pop(name, None) + else: + sys.modules[name] = original class _SeedEntriesStub: