1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

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
2 changed files with 58 additions and 7 deletions

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: