Support notification_id in notify.persistent_notification (#74822)

* Support notification_id in notify.persistent_notification

* Apply suggestions from code review

Co-authored-by: Scott Giminiani <ScottG489@Gmail.com>

---------

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
Co-authored-by: Scott Giminiani <ScottG489@Gmail.com>
pull/90897/head
Kevin Cathcart 2023-06-25 22:25:58 -04:00 committed by GitHub
parent 85d6e03dd3
commit d700415045
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 10 deletions

View File

@ -19,7 +19,6 @@ from .const import ( # noqa: F401
ATTR_TITLE,
DOMAIN,
NOTIFY_SERVICE_SCHEMA,
PERSISTENT_NOTIFICATION_SERVICE_SCHEMA,
SERVICE_NOTIFY,
SERVICE_PERSISTENT_NOTIFICATION,
)
@ -70,13 +69,19 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
title_tpl.hass = hass
title = title_tpl.async_render(parse_result=False)
pn.async_create(hass, message.async_render(parse_result=False), title)
notification_id = None
if data := service.data.get(ATTR_DATA):
notification_id = data.get(pn.ATTR_NOTIFICATION_ID)
pn.async_create(
hass, message.async_render(parse_result=False), title, notification_id
)
hass.services.async_register(
DOMAIN,
SERVICE_PERSISTENT_NOTIFICATION,
persistent_notification,
schema=PERSISTENT_NOTIFICATION_SERVICE_SCHEMA,
schema=NOTIFY_SERVICE_SCHEMA,
)
return True

View File

@ -31,10 +31,3 @@ NOTIFY_SERVICE_SCHEMA = vol.Schema(
vol.Optional(ATTR_DATA): dict,
}
)
PERSISTENT_NOTIFICATION_SERVICE_SCHEMA = vol.Schema(
{
vol.Required(ATTR_MESSAGE): cv.template,
vol.Optional(ATTR_TITLE): cv.template,
}
)

View File

@ -51,3 +51,11 @@ persistent_notification:
example: "Your Garage Door Friend"
selector:
text:
data:
name: Data
description:
Extended information for notification. Optional depending on the
platform.
example: platform specific
selector:
object:

View File

@ -25,3 +25,42 @@ async def test_async_send_message(hass: HomeAssistant) -> None:
assert notification["message"] == "Hello"
assert notification["title"] == "Test notification"
async def test_async_supports_notification_id(hass: HomeAssistant) -> None:
"""Test that notify.persistent_notification supports notification_id."""
await async_setup_component(hass, pn.DOMAIN, {"core": {}})
await async_setup_component(hass, notify.DOMAIN, {})
await hass.async_block_till_done()
message = {
"message": "Hello",
"title": "Test notification",
"data": {"notification_id": "my_id"},
}
await hass.services.async_call(
notify.DOMAIN, notify.SERVICE_PERSISTENT_NOTIFICATION, message
)
await hass.async_block_till_done()
notifications = async_get_persistent_notifications(hass)
assert len(notifications) == 1
# Send second message with same ID
message = {
"message": "Goodbye",
"title": "Notification was updated",
"data": {"notification_id": "my_id"},
}
await hass.services.async_call(
notify.DOMAIN, notify.SERVICE_PERSISTENT_NOTIFICATION, message
)
await hass.async_block_till_done()
notifications = async_get_persistent_notifications(hass)
assert len(notifications) == 1
notification = notifications[list(notifications)[0]]
assert notification["message"] == "Goodbye"
assert notification["title"] == "Notification was updated"