diff --git a/homeassistant/helpers/deprecation.py b/homeassistant/helpers/deprecation.py index 4f5094a3744b..00882edda3c2 100644 --- a/homeassistant/helpers/deprecation.py +++ b/homeassistant/helpers/deprecation.py @@ -362,13 +362,14 @@ class DeferredDeprecatedAlias[T]: _PREFIX_DEPRECATED = "_DEPRECATED_" +_REPORTED_DEPRECATED_CONSTANTS: set[tuple[str | None, str, str, int]] = set() def check_if_deprecated_constant(name: str, module_globals: dict[str, Any]) -> Any: """Check if the not found name is a deprecated constant. - If it is, print a deprecation warning and return the value of the constant. - Otherwise raise AttributeError. + If it is, print a deprecation warning once per caller and return the value + of the constant. Otherwise raise AttributeError. """ module_name = module_globals.get("__name__") value = replacement = None @@ -406,6 +407,12 @@ def check_if_deprecated_constant(name: str, module_globals: dict[str, Any]) -> A # https://peps.python.org/pep-0562/#specification raise AttributeError(msg) + caller = sys._getframe(1) # noqa: SLF001 + key = (module_name, name, caller.f_code.co_filename, caller.f_lineno) + if key in _REPORTED_DEPRECATED_CONSTANTS: + return value + _REPORTED_DEPRECATED_CONSTANTS.add(key) + _print_deprecation_warning_internal( name, module_name or __name__, diff --git a/packages/pytest-homeassistant/pytest_homeassistant/plugins.py b/packages/pytest-homeassistant/pytest_homeassistant/plugins.py index 843cb212a9d6..ca63d6f5d7f3 100644 --- a/packages/pytest-homeassistant/pytest_homeassistant/plugins.py +++ b/packages/pytest-homeassistant/pytest_homeassistant/plugins.py @@ -538,6 +538,7 @@ def reset_globals() -> Generator[None]: # Reset the frame helper globals frame.async_setup(None) frame._REPORTED_INTEGRATIONS.clear() + deprecation._REPORTED_DEPRECATED_CONSTANTS.clear() deprecation._REPORTED_DEPRECATED_ENTITY_USAGE.clear() # Reset patch_json diff --git a/tests/helpers/test_deprecation.py b/tests/helpers/test_deprecation.py index 60f645396f52..9f9f2f3d4432 100644 --- a/tests/helpers/test_deprecation.py +++ b/tests/helpers/test_deprecation.py @@ -566,6 +566,45 @@ def test_check_if_deprecated_constant_integration_not_found( ) not in caplog.record_tuples +def test_check_if_deprecated_constant_warns_once_per_caller( + caplog: pytest.LogCaptureFixture, +) -> None: + """Test check_if_deprecated_constant warns once per caller.""" + module_name = "config.custom_components.hue.light" + module_globals = { + "__name__": module_name, + "_DEPRECATED_TEST_CONSTANT": DeprecatedConstant("value", "NEW_CONSTANT", None), + } + filename = "/home/paulus/config/custom_components/hue/light.py" + + with ( + patch.dict(sys.modules, {module_name: Mock(__file__=filename)}), + patch( + "homeassistant.helpers.frame.get_current_frame", + return_value=extract_stack_to_frame( + [Mock(filename=filename, lineno="23", line="self.name")] + ), + ), + ): + for _ in range(2): + value = check_if_deprecated_constant("TEST_CONSTANT", module_globals) + assert value == "value" + assert len(caplog.record_tuples) == 1 + + # A different caller is warned about separately + check_if_deprecated_constant("TEST_CONSTANT", module_globals) + assert len(caplog.record_tuples) == 2 + + assert set(caplog.record_tuples) == { + ( + module_name, + logging.WARNING, + "The deprecated constant TEST_CONSTANT was used from hue. Use NEW_CONSTANT" + " instead, please report it to the author of the 'hue' custom integration", + ) + } + + def test_test_check_if_deprecated_constant_invalid( caplog: pytest.LogCaptureFixture, ) -> None: