mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Improve Entity._suggest_report_issue (#100204)
This commit is contained in:
parent
76c569c62d
commit
bbcbb2e322
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||||||
from abc import ABC
|
from abc import ABC
|
||||||
import asyncio
|
import asyncio
|
||||||
from collections.abc import Coroutine, Iterable, Mapping, MutableMapping
|
from collections.abc import Coroutine, Iterable, Mapping, MutableMapping
|
||||||
|
from contextlib import suppress
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from enum import Enum, auto
|
from enum import Enum, auto
|
||||||
@ -49,7 +50,11 @@ from homeassistant.exceptions import (
|
|||||||
InvalidStateError,
|
InvalidStateError,
|
||||||
NoEntitySpecifiedError,
|
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 homeassistant.util import ensure_unique_string, slugify
|
||||||
|
|
||||||
from . import device_registry as dr, entity_registry as er
|
from . import device_registry as dr, entity_registry as er
|
||||||
@ -1215,8 +1220,21 @@ class Entity(ABC):
|
|||||||
def _suggest_report_issue(self) -> str:
|
def _suggest_report_issue(self) -> str:
|
||||||
"""Suggest to report an issue."""
|
"""Suggest to report an issue."""
|
||||||
report_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__:
|
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:
|
else:
|
||||||
report_issue = (
|
report_issue = (
|
||||||
"create a bug report at "
|
"create a bug report at "
|
||||||
|
@ -164,7 +164,7 @@ async def test_deprecated_last_reset(
|
|||||||
f"with state_class {state_class} has set last_reset. Setting last_reset for "
|
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 "
|
"entities with state_class other than 'total' is not supported. Please update "
|
||||||
"your configuration if state_class is manually configured, otherwise report it "
|
"your configuration if state_class is manually configured, otherwise report it "
|
||||||
"to the custom integration author."
|
"to the custom integration author"
|
||||||
) in caplog.text
|
) in caplog.text
|
||||||
|
|
||||||
state = hass.states.get("sensor.test")
|
state = hass.states.get("sensor.test")
|
||||||
|
@ -27,8 +27,10 @@ from tests.common import (
|
|||||||
MockConfigEntry,
|
MockConfigEntry,
|
||||||
MockEntity,
|
MockEntity,
|
||||||
MockEntityPlatform,
|
MockEntityPlatform,
|
||||||
|
MockModule,
|
||||||
MockPlatform,
|
MockPlatform,
|
||||||
get_test_home_assistant,
|
get_test_home_assistant,
|
||||||
|
mock_integration,
|
||||||
mock_registry,
|
mock_registry,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -776,7 +778,7 @@ async def test_warn_slow_write_state_custom_component(
|
|||||||
assert (
|
assert (
|
||||||
"Updating state for comp_test.test_entity "
|
"Updating state for comp_test.test_entity "
|
||||||
"(<class 'custom_components.bla.sensor.test_warn_slow_write_state_custom_component.<locals>.CustomComponentEntity'>) "
|
"(<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
|
) in caplog.text
|
||||||
|
|
||||||
|
|
||||||
@ -1503,3 +1505,57 @@ async def test_invalid_state(
|
|||||||
ent._attr_state = "x" * 255
|
ent._attr_state = "x" * 255
|
||||||
ent.async_write_ha_state()
|
ent.async_write_ha_state()
|
||||||
assert hass.states.get("test.test").state == "x" * 255
|
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"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user