mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Improve automation repair issue handling (#185456)
This commit is contained in:
@@ -30,6 +30,7 @@ from homeassistant.core import (
|
||||
from homeassistant.exceptions import HomeAssistantError, ServiceNotFound, TemplateError
|
||||
from homeassistant.helpers import (
|
||||
condition as condition_helper,
|
||||
issue_registry as ir,
|
||||
trigger as trigger_helper,
|
||||
)
|
||||
from homeassistant.helpers.entity import ToggleEntity
|
||||
@@ -566,20 +567,8 @@ class AutomationEntity(BaseAutomationEntity, RestoreEntity):
|
||||
variables, trigger_context, started_action
|
||||
)
|
||||
except ServiceNotFound as err:
|
||||
async_create_issue(
|
||||
self.hass,
|
||||
DOMAIN,
|
||||
f"{self.entity_id}_service_not_found_{err.domain}.{err.service}",
|
||||
is_fixable=True,
|
||||
is_persistent=True,
|
||||
severity=IssueSeverity.ERROR,
|
||||
translation_key="service_not_found",
|
||||
translation_placeholders={
|
||||
"service": f"{err.domain}.{err.service}",
|
||||
"entity_id": self.entity_id,
|
||||
"name": self._attr_name or self.entity_id,
|
||||
"edit": f"/config/automation/edit/{self.unique_id}",
|
||||
},
|
||||
self._async_create_service_not_found_issue(
|
||||
f"{err.domain}.{err.service}"
|
||||
)
|
||||
automation_trace.set_error(err)
|
||||
except (probatio.Invalid, HomeAssistantError) as err:
|
||||
@@ -615,6 +604,7 @@ class AutomationEntity(BaseAutomationEntity, RestoreEntity):
|
||||
"""
|
||||
super().async_entity_id_changed(old_entity_id)
|
||||
self._async_update_logger()
|
||||
self._async_move_service_not_found_issues(old_entity_id)
|
||||
if (detach_triggers := self._async_detach_triggers) is None:
|
||||
# Not attached, or an attach in progress retries with the new entity_id
|
||||
return
|
||||
@@ -627,6 +617,58 @@ class AutomationEntity(BaseAutomationEntity, RestoreEntity):
|
||||
f"automation {self.entity_id} reattach",
|
||||
)
|
||||
|
||||
@callback
|
||||
def _async_create_service_not_found_issue(self, service: str) -> str:
|
||||
"""Create a repair issue for a missing service and return its id."""
|
||||
issue_id = f"{self.entity_id}_service_not_found_{service}"
|
||||
async_create_issue(
|
||||
self.hass,
|
||||
DOMAIN,
|
||||
issue_id,
|
||||
is_fixable=True,
|
||||
is_persistent=True,
|
||||
severity=IssueSeverity.ERROR,
|
||||
translation_key="service_not_found",
|
||||
translation_placeholders={
|
||||
"service": service,
|
||||
"entity_id": self.entity_id,
|
||||
"name": self._attr_name or self.entity_id,
|
||||
"edit": self._edit_path,
|
||||
},
|
||||
)
|
||||
return issue_id
|
||||
|
||||
@property
|
||||
def _edit_path(self) -> str:
|
||||
"""Return the frontend path for editing the automation."""
|
||||
return f"/config/automation/edit/{self.unique_id}"
|
||||
|
||||
@callback
|
||||
def _async_move_service_not_found_issues(self, old_entity_id: str) -> None:
|
||||
"""Move missing service issues, their id and placeholders use entity_id."""
|
||||
issue_registry = ir.async_get(self.hass)
|
||||
for (domain, issue_id), issue in list(issue_registry.issues.items()):
|
||||
# Matched on the placeholders, an issue_id prefix can match another
|
||||
# automation's entity_id, and a deleted automation's issue can have
|
||||
# the same entity_id
|
||||
if (
|
||||
domain != DOMAIN
|
||||
or issue.translation_key != "service_not_found"
|
||||
or issue.translation_placeholders is None
|
||||
or issue.translation_placeholders["entity_id"] != old_entity_id
|
||||
or issue.translation_placeholders["edit"] != self._edit_path
|
||||
):
|
||||
continue
|
||||
async_delete_issue(self.hass, DOMAIN, issue_id)
|
||||
new_issue_id = self._async_create_service_not_found_issue(
|
||||
issue.translation_placeholders["service"]
|
||||
)
|
||||
# An existing issue under the new id, e.g. left by a deleted automation,
|
||||
# keeps its own ignored state when created again
|
||||
issue_registry.async_ignore(
|
||||
DOMAIN, new_issue_id, issue.dismissed_version is not None
|
||||
)
|
||||
|
||||
async def _async_attach_triggers_for_entity_id(self) -> None:
|
||||
"""Attach the triggers, retrying if the entity_id changes while attaching.
|
||||
|
||||
|
||||
@@ -1918,6 +1918,167 @@ async def test_automation_with_error_in_script(
|
||||
assert issues[0]["issue_id"] == "automation.hello_service_not_found_test.automation"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ignored", [False, True])
|
||||
async def test_automation_changed_entity_id_moves_service_not_found_issue(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
ignored: bool,
|
||||
) -> None:
|
||||
"""Test service not found issues follow an entity_id change."""
|
||||
entry = entity_registry.async_get_or_create(
|
||||
"automation", "automation", "hello", suggested_object_id="hello"
|
||||
)
|
||||
entity_registry.async_get_or_create(
|
||||
"automation", "automation", "hello_2", suggested_object_id="hello_2"
|
||||
)
|
||||
# Left by a deleted automation which had the same entity_id
|
||||
deleted_issue_id = "automation.hello_service_not_found_test.other"
|
||||
ir.async_create_issue(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
deleted_issue_id,
|
||||
is_fixable=True,
|
||||
is_persistent=True,
|
||||
severity=ir.IssueSeverity.ERROR,
|
||||
translation_key="service_not_found",
|
||||
translation_placeholders={
|
||||
"service": "test.other",
|
||||
"entity_id": "automation.hello",
|
||||
"name": "Deleted",
|
||||
"edit": "/config/automation/edit/deleted",
|
||||
},
|
||||
)
|
||||
deleted_issue = issue_registry.async_get_issue(automation.DOMAIN, deleted_issue_id)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"id": "hello",
|
||||
"alias": "Hello",
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"action": {"action": "test.automation"},
|
||||
},
|
||||
{
|
||||
"id": "hello_2",
|
||||
"alias": "Hello 2",
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"action": {"action": "test.automation"},
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
old_issue_id = "automation.hello_service_not_found_test.automation"
|
||||
other_issue_id = "automation.hello_2_service_not_found_test.automation"
|
||||
assert issue_registry.async_get_issue(automation.DOMAIN, old_issue_id)
|
||||
other_issue = issue_registry.async_get_issue(automation.DOMAIN, other_issue_id)
|
||||
assert other_issue
|
||||
ir.async_ignore_issue(hass, automation.DOMAIN, old_issue_id, ignored)
|
||||
|
||||
entity_registry.async_update_entity(
|
||||
entry.entity_id, new_entity_id="automation.renamed"
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert issue_registry.async_get_issue(automation.DOMAIN, old_issue_id) is None
|
||||
issue = issue_registry.async_get_issue(
|
||||
automation.DOMAIN, "automation.renamed_service_not_found_test.automation"
|
||||
)
|
||||
assert issue
|
||||
assert issue.is_persistent
|
||||
assert (issue.dismissed_version is not None) is ignored
|
||||
assert issue.translation_placeholders == {
|
||||
"service": "test.automation",
|
||||
"entity_id": "automation.renamed",
|
||||
"name": "Hello",
|
||||
"edit": "/config/automation/edit/hello",
|
||||
}
|
||||
# Another automation's issue is not moved, its id starts with the old entity_id
|
||||
assert (
|
||||
issue_registry.async_get_issue(automation.DOMAIN, other_issue_id) == other_issue
|
||||
)
|
||||
# A deleted automation's issue is not moved, it has the old entity_id
|
||||
assert (
|
||||
issue_registry.async_get_issue(automation.DOMAIN, deleted_issue_id)
|
||||
== deleted_issue
|
||||
)
|
||||
assert (
|
||||
issue_registry.async_get_issue(
|
||||
automation.DOMAIN, "automation.renamed_service_not_found_test.other"
|
||||
)
|
||||
is None
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ignored", [False, True])
|
||||
@pytest.mark.parametrize("destination_ignored", [False, True])
|
||||
async def test_automation_changed_entity_id_moves_service_not_found_issue_onto_stale(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
ignored: bool,
|
||||
destination_ignored: bool,
|
||||
) -> None:
|
||||
"""Test a moved issue keeps its ignored state over a stale issue at its new id."""
|
||||
entry = entity_registry.async_get_or_create(
|
||||
"automation", "automation", "hello", suggested_object_id="hello"
|
||||
)
|
||||
new_issue_id = "automation.renamed_service_not_found_test.automation"
|
||||
# Left by a deleted automation which had the new entity_id
|
||||
ir.async_create_issue(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
new_issue_id,
|
||||
is_fixable=True,
|
||||
is_persistent=True,
|
||||
severity=ir.IssueSeverity.ERROR,
|
||||
translation_key="service_not_found",
|
||||
translation_placeholders={
|
||||
"service": "test.automation",
|
||||
"entity_id": "automation.renamed",
|
||||
"name": "Deleted",
|
||||
"edit": "/config/automation/edit/deleted",
|
||||
},
|
||||
)
|
||||
ir.async_ignore_issue(hass, automation.DOMAIN, new_issue_id, destination_ignored)
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"id": "hello",
|
||||
"alias": "Hello",
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"action": {"action": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
old_issue_id = "automation.hello_service_not_found_test.automation"
|
||||
ir.async_ignore_issue(hass, automation.DOMAIN, old_issue_id, ignored)
|
||||
|
||||
entity_registry.async_update_entity(
|
||||
entry.entity_id, new_entity_id="automation.renamed"
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert issue_registry.async_get_issue(automation.DOMAIN, old_issue_id) is None
|
||||
issue = issue_registry.async_get_issue(automation.DOMAIN, new_issue_id)
|
||||
assert issue
|
||||
assert (issue.dismissed_version is not None) is ignored
|
||||
assert issue.translation_placeholders == {
|
||||
"service": "test.automation",
|
||||
"entity_id": "automation.renamed",
|
||||
"name": "Hello",
|
||||
"edit": "/config/automation/edit/hello",
|
||||
}
|
||||
|
||||
|
||||
async def test_automation_with_error_in_script_2(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user