diff --git a/homeassistant/helpers/deprecation.py b/homeassistant/helpers/deprecation.py index fd3fb50efd4392..6c78055e0b1b72 100644 --- a/homeassistant/helpers/deprecation.py +++ b/homeassistant/helpers/deprecation.py @@ -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 diff --git a/tests/helpers/test_deprecation.py b/tests/helpers/test_deprecation.py index cb90d8e2bed405..ef3be2d2ef8337 100644 --- a/tests/helpers/test_deprecation.py +++ b/tests/helpers/test_deprecation.py @@ -17,6 +17,7 @@ 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: