diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index 51d5e348d5e..016c1aa9e89 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -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) diff --git a/homeassistant/components/notify/services.yaml b/homeassistant/components/notify/services.yaml index e80c7c85677..8c75c94e34a 100644 --- a/homeassistant/components/notify/services.yaml +++ b/homeassistant/components/notify/services.yaml @@ -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: diff --git a/tests/components/notify/test_persistent_notification.py b/tests/components/notify/test_persistent_notification.py new file mode 100644 index 00000000000..957e354ff75 --- /dev/null +++ b/tests/components/notify/test_persistent_notification.py @@ -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"