From 0ff34f232c931367d5c221170bd2c31de65d07a6 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 26 Jul 2022 22:42:19 +0200 Subject: [PATCH] Add events to repairs issue registry changes (#75784) --- .../components/repairs/issue_registry.py | 17 ++++++ .../components/repairs/test_issue_registry.py | 59 ++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/repairs/issue_registry.py b/homeassistant/components/repairs/issue_registry.py index c1eda0d53be..5c459309cc0 100644 --- a/homeassistant/components/repairs/issue_registry.py +++ b/homeassistant/components/repairs/issue_registry.py @@ -13,6 +13,7 @@ import homeassistant.util.dt as dt_util from .models import IssueSeverity DATA_REGISTRY = "issue_registry" +EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED = "repairs_issue_registry_updated" STORAGE_KEY = "repairs.issue_registry" STORAGE_VERSION = 1 SAVE_DELAY = 10 @@ -82,6 +83,10 @@ class IssueRegistry: ) self.issues[(domain, issue_id)] = issue self.async_schedule_save() + self.hass.bus.async_fire( + EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED, + {"action": "create", "domain": domain, "issue_id": issue_id}, + ) else: issue = self.issues[(domain, issue_id)] = dataclasses.replace( issue, @@ -93,6 +98,10 @@ 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}, + ) return issue @@ -103,6 +112,10 @@ class IssueRegistry: return self.async_schedule_save() + self.hass.bus.async_fire( + EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED, + {"action": "remove", "domain": domain, "issue_id": issue_id}, + ) @callback def async_ignore(self, domain: str, issue_id: str, ignore: bool) -> IssueEntry: @@ -118,6 +131,10 @@ class IssueRegistry: ) 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/components/repairs/test_issue_registry.py b/tests/components/repairs/test_issue_registry.py index 1af67601581..523f75bfdc2 100644 --- a/tests/components/repairs/test_issue_registry.py +++ b/tests/components/repairs/test_issue_registry.py @@ -1,11 +1,14 @@ """Test the repairs websocket API.""" from homeassistant.components.repairs import async_create_issue, issue_registry from homeassistant.components.repairs.const import DOMAIN -from homeassistant.components.repairs.issue_handler import async_ignore_issue +from homeassistant.components.repairs.issue_handler import ( + async_delete_issue, + async_ignore_issue, +) from homeassistant.core import HomeAssistant from homeassistant.setup import async_setup_component -from tests.common import flush_store +from tests.common import async_capture_events, flush_store async def test_load_issues(hass: HomeAssistant) -> None: @@ -33,8 +36,22 @@ async def test_load_issues(hass: HomeAssistant) -> None: "translation_key": "even_worse", "translation_placeholders": {"def": "456"}, }, + { + "breaks_in_ha_version": "2022.7", + "domain": "test", + "issue_id": "issue_3", + "is_fixable": True, + "learn_more_url": "https://checkboxrace.com", + "severity": "other", + "translation_key": "even_worse", + "translation_placeholders": {"def": "789"}, + }, ] + events = async_capture_events( + hass, issue_registry.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED + ) + for issue in issues: async_create_issue( hass, @@ -47,7 +64,45 @@ async def test_load_issues(hass: HomeAssistant) -> None: translation_key=issue["translation_key"], translation_placeholders=issue["translation_placeholders"], ) + + await hass.async_block_till_done() + + assert len(events) == 3 + assert events[0].data == { + "action": "create", + "domain": "test", + "issue_id": "issue_1", + } + assert events[1].data == { + "action": "create", + "domain": "test", + "issue_id": "issue_2", + } + assert events[2].data == { + "action": "create", + "domain": "test", + "issue_id": "issue_3", + } + async_ignore_issue(hass, issues[0]["domain"], issues[0]["issue_id"], True) + await hass.async_block_till_done() + + assert len(events) == 4 + assert events[3].data == { + "action": "update", + "domain": "test", + "issue_id": "issue_1", + } + + async_delete_issue(hass, issues[2]["domain"], issues[2]["issue_id"]) + await hass.async_block_till_done() + + assert len(events) == 5 + assert events[4].data == { + "action": "remove", + "domain": "test", + "issue_id": "issue_3", + } registry: issue_registry.IssueRegistry = hass.data[issue_registry.DATA_REGISTRY] assert len(registry.issues) == 2