Add persistent_notification service to the notify platform (#40638)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
parent
df2ede6522
commit
754ba18af0
3 changed files with 67 additions and 0 deletions
|
@ -6,6 +6,7 @@ from typing import Any, Dict, Optional
|
|||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.components.persistent_notification as pn
|
||||
from homeassistant.const import CONF_NAME, CONF_PLATFORM
|
||||
from homeassistant.core import ServiceCall
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
|
@ -36,6 +37,7 @@ ATTR_TITLE_DEFAULT = "Home Assistant"
|
|||
DOMAIN = "notify"
|
||||
|
||||
SERVICE_NOTIFY = "notify"
|
||||
SERVICE_PERSISTENT_NOTIFICATION = "persistent_notification"
|
||||
|
||||
NOTIFY_SERVICES = "notify_services"
|
||||
|
||||
|
@ -53,6 +55,13 @@ NOTIFY_SERVICE_SCHEMA = vol.Schema(
|
|||
}
|
||||
)
|
||||
|
||||
PERSISTENT_NOTIFICATION_SERVICE_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(ATTR_MESSAGE): cv.template,
|
||||
vol.Optional(ATTR_TITLE): cv.template,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@bind_hass
|
||||
async def async_reload(hass: HomeAssistantType, integration_name: str) -> None:
|
||||
|
@ -215,6 +224,22 @@ async def async_setup(hass, config):
|
|||
"""Set up the notify services."""
|
||||
hass.data.setdefault(NOTIFY_SERVICES, {})
|
||||
|
||||
async def persistent_notification(service: ServiceCall) -> None:
|
||||
"""Send notification via the built-in persistsent_notify integration."""
|
||||
payload = {}
|
||||
message = service.data[ATTR_MESSAGE]
|
||||
message.hass = hass
|
||||
payload[ATTR_MESSAGE] = message.async_render()
|
||||
|
||||
title = service.data.get(ATTR_TITLE)
|
||||
if title:
|
||||
title.hass = hass
|
||||
payload[ATTR_TITLE] = title.async_render()
|
||||
|
||||
await hass.services.async_call(
|
||||
pn.DOMAIN, pn.SERVICE_CREATE, payload, blocking=True
|
||||
)
|
||||
|
||||
async def async_setup_platform(
|
||||
integration_name, p_config=None, discovery_info=None
|
||||
):
|
||||
|
@ -274,6 +299,13 @@ async def async_setup(hass, config):
|
|||
|
||||
return True
|
||||
|
||||
hass.services.async_register(
|
||||
DOMAIN,
|
||||
SERVICE_PERSISTENT_NOTIFICATION,
|
||||
persistent_notification,
|
||||
schema=PERSISTENT_NOTIFICATION_SERVICE_SCHEMA,
|
||||
)
|
||||
|
||||
setup_tasks = [
|
||||
async_setup_platform(integration_name, p_config)
|
||||
for integration_name, p_config in config_per_platform(config, DOMAIN)
|
||||
|
|
|
@ -16,6 +16,16 @@ notify:
|
|||
description: Extended information for notification. Optional depending on the platform.
|
||||
example: platform specific
|
||||
|
||||
persistent_notification:
|
||||
description: Sends a notification to the visible in the front-end.
|
||||
fields:
|
||||
message:
|
||||
description: Message body of the notification.
|
||||
example: The garage door has been open for 10 minutes.
|
||||
title:
|
||||
description: Optional title for your notification.
|
||||
example: "Your Garage Door Friend"
|
||||
|
||||
apns_register:
|
||||
description: Registers a device to receive push notifications.
|
||||
fields:
|
||||
|
|
25
tests/components/notify/test_persistent_notification.py
Normal file
25
tests/components/notify/test_persistent_notification.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
"""The tests for the notify.persistent_notification service."""
|
||||
from homeassistant.components import notify
|
||||
import homeassistant.components.persistent_notification as pn
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
async def test_async_send_message(hass: HomeAssistant):
|
||||
"""Test sending a message to notify.persistent_notification service."""
|
||||
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"}
|
||||
await hass.services.async_call(
|
||||
notify.DOMAIN, notify.SERVICE_PERSISTENT_NOTIFICATION, message
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_ids = hass.states.async_entity_ids(pn.DOMAIN)
|
||||
assert len(entity_ids) == 1
|
||||
|
||||
state = hass.states.get(entity_ids[0])
|
||||
assert state.attributes.get("message") == "Hello"
|
||||
assert state.attributes.get("title") == "Test notification"
|
Loading…
Add table
Reference in a new issue