Deprecate some deprecated const constants (#106230)

* Deprecate some deprecated const constants

* Improve code

* fix typing

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Robert Resch 2023-12-23 20:18:51 +01:00 committed by GitHub
parent 55a5e9c4b5
commit ebdf7b9c8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 393 additions and 88 deletions

View file

@ -252,6 +252,7 @@ def check_if_deprecated_constant(name: str, module_globals: dict[str, Any]) -> A
"""
module_name = module_globals.get("__name__")
logger = logging.getLogger(module_name)
value = replacement = None
if (deprecated_const := module_globals.get(_PREFIX_DEPRECATED + name)) is None:
raise AttributeError(f"Module {module_name!r} has no attribute {name!r}")
if isinstance(deprecated_const, DeprecatedConstant):
@ -264,9 +265,22 @@ def check_if_deprecated_constant(name: str, module_globals: dict[str, Any]) -> A
f"{deprecated_const.enum.__class__.__name__}.{deprecated_const.enum.name}"
)
breaks_in_ha_version = deprecated_const.breaks_in_ha_version
else:
elif isinstance(deprecated_const, tuple):
# Use DeprecatedConstant and DeprecatedConstant instead, where possible
# Used to avoid import cycles.
if len(deprecated_const) == 3:
value = deprecated_const[0]
replacement = deprecated_const[1]
breaks_in_ha_version = deprecated_const[2]
elif len(deprecated_const) == 2 and isinstance(deprecated_const[0], Enum):
enum = deprecated_const[0]
value = enum.value
replacement = f"{enum.__class__.__name__}.{enum.name}"
breaks_in_ha_version = deprecated_const[1]
if value is None or replacement is None:
msg = (
f"Value of {_PREFIX_DEPRECATED}{name!r} is an instance of {type(deprecated_const)} "
f"Value of {_PREFIX_DEPRECATED}{name} is an instance of {type(deprecated_const)} "
"but an instance of DeprecatedConstant or DeprecatedConstantEnum is required"
)