mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
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>
This commit is contained in:
parent
85d6e03dd3
commit
d700415045
@ -19,7 +19,6 @@ from .const import ( # noqa: F401
|
|||||||
ATTR_TITLE,
|
ATTR_TITLE,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
NOTIFY_SERVICE_SCHEMA,
|
NOTIFY_SERVICE_SCHEMA,
|
||||||
PERSISTENT_NOTIFICATION_SERVICE_SCHEMA,
|
|
||||||
SERVICE_NOTIFY,
|
SERVICE_NOTIFY,
|
||||||
SERVICE_PERSISTENT_NOTIFICATION,
|
SERVICE_PERSISTENT_NOTIFICATION,
|
||||||
)
|
)
|
||||||
@ -70,13 +69,19 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
title_tpl.hass = hass
|
title_tpl.hass = hass
|
||||||
title = title_tpl.async_render(parse_result=False)
|
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(
|
hass.services.async_register(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_PERSISTENT_NOTIFICATION,
|
SERVICE_PERSISTENT_NOTIFICATION,
|
||||||
persistent_notification,
|
persistent_notification,
|
||||||
schema=PERSISTENT_NOTIFICATION_SERVICE_SCHEMA,
|
schema=NOTIFY_SERVICE_SCHEMA,
|
||||||
)
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -31,10 +31,3 @@ NOTIFY_SERVICE_SCHEMA = vol.Schema(
|
|||||||
vol.Optional(ATTR_DATA): dict,
|
vol.Optional(ATTR_DATA): dict,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
PERSISTENT_NOTIFICATION_SERVICE_SCHEMA = vol.Schema(
|
|
||||||
{
|
|
||||||
vol.Required(ATTR_MESSAGE): cv.template,
|
|
||||||
vol.Optional(ATTR_TITLE): cv.template,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
@ -51,3 +51,11 @@ persistent_notification:
|
|||||||
example: "Your Garage Door Friend"
|
example: "Your Garage Door Friend"
|
||||||
selector:
|
selector:
|
||||||
text:
|
text:
|
||||||
|
data:
|
||||||
|
name: Data
|
||||||
|
description:
|
||||||
|
Extended information for notification. Optional depending on the
|
||||||
|
platform.
|
||||||
|
example: platform specific
|
||||||
|
selector:
|
||||||
|
object:
|
||||||
|
@ -25,3 +25,42 @@ async def test_async_send_message(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
assert notification["message"] == "Hello"
|
assert notification["message"] == "Hello"
|
||||||
assert notification["title"] == "Test notification"
|
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"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user