diff --git a/homeassistant/components/persistent_notification/__init__.py b/homeassistant/components/persistent_notification/__init__.py index 960d0a5ca598f3..581720c2730346 100644 --- a/homeassistant/components/persistent_notification/__init__.py +++ b/homeassistant/components/persistent_notification/__init__.py @@ -140,6 +140,20 @@ def async_dismiss(hass: HomeAssistant, notification_id: str) -> None: ) +@callback +def async_dismiss_all(hass: HomeAssistant) -> None: + """Remove all notifications.""" + notifications = _async_get_or_create_notifications(hass) + notifications_copy = notifications.copy() + notifications.clear() + async_dispatcher_send( + hass, + SIGNAL_PERSISTENT_NOTIFICATIONS_UPDATED, + UpdateType.REMOVED, + notifications_copy, + ) + + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the persistent notification component.""" @@ -158,6 +172,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 +194,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..5ebd7e34409ad1 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. diff --git a/tests/components/persistent_notification/test_init.py b/tests/components/persistent_notification/test_init.py index 71a0fcae917483..921f4b120450a0 100644 --- a/tests/components/persistent_notification/test_init.py +++ b/tests/components/persistent_notification/test_init.py @@ -54,11 +54,31 @@ 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 + + async def test_ws_get_notifications( hass: HomeAssistant, hass_ws_client: WebSocketGenerator ) -> None: @@ -169,3 +189,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