Skip to content

Commit

Permalink
Skip logging deprecated constant if the calling integration couldn't …
Browse files Browse the repository at this point in the history
…be indentified (home-assistant#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
  • Loading branch information
edenhaus authored Dec 21, 2023
1 parent 9fbc15c commit c4c422d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
19 changes: 12 additions & 7 deletions homeassistant/helpers/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def _print_deprecation_warning(
description,
verb,
breaks_in_ha_version,
log_when_no_integration_is_found=True,
)


Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
46 changes: 46 additions & 0 deletions tests/helpers/test_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
dir_with_deprecated_constants,
get_deprecated,
)
from homeassistant.helpers.frame import MissingIntegrationFrame

from tests.common import MockModule, mock_integration

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit c4c422d

Please sign in to comment.