Skip logging deprecated constant if the calling integration couldn't be indentified (#106181)

* Add option to log only if a integreation is detected for a deprecated constant

* Require param

* Add test that log entry is not created

* typo
This commit is contained in:
Robert Resch 2023-12-21 23:19:40 +01:00 committed by GitHub
parent 9fbc15c28b
commit c4c422de79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 7 deletions

View File

@ -161,6 +161,7 @@ def _print_deprecation_warning(
description,
verb,
breaks_in_ha_version,
log_when_no_integration_is_found=True,
)
@ -171,6 +172,8 @@ def _print_deprecation_warning_internal(
description: str,
verb: str,
breaks_in_ha_version: str | None,
*,
log_when_no_integration_is_found: bool,
) -> None:
logger = logging.getLogger(module_name)
if breaks_in_ha_version:
@ -180,13 +183,14 @@ def _print_deprecation_warning_internal(
try:
integration_frame = get_integration_frame()
except MissingIntegrationFrame:
logger.warning(
"%s is a deprecated %s%s. Use %s instead",
obj_name,
description,
breaks_in,
replacement,
)
if log_when_no_integration_is_found:
logger.warning(
"%s is a deprecated %s%s. Use %s instead",
obj_name,
description,
breaks_in,
replacement,
)
else:
if integration_frame.custom_integration:
hass: HomeAssistant | None = None
@ -280,6 +284,7 @@ def check_if_deprecated_constant(name: str, module_globals: dict[str, Any]) -> A
"constant",
"used",
breaks_in_ha_version,
log_when_no_integration_is_found=False,
)
return value

View File

@ -17,6 +17,7 @@ from homeassistant.helpers.deprecation import (
dir_with_deprecated_constants,
get_deprecated,
)
from homeassistant.helpers.frame import MissingIntegrationFrame
from tests.common import MockModule, mock_integration
@ -324,6 +325,51 @@ def test_check_if_deprecated_constant(
) in caplog.record_tuples
@pytest.mark.parametrize(
("deprecated_constant", "extra_msg"),
[
(
DeprecatedConstant("value", "NEW_CONSTANT", None),
". Use NEW_CONSTANT instead",
),
(
DeprecatedConstant(1, "NEW_CONSTANT", "2099.1"),
" which will be removed in HA Core 2099.1. Use NEW_CONSTANT instead",
),
],
)
@pytest.mark.parametrize(
("module_name"),
[
"homeassistant.components.hue.light", # builtin integration
"config.custom_components.hue.light", # custom component integration
],
)
def test_check_if_deprecated_constant_integration_not_found(
caplog: pytest.LogCaptureFixture,
deprecated_constant: DeprecatedConstant | DeprecatedConstantEnum,
extra_msg: str,
module_name: str,
) -> None:
"""Test check_if_deprecated_constant."""
module_globals = {
"__name__": module_name,
"_DEPRECATED_TEST_CONSTANT": deprecated_constant,
}
with patch(
"homeassistant.helpers.frame.extract_stack", side_effect=MissingIntegrationFrame
):
value = check_if_deprecated_constant("TEST_CONSTANT", module_globals)
assert value == deprecated_constant.value
assert (
module_name,
logging.WARNING,
f"TEST_CONSTANT is a deprecated constant{extra_msg}",
) not in caplog.record_tuples
def test_test_check_if_deprecated_constant_invalid(
caplog: pytest.LogCaptureFixture,
) -> None: