Add events to repairs issue registry changes (#75784)

This commit is contained in:
Franck Nijhof 2022-07-26 22:42:19 +02:00 committed by GitHub
parent e2dd2c9424
commit 0ff34f232c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 2 deletions

View File

@ -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

View File

@ -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