2017-02-11 04:46:15 +00:00
|
|
|
"""Deprecation helpers for Home Assistant."""
|
2021-01-26 14:53:21 +00:00
|
|
|
import functools
|
2017-02-11 04:46:15 +00:00
|
|
|
import inspect
|
|
|
|
import logging
|
2018-11-11 16:39:50 +00:00
|
|
|
from typing import Any, Callable, Dict, Optional
|
2017-02-11 04:46:15 +00:00
|
|
|
|
|
|
|
|
2018-11-11 16:39:50 +00:00
|
|
|
def deprecated_substitute(substitute_name: str) -> Callable[..., Callable]:
|
2017-02-11 04:46:15 +00:00
|
|
|
"""Help migrate properties to new names.
|
|
|
|
|
|
|
|
When a property is added to replace an older property, this decorator can
|
|
|
|
be added to the new property, listing the old property as the substitute.
|
2018-02-02 21:35:34 +00:00
|
|
|
If the old property is defined, its value will be used instead, and a log
|
2017-02-11 04:46:15 +00:00
|
|
|
warning will be issued alerting the user of the impending change.
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-11-11 16:39:50 +00:00
|
|
|
def decorator(func: Callable) -> Callable:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Decorate function as deprecated."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-11-11 16:39:50 +00:00
|
|
|
def func_wrapper(self: Callable) -> Any:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Wrap for the original function."""
|
2017-02-11 04:46:15 +00:00
|
|
|
if hasattr(self, substitute_name):
|
|
|
|
# If this platform is still using the old property, issue
|
|
|
|
# a logger warning once with instructions on how to fix it.
|
2019-07-31 19:25:30 +00:00
|
|
|
warnings = getattr(func, "_deprecated_substitute_warnings", {})
|
2017-02-11 04:46:15 +00:00
|
|
|
module_name = self.__module__
|
|
|
|
if not warnings.get(module_name):
|
|
|
|
logger = logging.getLogger(module_name)
|
|
|
|
logger.warning(
|
2017-05-20 19:18:59 +00:00
|
|
|
"'%s' is deprecated. Please rename '%s' to "
|
|
|
|
"'%s' in '%s' to ensure future support.",
|
2019-07-31 19:25:30 +00:00
|
|
|
substitute_name,
|
|
|
|
substitute_name,
|
|
|
|
func.__name__,
|
|
|
|
inspect.getfile(self.__class__),
|
|
|
|
)
|
2017-02-11 04:46:15 +00:00
|
|
|
warnings[module_name] = True
|
2019-07-31 19:25:30 +00:00
|
|
|
setattr(func, "_deprecated_substitute_warnings", warnings)
|
2017-02-11 04:46:15 +00:00
|
|
|
|
|
|
|
# Return the old property
|
|
|
|
return getattr(self, substitute_name)
|
2018-07-23 08:16:05 +00:00
|
|
|
return func(self)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-02-11 04:46:15 +00:00
|
|
|
return func_wrapper
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-02-11 04:46:15 +00:00
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def get_deprecated(
|
|
|
|
config: Dict[str, Any], new_name: str, old_name: str, default: Optional[Any] = None
|
|
|
|
) -> Optional[Any]:
|
2017-02-11 04:46:15 +00:00
|
|
|
"""Allow an old config name to be deprecated with a replacement.
|
|
|
|
|
|
|
|
If the new config isn't found, but the old one is, the old value is used
|
|
|
|
and a warning is issued to the user.
|
|
|
|
"""
|
|
|
|
if old_name in config:
|
2019-10-05 09:59:34 +00:00
|
|
|
module = inspect.getmodule(inspect.stack()[1][0])
|
|
|
|
if module is not None:
|
|
|
|
module_name = module.__name__
|
|
|
|
else:
|
|
|
|
# If Python is unable to access the sources files, the call stack frame
|
|
|
|
# will be missing information, so let's guard.
|
2020-10-02 22:04:11 +00:00
|
|
|
# https://github.com/home-assistant/core/issues/24982
|
2019-10-05 09:59:34 +00:00
|
|
|
module_name = __name__
|
|
|
|
|
2017-02-11 04:46:15 +00:00
|
|
|
logger = logging.getLogger(module_name)
|
|
|
|
logger.warning(
|
2017-05-20 19:18:59 +00:00
|
|
|
"'%s' is deprecated. Please rename '%s' to '%s' in your "
|
2019-07-31 19:25:30 +00:00
|
|
|
"configuration file.",
|
|
|
|
old_name,
|
|
|
|
old_name,
|
|
|
|
new_name,
|
|
|
|
)
|
2017-02-11 04:46:15 +00:00
|
|
|
return config.get(old_name)
|
|
|
|
return config.get(new_name, default)
|
2021-01-26 14:53:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def deprecated_function(replacement: str) -> Callable[..., Callable]:
|
|
|
|
"""Mark function as deprecated and provide a replacement function to be used instead."""
|
|
|
|
|
|
|
|
def deprecated_decorator(func: Callable) -> Callable:
|
|
|
|
"""Decorate function as deprecated."""
|
|
|
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
def deprecated_func(*args: tuple, **kwargs: Dict[str, Any]) -> Any:
|
|
|
|
"""Wrap for the original function."""
|
|
|
|
logger = logging.getLogger(func.__module__)
|
|
|
|
logger.warning(
|
|
|
|
"%s is a deprecated function. Use %s instead",
|
|
|
|
func.__name__,
|
|
|
|
replacement,
|
|
|
|
)
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
|
|
|
return deprecated_func
|
|
|
|
|
|
|
|
return deprecated_decorator
|