Use loader.async_suggest_report_issue in deprecation helper (#101393)

This commit is contained in:
Erik Montnemery 2023-10-06 09:51:08 +02:00 committed by GitHub
parent 60fa63a1f0
commit 3478666973
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

View File

@ -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(

View File

@ -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
)