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

Actionable Persistent Notifications #121689

Closed
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
8 changes: 7 additions & 1 deletion homeassistant/components/notify/__init__.py
Original file line number Diff line number Diff line change
@@ -102,11 +102,17 @@ async def persistent_notification(service: ServiceCall) -> None:
title = title_tpl.async_render(parse_result=False)

notification_id = None
actions = None
if data := service.data.get(ATTR_DATA):
notification_id = data.get(pn.ATTR_NOTIFICATION_ID)
actions = data.get(pn.ATTR_ACTIONS)

pn.async_create(
hass, message.async_render(parse_result=False), title, notification_id
hass,
message.async_render(parse_result=False),
title,
notification_id,
actions,
)

hass.services.async_register(
27 changes: 26 additions & 1 deletion homeassistant/components/persistent_notification/__init__.py
Original file line number Diff line number Diff line change
@@ -30,7 +30,16 @@
ATTR_MESSAGE: Final = "message"
ATTR_NOTIFICATION_ID: Final = "notification_id"
ATTR_TITLE: Final = "title"
ATTR_ACTION: Final = "action"
ATTR_STATUS: Final = "status"
ATTR_ACTIONS: Final = "actions"


class NotificationAction(TypedDict):
"""Action for a persistent notification."""

action: str
title: str


class Notification(TypedDict):
@@ -40,6 +49,7 @@ class Notification(TypedDict):
message: str
notification_id: str
title: str | None
actions: list[NotificationAction] | None


class UpdateType(StrEnum):
@@ -81,9 +91,10 @@ def create(
message: str,
title: str | None = None,
notification_id: str | None = None,
actions: list[NotificationAction] | None = None,
) -> None:
"""Generate a notification."""
hass.add_job(async_create, hass, message, title, notification_id)
hass.add_job(async_create, hass, message, title, notification_id, actions)


@bind_hass
@@ -99,6 +110,7 @@ def async_create(
message: str,
title: str | None = None,
notification_id: str | None = None,
actions: list[NotificationAction] | None = None,
) -> None:
"""Generate a notification."""
notifications = _async_get_or_create_notifications(hass)
@@ -108,6 +120,7 @@ def async_create(
ATTR_MESSAGE: message,
ATTR_NOTIFICATION_ID: notification_id,
ATTR_TITLE: title,
ATTR_ACTIONS: actions,
ATTR_CREATED_AT: dt_util.utcnow(),
}

@@ -166,6 +179,7 @@ def create_service(call: ServiceCall) -> None:
call.data[ATTR_MESSAGE],
call.data.get(ATTR_TITLE),
call.data.get(ATTR_NOTIFICATION_ID),
call.data.get(ATTR_ACTIONS),
)

@callback
@@ -187,6 +201,17 @@ def dismiss_all_service(call: ServiceCall) -> None:
vol.Required(ATTR_MESSAGE): vol.Any(cv.dynamic_template, cv.string),
vol.Optional(ATTR_TITLE): vol.Any(cv.dynamic_template, cv.string),
vol.Optional(ATTR_NOTIFICATION_ID): cv.string,
vol.Optional(ATTR_ACTIONS): vol.All(
cv.ensure_list,
[
vol.Schema(
{
vol.Required(ATTR_ACTION): cv.string,
vol.Required(ATTR_TITLE): cv.string,
}
)
],
),
}
),
)
Original file line number Diff line number Diff line change
@@ -9,6 +9,9 @@ create:
example: Test notification
selector:
text:
actions:
selector:
object:
notification_id:
example: 1234
selector:
Original file line number Diff line number Diff line change
@@ -13,6 +13,10 @@
"name": "Title",
"description": "Optional title of the notification."
},
"actions": {
"name": "Actions",
"description": "Optional actions to be presented to the user with the notification."
},
"notification_id": {
"name": "Notification ID",
"description": "ID of the notification. This new notification will overwrite an existing notification with the same ID."
Loading