Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add persistent_notification.dismiss_all service call #95004

Merged
merged 5 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions homeassistant/components/persistent_notification/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -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",
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ dismiss:
example: 1234
selector:
text:

dismiss_all:
name: Dismiss All
description: Remove all notifications.
51 changes: 51 additions & 0 deletions tests/components/persistent_notification/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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