Improve Entity._suggest_report_issue (#100204)

This commit is contained in:
Erik Montnemery 2023-09-12 21:07:32 +02:00 committed by GitHub
parent 76c569c62d
commit bbcbb2e322
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 4 deletions

View File

@ -4,6 +4,7 @@ from __future__ import annotations
from abc import ABC
import asyncio
from collections.abc import Coroutine, Iterable, Mapping, MutableMapping
from contextlib import suppress
from dataclasses import dataclass
from datetime import timedelta
from enum import Enum, auto
@ -49,7 +50,11 @@ from homeassistant.exceptions import (
InvalidStateError,
NoEntitySpecifiedError,
)
from homeassistant.loader import bind_hass
from homeassistant.loader import (
IntegrationNotLoaded,
async_get_loaded_integration,
bind_hass,
)
from homeassistant.util import ensure_unique_string, slugify
from . import device_registry as dr, entity_registry as er
@ -1215,8 +1220,21 @@ class Entity(ABC):
def _suggest_report_issue(self) -> str:
"""Suggest to report an issue."""
report_issue = ""
integration = None
# The check for self.platform guards against integrations not using an
# EntityComponent and can be removed in HA Core 2024.1
if self.platform:
with suppress(IntegrationNotLoaded):
integration = async_get_loaded_integration(
self.hass, self.platform.platform_name
)
if "custom_components" in type(self).__module__:
report_issue = "report it to the custom integration author."
if integration and integration.issue_tracker:
report_issue = f"create a bug report at {integration.issue_tracker}"
else:
report_issue = "report it to the custom integration author"
else:
report_issue = (
"create a bug report at "

View File

@ -164,7 +164,7 @@ async def test_deprecated_last_reset(
f"with state_class {state_class} has set last_reset. Setting last_reset for "
"entities with state_class other than 'total' is not supported. Please update "
"your configuration if state_class is manually configured, otherwise report it "
"to the custom integration author."
"to the custom integration author"
) in caplog.text
state = hass.states.get("sensor.test")

View File

@ -27,8 +27,10 @@ from tests.common import (
MockConfigEntry,
MockEntity,
MockEntityPlatform,
MockModule,
MockPlatform,
get_test_home_assistant,
mock_integration,
mock_registry,
)
@ -776,7 +778,7 @@ async def test_warn_slow_write_state_custom_component(
assert (
"Updating state for comp_test.test_entity "
"(<class 'custom_components.bla.sensor.test_warn_slow_write_state_custom_component.<locals>.CustomComponentEntity'>) "
"took 10.000 seconds. Please report it to the custom integration author."
"took 10.000 seconds. Please report it to the custom integration author"
) in caplog.text
@ -1503,3 +1505,57 @@ async def test_invalid_state(
ent._attr_state = "x" * 255
ent.async_write_ha_state()
assert hass.states.get("test.test").state == "x" * 255
async def test_suggest_report_issue_built_in(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test _suggest_report_issue for an entity from a built-in integration."""
mock_entity = entity.Entity()
mock_entity.entity_id = "comp_test.test_entity"
suggestion = mock_entity._suggest_report_issue()
assert suggestion == (
"create a bug report at https://github.com/home-assistant/core/issues"
"?q=is%3Aopen+is%3Aissue"
)
mock_integration(hass, MockModule(domain="test"), built_in=True)
platform = MockEntityPlatform(hass, domain="comp_test", platform_name="test")
await platform.async_add_entities([mock_entity])
suggestion = mock_entity._suggest_report_issue()
assert suggestion == (
"create a bug report at https://github.com/home-assistant/core/issues"
"?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+test%22"
)
async def test_suggest_report_issue_custom_component(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test _suggest_report_issue for an entity from a custom component."""
class CustomComponentEntity(entity.Entity):
"""Custom component entity."""
__module__ = "custom_components.bla.sensor"
mock_entity = CustomComponentEntity()
mock_entity.entity_id = "comp_test.test_entity"
suggestion = mock_entity._suggest_report_issue()
assert suggestion == "report it to the custom integration author"
mock_integration(
hass,
MockModule(
domain="test", partial_manifest={"issue_tracker": "httpts://some_url"}
),
built_in=False,
)
platform = MockEntityPlatform(hass, domain="comp_test", platform_name="test")
await platform.async_add_entities([mock_entity])
suggestion = mock_entity._suggest_report_issue()
assert suggestion == "create a bug report at httpts://some_url"