diff --git a/homeassistant/helpers/deprecation.py b/homeassistant/helpers/deprecation.py index 307a297272c..c499dd0b6cd 100644 --- a/homeassistant/helpers/deprecation.py +++ b/homeassistant/helpers/deprecation.py @@ -2,12 +2,17 @@ from __future__ import annotations from collections.abc import Callable +from contextlib import suppress import functools import inspect import logging from typing import Any, ParamSpec, TypeVar -from ..helpers.frame import MissingIntegrationFrame, get_integration_frame +from homeassistant.core import HomeAssistant, async_get_hass +from homeassistant.exceptions import HomeAssistantError +from homeassistant.loader import async_suggest_report_issue + +from .frame import MissingIntegrationFrame, get_integration_frame _ObjectT = TypeVar("_ObjectT", bound=object) _R = TypeVar("_R") @@ -134,16 +139,24 @@ def _print_deprecation_warning(obj: Any, replacement: str, description: str) -> try: integration_frame = get_integration_frame() if integration_frame.custom_integration: + hass: HomeAssistant | None = None + with suppress(HomeAssistantError): + hass = async_get_hass() + report_issue = async_suggest_report_issue( + hass, + integration_domain=integration_frame.integration, + module=integration_frame.module, + ) logger.warning( ( "%s was called from %s, this is a deprecated %s. Use %s instead," - " please report this to the maintainer of %s" + " please %s" ), obj.__name__, integration_frame.integration, description, replacement, - integration_frame.integration, + report_issue, ) else: logger.warning( diff --git a/tests/helpers/test_deprecation.py b/tests/helpers/test_deprecation.py index 9a24cda74dd..1216bd6e293 100644 --- a/tests/helpers/test_deprecation.py +++ b/tests/helpers/test_deprecation.py @@ -3,6 +3,7 @@ from unittest.mock import MagicMock, Mock, patch import pytest +from homeassistant.core import HomeAssistant from homeassistant.helpers.deprecation import ( deprecated_class, deprecated_function, @@ -10,6 +11,8 @@ from homeassistant.helpers.deprecation import ( get_deprecated, ) +from tests.common import MockModule, mock_integration + class MockBaseClassDeprecatedProperty: """Mock base class for deprecated testing.""" @@ -173,6 +176,7 @@ def test_deprecated_function_called_from_built_in_integration( def test_deprecated_function_called_from_custom_integration( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture, ) -> None: """Test deprecated_function decorator. @@ -180,6 +184,8 @@ def test_deprecated_function_called_from_custom_integration( This tests the behavior when the calling integration is custom. """ + mock_integration(hass, MockModule("hue"), built_in=False) + @deprecated_function("new_function") def mock_deprecated_function(): pass @@ -207,6 +213,6 @@ def test_deprecated_function_called_from_custom_integration( mock_deprecated_function() assert ( "mock_deprecated_function was called from hue, this is a deprecated function. " - "Use new_function instead, please report this to the maintainer of hue" - in caplog.text + "Use new_function instead, please report it to the author of the 'hue' custom " + "integration" in caplog.text )