From 403bbda9592a19ef1f2490de0cc55799256eaf31 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 19 Jul 2022 13:58:39 +0200 Subject: [PATCH] Rename resolution_center dismiss to ignore (#75432) --- .../resolution_center/issue_handler.py | 8 ++- .../resolution_center/issue_registry.py | 9 +-- .../resolution_center/websocket_api.py | 13 ++-- .../components/resolution_center/test_init.py | 60 ++++++++++++------- .../resolution_center/test_issue_registry.py | 4 +- .../resolution_center/test_websocket_api.py | 43 +++++++++++-- 6 files changed, 96 insertions(+), 41 deletions(-) diff --git a/homeassistant/components/resolution_center/issue_handler.py b/homeassistant/components/resolution_center/issue_handler.py index 210a998ce14..78085e2cec6 100644 --- a/homeassistant/components/resolution_center/issue_handler.py +++ b/homeassistant/components/resolution_center/issue_handler.py @@ -128,10 +128,12 @@ def async_delete_issue(hass: HomeAssistant, domain: str, issue_id: str) -> None: @callback -def async_dismiss_issue(hass: HomeAssistant, domain: str, issue_id: str) -> None: - """Dismiss an issue. +def async_ignore_issue( + hass: HomeAssistant, domain: str, issue_id: str, ignore: bool +) -> None: + """Ignore an issue. Will raise if the issue does not exist. """ issue_registry = async_get_issue_registry(hass) - issue_registry.async_dismiss(domain, issue_id) + issue_registry.async_ignore(domain, issue_id, ignore) diff --git a/homeassistant/components/resolution_center/issue_registry.py b/homeassistant/components/resolution_center/issue_registry.py index aed1cd51b10..e398d58b3e0 100644 --- a/homeassistant/components/resolution_center/issue_registry.py +++ b/homeassistant/components/resolution_center/issue_registry.py @@ -105,15 +105,16 @@ class IssueRegistry: self.async_schedule_save() @callback - def async_dismiss(self, domain: str, issue_id: str) -> IssueEntry: - """Dismiss issue.""" + def async_ignore(self, domain: str, issue_id: str, ignore: bool) -> IssueEntry: + """Ignore issue.""" old = self.issues[(domain, issue_id)] - if old.dismissed_version == ha_version: + dismissed_version = ha_version if ignore else None + if old.dismissed_version == dismissed_version: return old issue = self.issues[(domain, issue_id)] = dataclasses.replace( old, - dismissed_version=ha_version, + dismissed_version=dismissed_version, ) self.async_schedule_save() diff --git a/homeassistant/components/resolution_center/websocket_api.py b/homeassistant/components/resolution_center/websocket_api.py index 314df009118..e111c2b3e50 100644 --- a/homeassistant/components/resolution_center/websocket_api.py +++ b/homeassistant/components/resolution_center/websocket_api.py @@ -20,14 +20,14 @@ from homeassistant.helpers.data_entry_flow import ( ) from .const import DOMAIN -from .issue_handler import async_dismiss_issue +from .issue_handler import async_ignore_issue from .issue_registry import async_get as async_get_issue_registry @callback def async_setup(hass: HomeAssistant) -> None: """Set up the resolution center websocket API.""" - websocket_api.async_register_command(hass, ws_dismiss_issue) + websocket_api.async_register_command(hass, ws_ignore_issue) websocket_api.async_register_command(hass, ws_list_issues) hass.http.register_view( @@ -41,16 +41,17 @@ def async_setup(hass: HomeAssistant) -> None: @callback @websocket_api.websocket_command( { - vol.Required("type"): "resolution_center/dismiss_issue", + vol.Required("type"): "resolution_center/ignore_issue", vol.Required("domain"): str, vol.Required("issue_id"): str, + vol.Required("ignore"): bool, } ) -def ws_dismiss_issue( +def ws_ignore_issue( hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict ) -> None: """Fix an issue.""" - async_dismiss_issue(hass, msg["domain"], msg["issue_id"]) + async_ignore_issue(hass, msg["domain"], msg["issue_id"], msg["ignore"]) connection.send_result(msg["id"]) @@ -68,7 +69,7 @@ def ws_list_issues( def ws_dict(kv_pairs: list[tuple[Any, Any]]) -> dict[Any, Any]: result = {k: v for k, v in kv_pairs if k not in ("active")} - result["dismissed"] = result["dismissed_version"] is not None + result["ignored"] = result["dismissed_version"] is not None result["created"] = result["created"].isoformat() return result diff --git a/tests/components/resolution_center/test_init.py b/tests/components/resolution_center/test_init.py index a707c3845fe..478c2d51f7f 100644 --- a/tests/components/resolution_center/test_init.py +++ b/tests/components/resolution_center/test_init.py @@ -10,7 +10,7 @@ from homeassistant.components.resolution_center import ( ) from homeassistant.components.resolution_center.const import DOMAIN from homeassistant.components.resolution_center.issue_handler import ( - async_dismiss_issue, + async_ignore_issue, async_process_resolution_center_platforms, ) from homeassistant.const import __version__ as ha_version @@ -78,8 +78,8 @@ async def test_create_update_issue(hass: HomeAssistant, hass_ws_client) -> None: dict( issue, created="2022-07-19T07:53:05+00:00", - dismissed=False, dismissed_version=None, + ignored=False, ) for issue in issues ] @@ -105,8 +105,8 @@ async def test_create_update_issue(hass: HomeAssistant, hass_ws_client) -> None: assert msg["result"]["issues"][0] == dict( issues[0], created="2022-07-19T07:53:05+00:00", - dismissed=False, dismissed_version=None, + ignored=False, learn_more_url="blablabla", ) @@ -152,8 +152,8 @@ async def test_create_issue_invalid_version( @freeze_time("2022-07-19 07:53:05") -async def test_dismiss_issue(hass: HomeAssistant, hass_ws_client) -> None: - """Test dismissing issues.""" +async def test_ignore_issue(hass: HomeAssistant, hass_ws_client) -> None: + """Test ignoring issues.""" assert await async_setup_component(hass, DOMAIN, {}) client = await hass_ws_client(hass) @@ -199,16 +199,16 @@ async def test_dismiss_issue(hass: HomeAssistant, hass_ws_client) -> None: dict( issue, created="2022-07-19T07:53:05+00:00", - dismissed=False, dismissed_version=None, + ignored=False, ) for issue in issues ] } - # Dismiss a non-existing issue + # Ignore a non-existing issue with pytest.raises(KeyError): - async_dismiss_issue(hass, issues[0]["domain"], "no_such_issue") + async_ignore_issue(hass, issues[0]["domain"], "no_such_issue", True) await client.send_json({"id": 3, "type": "resolution_center/list_issues"}) msg = await client.receive_json() @@ -219,15 +219,15 @@ async def test_dismiss_issue(hass: HomeAssistant, hass_ws_client) -> None: dict( issue, created="2022-07-19T07:53:05+00:00", - dismissed=False, dismissed_version=None, + ignored=False, ) for issue in issues ] } - # Dismiss an existing issue - async_dismiss_issue(hass, issues[0]["domain"], issues[0]["issue_id"]) + # Ignore an existing issue + async_ignore_issue(hass, issues[0]["domain"], issues[0]["issue_id"], True) await client.send_json({"id": 4, "type": "resolution_center/list_issues"}) msg = await client.receive_json() @@ -238,15 +238,15 @@ async def test_dismiss_issue(hass: HomeAssistant, hass_ws_client) -> None: dict( issue, created="2022-07-19T07:53:05+00:00", - dismissed=True, dismissed_version=ha_version, + ignored=True, ) for issue in issues ] } - # Dismiss the same issue again - async_dismiss_issue(hass, issues[0]["domain"], issues[0]["issue_id"]) + # Ignore the same issue again + async_ignore_issue(hass, issues[0]["domain"], issues[0]["issue_id"], True) await client.send_json({"id": 5, "type": "resolution_center/list_issues"}) msg = await client.receive_json() @@ -257,14 +257,14 @@ async def test_dismiss_issue(hass: HomeAssistant, hass_ws_client) -> None: dict( issue, created="2022-07-19T07:53:05+00:00", - dismissed=True, dismissed_version=ha_version, + ignored=True, ) for issue in issues ] } - # Update a dismissed issue + # Update an ignored issue async_create_issue( hass, issues[0]["domain"], @@ -284,11 +284,31 @@ async def test_dismiss_issue(hass: HomeAssistant, hass_ws_client) -> None: assert msg["result"]["issues"][0] == dict( issues[0], created="2022-07-19T07:53:05+00:00", - dismissed=True, dismissed_version=ha_version, + ignored=True, learn_more_url="blablabla", ) + # Unignore the same issue + async_ignore_issue(hass, issues[0]["domain"], issues[0]["issue_id"], False) + + await client.send_json({"id": 7, "type": "resolution_center/list_issues"}) + msg = await client.receive_json() + + assert msg["success"] + assert msg["result"] == { + "issues": [ + dict( + issue, + created="2022-07-19T07:53:05+00:00", + dismissed_version=None, + ignored=False, + learn_more_url="blablabla", + ) + for issue in issues + ] + } + async def test_delete_issue(hass: HomeAssistant, hass_ws_client, freezer) -> None: """Test we can delete an issue.""" @@ -332,8 +352,8 @@ async def test_delete_issue(hass: HomeAssistant, hass_ws_client, freezer) -> Non dict( issue, created="2022-07-19T07:53:05+00:00", - dismissed=False, dismissed_version=None, + ignored=False, ) for issue in issues ] @@ -351,8 +371,8 @@ async def test_delete_issue(hass: HomeAssistant, hass_ws_client, freezer) -> Non dict( issue, created="2022-07-19T07:53:05+00:00", - dismissed=False, dismissed_version=None, + ignored=False, ) for issue in issues ] @@ -401,8 +421,8 @@ async def test_delete_issue(hass: HomeAssistant, hass_ws_client, freezer) -> Non dict( issue, created="2022-07-19T08:53:05+00:00", - dismissed=False, dismissed_version=None, + ignored=False, ) for issue in issues ] diff --git a/tests/components/resolution_center/test_issue_registry.py b/tests/components/resolution_center/test_issue_registry.py index 3f5cc235a19..854a14fc84e 100644 --- a/tests/components/resolution_center/test_issue_registry.py +++ b/tests/components/resolution_center/test_issue_registry.py @@ -4,7 +4,7 @@ from homeassistant.components.resolution_center import ( issue_registry, ) from homeassistant.components.resolution_center.const import DOMAIN -from homeassistant.components.resolution_center.issue_handler import async_dismiss_issue +from homeassistant.components.resolution_center.issue_handler import async_ignore_issue from homeassistant.core import HomeAssistant from homeassistant.setup import async_setup_component @@ -50,7 +50,7 @@ async def test_load_issues(hass: HomeAssistant) -> None: translation_key=issue["translation_key"], translation_placeholders=issue["translation_placeholders"], ) - async_dismiss_issue(hass, issues[0]["domain"], issues[0]["issue_id"]) + async_ignore_issue(hass, issues[0]["domain"], issues[0]["issue_id"], True) registry: issue_registry.IssueRegistry = hass.data[issue_registry.DATA_REGISTRY] assert len(registry.issues) == 2 diff --git a/tests/components/resolution_center/test_websocket_api.py b/tests/components/resolution_center/test_websocket_api.py index 8701996a535..044b0e9832b 100644 --- a/tests/components/resolution_center/test_websocket_api.py +++ b/tests/components/resolution_center/test_websocket_api.py @@ -58,8 +58,8 @@ async def create_issues(hass, ws_client): dict( issue, created=ANY, - dismissed=False, dismissed_version=None, + ignored=False, ) for issue in issues ] @@ -120,9 +120,10 @@ async def test_dismiss_issue(hass: HomeAssistant, hass_ws_client) -> None: await client.send_json( { "id": 2, - "type": "resolution_center/dismiss_issue", + "type": "resolution_center/ignore_issue", "domain": "fake_integration", "issue_id": "no_such_issue", + "ignore": True, } ) msg = await client.receive_json() @@ -131,9 +132,10 @@ async def test_dismiss_issue(hass: HomeAssistant, hass_ws_client) -> None: await client.send_json( { "id": 3, - "type": "resolution_center/dismiss_issue", + "type": "resolution_center/ignore_issue", "domain": "fake_integration", "issue_id": "issue_1", + "ignore": True, } ) msg = await client.receive_json() @@ -149,8 +151,37 @@ async def test_dismiss_issue(hass: HomeAssistant, hass_ws_client) -> None: dict( issue, created=ANY, - dismissed=True, dismissed_version=ha_version, + ignored=True, + ) + for issue in issues + ] + } + + await client.send_json( + { + "id": 5, + "type": "resolution_center/ignore_issue", + "domain": "fake_integration", + "issue_id": "issue_1", + "ignore": False, + } + ) + msg = await client.receive_json() + assert msg["success"] + assert msg["result"] is None + + await client.send_json({"id": 6, "type": "resolution_center/list_issues"}) + msg = await client.receive_json() + + assert msg["success"] + assert msg["result"] == { + "issues": [ + dict( + issue, + created=ANY, + dismissed_version=None, + ignored=False, ) for issue in issues ] @@ -192,8 +223,8 @@ async def test_fix_non_existing_issue( dict( issue, created=ANY, - dismissed=False, dismissed_version=None, + ignored=False, ) for issue in issues ] @@ -395,8 +426,8 @@ async def test_list_issues(hass: HomeAssistant, hass_ws_client) -> None: dict( issue, created="2022-07-19T07:53:05+00:00", - dismissed=False, dismissed_version=None, + ignored=False, ) for issue in issues ]