From d700415045d9fe1a6dd06c5265c0fc9eea258af3 Mon Sep 17 00:00:00 2001 From: Kevin Cathcart Date: Sun, 25 Jun 2023 22:25:58 -0400 Subject: [PATCH] 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 --------- Co-authored-by: Paulus Schoutsen Co-authored-by: Scott Giminiani --- homeassistant/components/notify/__init__.py | 11 ++++-- homeassistant/components/notify/const.py | 7 ---- homeassistant/components/notify/services.yaml | 8 ++++ .../notify/test_persistent_notification.py | 39 +++++++++++++++++++ 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index eae47b55179..e9e61527884 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -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 diff --git a/homeassistant/components/notify/const.py b/homeassistant/components/notify/const.py index d30702915d9..38dba680635 100644 --- a/homeassistant/components/notify/const.py +++ b/homeassistant/components/notify/const.py @@ -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, - } -) diff --git a/homeassistant/components/notify/services.yaml b/homeassistant/components/notify/services.yaml index 1a0de7344a3..9311acf2ba9 100644 --- a/homeassistant/components/notify/services.yaml +++ b/homeassistant/components/notify/services.yaml @@ -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: diff --git a/tests/components/notify/test_persistent_notification.py b/tests/components/notify/test_persistent_notification.py index 6f273ac3d9b..580b2bdb614 100644 --- a/tests/components/notify/test_persistent_notification.py +++ b/tests/components/notify/test_persistent_notification.py @@ -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"