From 08eb3e420165028da8a1802975378d4a10e84f80 Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Wed, 21 Jun 2023 17:08:57 +0000 Subject: [PATCH 1/5] add persistent_notification.dismiss_all --- .../persistent_notification/__init__.py | 16 ++++++ .../persistent_notification/services.yaml | 4 ++ .../persistent_notification/test_init.py | 53 +++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/homeassistant/components/persistent_notification/__init__.py b/homeassistant/components/persistent_notification/__init__.py index 960d0a5ca598f3..fe1b607a0f201b 100644 --- a/homeassistant/components/persistent_notification/__init__.py +++ b/homeassistant/components/persistent_notification/__init__.py @@ -140,6 +140,15 @@ def async_dismiss(hass: HomeAssistant, notification_id: str) -> None: ) +@callback +@bind_hass +def async_dismiss_all(hass: HomeAssistant) -> None: + """Remove all notifications.""" + notifications = _async_get_or_create_notifications(hass) + for notification_id in list(notifications): + async_dismiss(hass, notification_id) + + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the persistent notification component.""" @@ -158,6 +167,11 @@ def dismiss_service(call: ServiceCall) -> None: """Handle the dismiss notification service call.""" async_dismiss(hass, call.data[ATTR_NOTIFICATION_ID]) + @callback + def dismiss_all_service(call: ServiceCall) -> None: + """Handle the dismiss all notification service call.""" + async_dismiss_all(hass) + hass.services.async_register( DOMAIN, "create", @@ -175,6 +189,8 @@ def dismiss_service(call: ServiceCall) -> None: DOMAIN, "dismiss", dismiss_service, SCHEMA_SERVICE_NOTIFICATION ) + hass.services.async_register(DOMAIN, "dismiss_all", dismiss_all_service, None) + websocket_api.async_register_command(hass, websocket_get_notifications) websocket_api.async_register_command(hass, websocket_subscribe_notifications) diff --git a/homeassistant/components/persistent_notification/services.yaml b/homeassistant/components/persistent_notification/services.yaml index 60dbf5c864acc2..7f00a42c3805e5 100644 --- a/homeassistant/components/persistent_notification/services.yaml +++ b/homeassistant/components/persistent_notification/services.yaml @@ -33,3 +33,7 @@ dismiss: example: 1234 selector: text: + +dismiss_all: + name: Dismiss All + description: Remove all notifications from the frontend. diff --git a/tests/components/persistent_notification/test_init.py b/tests/components/persistent_notification/test_init.py index 71a0fcae917483..332a0cf724a3e9 100644 --- a/tests/components/persistent_notification/test_init.py +++ b/tests/components/persistent_notification/test_init.py @@ -54,11 +54,33 @@ async def test_dismiss_notification(hass: HomeAssistant) -> None: pn.async_create(hass, "test", notification_id="Beer 2") assert len(notifications) == 1 + pn.async_dismiss(hass, notification_id="Does Not Exist") + + assert len(notifications) == 1 + pn.async_dismiss(hass, notification_id="Beer 2") assert len(notifications) == 0 +async def test_dismiss_all_notifications(hass: HomeAssistant) -> None: + """Ensure removal of all notifications.""" + notifications = pn._async_get_or_create_notifications(hass) + assert len(notifications) == 0 + + pn.async_create(hass, "test", notification_id="Beer 2") + pn.async_create(hass, "test", notification_id="Beer 3") + pn.async_create(hass, "test", notification_id="Beer 4") + pn.async_create(hass, "test", notification_id="Beer 5") + + assert len(notifications) == 4 + pn.async_dismiss_all(hass) + + assert len(notifications) == 0 + + pn.create(hass, "test", notification_id="Beer 2") + + async def test_ws_get_notifications( hass: HomeAssistant, hass_ws_client: WebSocketGenerator ) -> None: @@ -169,3 +191,34 @@ async def test_manual_notification_id_round_trip(hass: HomeAssistant) -> None: ) assert len(notifications) == 0 + + +async def test_manual_dismiss_all(hass: HomeAssistant) -> None: + """Test the dismiss all service.""" + notifications = pn._async_get_or_create_notifications(hass) + assert len(notifications) == 0 + + await hass.services.async_call( + pn.DOMAIN, + "create", + {"notification_id": "Beer 1", "message": "test"}, + blocking=True, + ) + + await hass.services.async_call( + pn.DOMAIN, + "create", + {"notification_id": "Beer 2", "message": "test 2"}, + blocking=True, + ) + + assert len(notifications) == 2 + + await hass.services.async_call( + pn.DOMAIN, + "dismiss_all", + None, + blocking=True, + ) + + assert len(notifications) == 0 From cfb4bd287b2e4b04ad7668d2eccf93905887cafc Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Wed, 21 Jun 2023 17:17:50 +0000 Subject: [PATCH 2/5] remove bind_hass --- homeassistant/components/persistent_notification/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/persistent_notification/__init__.py b/homeassistant/components/persistent_notification/__init__.py index fe1b607a0f201b..4b0ead0c405ff4 100644 --- a/homeassistant/components/persistent_notification/__init__.py +++ b/homeassistant/components/persistent_notification/__init__.py @@ -141,7 +141,6 @@ def async_dismiss(hass: HomeAssistant, notification_id: str) -> None: @callback -@bind_hass def async_dismiss_all(hass: HomeAssistant) -> None: """Remove all notifications.""" notifications = _async_get_or_create_notifications(hass) From 26620bbdeea2e5fb73d8a239538e6dd8fd1e4480 Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Wed, 21 Jun 2023 17:26:44 +0000 Subject: [PATCH 3/5] remove loop --- .../components/persistent_notification/__init__.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/persistent_notification/__init__.py b/homeassistant/components/persistent_notification/__init__.py index 4b0ead0c405ff4..3b06e6e0fec2a7 100644 --- a/homeassistant/components/persistent_notification/__init__.py +++ b/homeassistant/components/persistent_notification/__init__.py @@ -144,8 +144,14 @@ def async_dismiss(hass: HomeAssistant, notification_id: str) -> None: def async_dismiss_all(hass: HomeAssistant) -> None: """Remove all notifications.""" notifications = _async_get_or_create_notifications(hass) - for notification_id in list(notifications): - async_dismiss(hass, notification_id) + copied = notifications.copy() + notifications.clear() + async_dispatcher_send( + hass, + SIGNAL_PERSISTENT_NOTIFICATIONS_UPDATED, + UpdateType.REMOVED, + copied, + ) async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: From 633632d0db444bd8db03b3071f8720c08ee68431 Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Wed, 21 Jun 2023 17:34:56 +0000 Subject: [PATCH 4/5] fixes --- homeassistant/components/persistent_notification/services.yaml | 2 +- tests/components/persistent_notification/test_init.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/homeassistant/components/persistent_notification/services.yaml b/homeassistant/components/persistent_notification/services.yaml index 7f00a42c3805e5..5ebd7e34409ad1 100644 --- a/homeassistant/components/persistent_notification/services.yaml +++ b/homeassistant/components/persistent_notification/services.yaml @@ -36,4 +36,4 @@ dismiss: dismiss_all: name: Dismiss All - description: Remove all notifications from the frontend. + description: Remove all notifications. diff --git a/tests/components/persistent_notification/test_init.py b/tests/components/persistent_notification/test_init.py index 332a0cf724a3e9..921f4b120450a0 100644 --- a/tests/components/persistent_notification/test_init.py +++ b/tests/components/persistent_notification/test_init.py @@ -78,8 +78,6 @@ async def test_dismiss_all_notifications(hass: HomeAssistant) -> None: assert len(notifications) == 0 - pn.create(hass, "test", notification_id="Beer 2") - async def test_ws_get_notifications( hass: HomeAssistant, hass_ws_client: WebSocketGenerator From d828d8b56483dd439e829d8519850241a319ca50 Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Wed, 21 Jun 2023 13:48:24 -0400 Subject: [PATCH 5/5] Update homeassistant/components/persistent_notification/__init__.py Co-authored-by: J. Nick Koston --- homeassistant/components/persistent_notification/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/persistent_notification/__init__.py b/homeassistant/components/persistent_notification/__init__.py index 3b06e6e0fec2a7..581720c2730346 100644 --- a/homeassistant/components/persistent_notification/__init__.py +++ b/homeassistant/components/persistent_notification/__init__.py @@ -144,13 +144,13 @@ def async_dismiss(hass: HomeAssistant, notification_id: str) -> None: def async_dismiss_all(hass: HomeAssistant) -> None: """Remove all notifications.""" notifications = _async_get_or_create_notifications(hass) - copied = notifications.copy() + notifications_copy = notifications.copy() notifications.clear() async_dispatcher_send( hass, SIGNAL_PERSISTENT_NOTIFICATIONS_UPDATED, UpdateType.REMOVED, - copied, + notifications_copy, )