From 296c27859eb823cd711f53a5d65d46becf0e6ee8 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 11 Aug 2023 03:57:42 +0200 Subject: [PATCH] Fix issue registry sending unneeded update events (#98230) --- homeassistant/helpers/issue_registry.py | 14 ++++++--- tests/helpers/test_issue_registry.py | 42 ++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/homeassistant/helpers/issue_registry.py b/homeassistant/helpers/issue_registry.py index 9bd6ebffadb..30866ccf7cd 100644 --- a/homeassistant/helpers/issue_registry.py +++ b/homeassistant/helpers/issue_registry.py @@ -154,7 +154,7 @@ class IssueRegistry: {"action": "create", "domain": domain, "issue_id": issue_id}, ) else: - issue = self.issues[(domain, issue_id)] = dataclasses.replace( + replacement = dataclasses.replace( issue, active=True, breaks_in_ha_version=breaks_in_ha_version, @@ -167,10 +167,14 @@ class IssueRegistry: translation_key=translation_key, translation_placeholders=translation_placeholders, ) - self.hass.bus.async_fire( - EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED, - {"action": "update", "domain": domain, "issue_id": issue_id}, - ) + # Only fire is something changed + if replacement != issue: + issue = self.issues[(domain, issue_id)] = replacement + self.async_schedule_save() + self.hass.bus.async_fire( + EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED, + {"action": "update", "domain": domain, "issue_id": issue_id}, + ) return issue diff --git a/tests/helpers/test_issue_registry.py b/tests/helpers/test_issue_registry.py index 51cffbc7810..d184ccf0a2b 100644 --- a/tests/helpers/test_issue_registry.py +++ b/tests/helpers/test_issue_registry.py @@ -109,11 +109,51 @@ async def test_load_issues(hass: HomeAssistant) -> None: "issue_id": "issue_1", } - ir.async_delete_issue(hass, issues[2]["domain"], issues[2]["issue_id"]) + # Update an issue by creating it again with the same value, + # no update event should be fired, as nothing changed. + ir.async_create_issue( + hass, + issues[2]["domain"], + issues[2]["issue_id"], + breaks_in_ha_version=issues[2]["breaks_in_ha_version"], + is_fixable=issues[2]["is_fixable"], + is_persistent=issues[2]["is_persistent"], + learn_more_url=issues[2]["learn_more_url"], + severity=issues[2]["severity"], + translation_key=issues[2]["translation_key"], + translation_placeholders=issues[2]["translation_placeholders"], + ) + await hass.async_block_till_done() + + assert len(events) == 5 + + # Update an issue by creating it again, url changed + ir.async_create_issue( + hass, + issues[2]["domain"], + issues[2]["issue_id"], + breaks_in_ha_version=issues[2]["breaks_in_ha_version"], + is_fixable=issues[2]["is_fixable"], + is_persistent=issues[2]["is_persistent"], + learn_more_url="https://www.example.com/something_changed", + severity=issues[2]["severity"], + translation_key=issues[2]["translation_key"], + translation_placeholders=issues[2]["translation_placeholders"], + ) await hass.async_block_till_done() assert len(events) == 6 assert events[5].data == { + "action": "update", + "domain": "test", + "issue_id": "issue_3", + } + + ir.async_delete_issue(hass, issues[2]["domain"], issues[2]["issue_id"]) + await hass.async_block_till_done() + + assert len(events) == 7 + assert events[6].data == { "action": "remove", "domain": "test", "issue_id": "issue_3",