Deprecate deprecated core constants (#106456)

This commit is contained in:
Robert Resch 2023-12-27 08:42:57 +01:00 committed by GitHub
parent 2afe3364ca
commit b08268da31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 4 deletions

View file

@ -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

View file

@ -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")