Add persistent_notification.dismiss_all service call (#95004)

This commit is contained in:
Petro31 2023-06-22 08:27:18 -04:00 committed by GitHub
parent 3c86497bc8
commit e4c8a94aaf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 0 deletions

View file

@ -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 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""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 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
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)

View file

@ -33,3 +33,7 @@ dismiss:
example: 1234
selector:
text:
dismiss_all:
name: Dismiss All
description: Remove all notifications.

View file

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