diff --git a/homeassistant/core.py b/homeassistant/core.py index 7f0883ca880..da49f30d58a 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -147,9 +147,42 @@ class ConfigSource(enum.StrEnum): # SOURCE_* are deprecated as of Home Assistant 2022.2, use ConfigSource instead -SOURCE_DISCOVERED = ConfigSource.DISCOVERED.value -SOURCE_STORAGE = ConfigSource.STORAGE.value -SOURCE_YAML = ConfigSource.YAML.value +_DEPRECATED_SOURCE_DISCOVERED = (ConfigSource.DISCOVERED, "2025.1") +_DEPRECATED_SOURCE_STORAGE = (ConfigSource.STORAGE, "2025.1") +_DEPRECATED_SOURCE_YAML = (ConfigSource.YAML, "2025.1") + + +# Can be removed if no deprecated constant are in this module anymore +def __getattr__(name: str) -> Any: + """Check if the not found name is a deprecated constant. + + If it is, print a deprecation warning and return the value of the constant. + Otherwise raise AttributeError. + """ + module_globals = globals() + if f"_DEPRECATED_{name}" not in module_globals: + raise AttributeError(f"Module {__name__} has no attribute {name!r}") + + # Avoid circular import + from .helpers.deprecation import ( # pylint: disable=import-outside-toplevel + check_if_deprecated_constant, + ) + + return check_if_deprecated_constant(name, module_globals) + + +# Can be removed if no deprecated constant are in this module anymore +def __dir__() -> list[str]: + """Return dir() with deprecated constants.""" + # Copied method from homeassistant.helpers.deprecattion#dir_with_deprecated_constants to avoid import cycle + module_globals = globals() + + return list(module_globals) + [ + name.removeprefix("_DEPRECATED_") + for name in module_globals + if name.startswith("_DEPRECATED_") + ] + # How long to wait until things that run on startup have to finish. TIMEOUT_EVENT_START = 15 diff --git a/tests/test_core.py b/tests/test_core.py index ce1767f2755..5f5be1b05db 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -58,7 +58,11 @@ import homeassistant.util.dt as dt_util from homeassistant.util.read_only_dict import ReadOnlyDict from homeassistant.util.unit_system import METRIC_SYSTEM -from .common import async_capture_events, async_mock_service +from .common import ( + async_capture_events, + async_mock_service, + import_and_test_deprecated_constant_enum, +) PST = dt_util.get_time_zone("America/Los_Angeles") @@ -2621,3 +2625,19 @@ async def test_cancel_shutdown_job(hass: HomeAssistant) -> None: cancel() await hass.async_stop() assert not evt.is_set() + + +@pytest.mark.parametrize( + ("enum"), + [ + ha.ConfigSource.DISCOVERED, + ha.ConfigSource.YAML, + ha.ConfigSource.STORAGE, + ], +) +def test_deprecated_constants( + caplog: pytest.LogCaptureFixture, + enum: ha.ConfigSource, +) -> None: + """Test deprecated constants.""" + import_and_test_deprecated_constant_enum(caplog, ha, enum, "SOURCE_", "2025.1")