2019-04-03 15:40:03 +00:00
|
|
|
"""Provides functionality to notify people."""
|
2021-03-18 12:21:46 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-04-12 06:51:51 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-10-21 15:12:36 +00:00
|
|
|
import homeassistant.components.persistent_notification as pn
|
2021-10-21 23:09:08 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_PLATFORM
|
|
|
|
from homeassistant.core import HomeAssistant, ServiceCall
|
2019-12-09 13:46:24 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-10-21 23:09:08 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
|
|
|
from .const import ( # noqa: F401
|
|
|
|
ATTR_DATA,
|
|
|
|
ATTR_MESSAGE,
|
|
|
|
ATTR_TARGET,
|
|
|
|
ATTR_TITLE,
|
|
|
|
DOMAIN,
|
|
|
|
NOTIFY_SERVICE_SCHEMA,
|
|
|
|
PERSISTENT_NOTIFICATION_SERVICE_SCHEMA,
|
|
|
|
SERVICE_NOTIFY,
|
|
|
|
SERVICE_PERSISTENT_NOTIFICATION,
|
|
|
|
)
|
|
|
|
from .legacy import ( # noqa: F401
|
|
|
|
BaseNotificationService,
|
|
|
|
async_reload,
|
|
|
|
async_reset_platform,
|
|
|
|
async_setup_legacy,
|
|
|
|
check_templates_warn,
|
|
|
|
)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2016-11-28 19:50:42 +00:00
|
|
|
# Platform specific data
|
|
|
|
ATTR_TITLE_DEFAULT = "Home Assistant"
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = vol.Schema(
|
|
|
|
{vol.Required(CONF_PLATFORM): cv.string, vol.Optional(CONF_NAME): cv.string},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2016-08-14 08:10:07 +00:00
|
|
|
|
2020-08-28 17:18:02 +00:00
|
|
|
|
2021-10-21 23:09:08 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the notify services."""
|
2021-10-21 23:09:08 +00:00
|
|
|
await async_setup_legacy(hass, config)
|
2016-08-17 05:05:41 +00:00
|
|
|
|
2020-10-21 15:12:36 +00:00
|
|
|
async def persistent_notification(service: ServiceCall) -> None:
|
|
|
|
"""Send notification via the built-in persistsent_notify integration."""
|
|
|
|
message = service.data[ATTR_MESSAGE]
|
|
|
|
message.hass = hass
|
2021-10-21 23:09:08 +00:00
|
|
|
check_templates_warn(hass, message)
|
2020-10-21 15:12:36 +00:00
|
|
|
|
2021-10-07 10:58:00 +00:00
|
|
|
title = None
|
|
|
|
if title_tpl := service.data.get(ATTR_TITLE):
|
2021-10-21 23:09:08 +00:00
|
|
|
check_templates_warn(hass, title_tpl)
|
2021-10-07 10:58:00 +00:00
|
|
|
title_tpl.hass = hass
|
|
|
|
title = title_tpl.async_render(parse_result=False)
|
2020-10-21 15:12:36 +00:00
|
|
|
|
2021-10-07 10:58:00 +00:00
|
|
|
pn.async_create(hass, message.async_render(parse_result=False), title)
|
2020-10-21 15:12:36 +00:00
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_PERSISTENT_NOTIFICATION,
|
|
|
|
persistent_notification,
|
|
|
|
schema=PERSISTENT_NOTIFICATION_SERVICE_SCHEMA,
|
|
|
|
)
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
return True
|