From dce5099d92bce99bc1e072ff3ab4d4d539d557cd Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 5 Oct 2023 19:52:26 +0200 Subject: [PATCH] Use loader.async_suggest_report_issue in frame helper (#101461) --- homeassistant/helpers/frame.py | 25 +++++++++++++++---------- tests/helpers/test_aiohttp_client.py | 18 ++++++++++-------- tests/helpers/test_frame.py | 9 +++++---- tests/helpers/test_httpx_client.py | 19 +++++++++++-------- 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/homeassistant/helpers/frame.py b/homeassistant/helpers/frame.py index 19767c39284..920c7150f6d 100644 --- a/homeassistant/helpers/frame.py +++ b/homeassistant/helpers/frame.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio from collections.abc import Callable +from contextlib import suppress from dataclasses import dataclass import functools import logging @@ -10,7 +11,9 @@ import sys from traceback import FrameSummary, extract_stack from typing import Any, TypeVar, cast +from homeassistant.core import HomeAssistant, async_get_hass from homeassistant.exceptions import HomeAssistantError +from homeassistant.loader import async_suggest_report_issue _LOGGER = logging.getLogger(__name__) @@ -118,23 +121,25 @@ def _report_integration( return _REPORTED_INTEGRATIONS.add(key) - if integration_frame.custom_integration: - extra = " to the custom integration author" - else: - extra = "" + 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.log( level, - ( - "Detected integration that %s. " - "Please report issue%s for %s using this method at %s, line %s: %s" - ), - what, - extra, + "Detected that %sintegration '%s' %s at %s, line %s: %s, please %s", + "custom " if integration_frame.custom_integration else "", integration_frame.integration, + what, integration_frame.relative_filename, found_frame.lineno, (found_frame.line or "?").strip(), + report_issue, ) diff --git a/tests/helpers/test_aiohttp_client.py b/tests/helpers/test_aiohttp_client.py index fe7ffca9a47..daeb324b19f 100644 --- a/tests/helpers/test_aiohttp_client.py +++ b/tests/helpers/test_aiohttp_client.py @@ -21,7 +21,7 @@ from homeassistant.core import EVENT_HOMEASSISTANT_CLOSE, HomeAssistant import homeassistant.helpers.aiohttp_client as client from homeassistant.util.color import RGBColor -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, MockModule, mock_integration from tests.test_util.aiohttp import AiohttpClientMocker @@ -155,9 +155,10 @@ async def test_warning_close_session_integration( session = client.async_get_clientsession(hass) await session.close() assert ( - "Detected integration that closes the Home Assistant aiohttp session. " - "Please report issue for hue using this method at " - "homeassistant/components/hue/light.py, line 23: await session.close()" + "Detected that integration 'hue' closes the Home Assistant aiohttp session at " + "homeassistant/components/hue/light.py, line 23: await session.close(), " + "please create a bug report at https://github.com/home-assistant/core/issues?" + "q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+hue%22" ) in caplog.text @@ -166,6 +167,7 @@ async def test_warning_close_session_custom( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test log warning message when closing the session from custom context.""" + mock_integration(hass, MockModule("hue"), built_in=False) with patch( "homeassistant.helpers.frame.extract_stack", return_value=[ @@ -189,10 +191,10 @@ async def test_warning_close_session_custom( session = client.async_get_clientsession(hass) await session.close() assert ( - "Detected integration that closes the Home Assistant aiohttp session. Please" - " report issue to the custom integration author for hue using this method at" - " custom_components/hue/light.py, line 23: await session.close()" in caplog.text - ) + "Detected that custom integration 'hue' closes the Home Assistant aiohttp " + "session at custom_components/hue/light.py, line 23: await session.close(), " + "please report it to the author of the 'hue' custom integration" + ) in caplog.text async def test_async_aiohttp_proxy_stream( diff --git a/tests/helpers/test_frame.py b/tests/helpers/test_frame.py index 6756f14bf08..f1547f36e39 100644 --- a/tests/helpers/test_frame.py +++ b/tests/helpers/test_frame.py @@ -132,7 +132,7 @@ async def test_extract_frame_no_integration(caplog: pytest.LogCaptureFixture) -> @patch.object(frame, "_REPORTED_INTEGRATIONS", set()) async def test_prevent_flooding( - caplog: pytest.LogCaptureFixture, mock_integration_frame: Mock + hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_integration_frame: Mock ) -> None: """Test to ensure a report is only written once to the log.""" @@ -142,9 +142,10 @@ async def test_prevent_flooding( filename = "homeassistant/components/hue/light.py" expected_message = ( - f"Detected integration that {what}. Please report issue for {integration} using" - f" this method at {filename}, line " - f"{mock_integration_frame.lineno}: {mock_integration_frame.line}" + f"Detected that integration '{integration}' {what} at {filename}, line " + f"{mock_integration_frame.lineno}: {mock_integration_frame.line}, " + f"please create a bug report at https://github.com/home-assistant/core/issues?" + f"q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+{integration}%22" ) frame.report(what, error_if_core=False) diff --git a/tests/helpers/test_httpx_client.py b/tests/helpers/test_httpx_client.py index f9473ffaf87..693c45cc73a 100644 --- a/tests/helpers/test_httpx_client.py +++ b/tests/helpers/test_httpx_client.py @@ -7,6 +7,8 @@ import pytest from homeassistant.core import EVENT_HOMEASSISTANT_CLOSE, HomeAssistant import homeassistant.helpers.httpx_client as client +from tests.common import MockModule, mock_integration + async def test_get_async_client_with_ssl(hass: HomeAssistant) -> None: """Test init async client with ssl.""" @@ -125,9 +127,10 @@ async def test_warning_close_session_integration( await httpx_session.aclose() assert ( - "Detected integration that closes the Home Assistant httpx client. " - "Please report issue for hue using this method at " - "homeassistant/components/hue/light.py, line 23: await session.aclose()" + "Detected that integration 'hue' closes the Home Assistant httpx client at " + "homeassistant/components/hue/light.py, line 23: await session.aclose(), " + "please create a bug report at https://github.com/home-assistant/core/issues?" + "q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+hue%22" ) in caplog.text @@ -136,6 +139,7 @@ async def test_warning_close_session_custom( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test log warning message when closing the session from custom context.""" + mock_integration(hass, MockModule("hue"), built_in=False) with patch( "homeassistant.helpers.frame.extract_stack", return_value=[ @@ -159,8 +163,7 @@ async def test_warning_close_session_custom( httpx_session = client.get_async_client(hass) await httpx_session.aclose() assert ( - "Detected integration that closes the Home Assistant httpx client. Please" - " report issue to the custom integration author for hue using this method at" - " custom_components/hue/light.py, line 23: await session.aclose()" - in caplog.text - ) + "Detected that custom integration 'hue' closes the Home Assistant httpx client " + "at custom_components/hue/light.py, line 23: await session.aclose(), " + "please report it to the author of the 'hue' custom integration" + ) in caplog.text