2019-04-03 15:40:03 +00:00
|
|
|
"""Provides functionality to notify people."""
|
2017-01-18 06:08:03 +00:00
|
|
|
import asyncio
|
2016-11-28 19:50:42 +00:00
|
|
|
from functools import partial
|
2019-12-09 13:46:24 +00:00
|
|
|
import logging
|
2019-08-12 03:38:18 +00:00
|
|
|
from typing import Optional
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2016-04-12 06:51:51 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-08-14 08:10:07 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_PLATFORM
|
2019-12-09 13:46:24 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2017-01-15 02:53:14 +00:00
|
|
|
from homeassistant.helpers import config_per_platform, discovery
|
2019-12-09 13:46:24 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-08-12 03:38:18 +00:00
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2019-12-09 13:46:24 +00:00
|
|
|
from homeassistant.setup import async_prepare_setup_platform
|
2016-08-14 08:10:07 +00:00
|
|
|
from homeassistant.util import slugify
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2019-08-12 03:38:18 +00:00
|
|
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
|
|
|
|
2016-11-28 19:50:42 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2016-11-28 19:50:42 +00:00
|
|
|
# Platform specific data
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DATA = "data"
|
2016-11-28 19:50:42 +00:00
|
|
|
|
|
|
|
# Text to notify user of
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_MESSAGE = "message"
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-11-15 17:50:36 +00:00
|
|
|
# Target of the notification (user, device, etc)
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_TARGET = "target"
|
2015-11-15 17:50:36 +00:00
|
|
|
|
2016-11-28 19:50:42 +00:00
|
|
|
# Title of notification
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_TITLE = "title"
|
2016-11-28 19:50:42 +00:00
|
|
|
ATTR_TITLE_DEFAULT = "Home Assistant"
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "notify"
|
2016-04-04 00:54:58 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_NOTIFY = "notify"
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
NOTIFY_SERVICE_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(ATTR_MESSAGE): cv.template,
|
|
|
|
vol.Optional(ATTR_TITLE): cv.template,
|
|
|
|
vol.Optional(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]),
|
|
|
|
vol.Optional(ATTR_DATA): dict,
|
|
|
|
}
|
|
|
|
)
|
2016-04-12 06:51:51 +00:00
|
|
|
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_setup(hass, config):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the notify services."""
|
2016-08-17 05:05:41 +00:00
|
|
|
targets = {}
|
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_setup_platform(p_type, p_config=None, discovery_info=None):
|
2017-01-15 02:53:14 +00:00
|
|
|
"""Set up a notify platform."""
|
|
|
|
if p_config is None:
|
|
|
|
p_config = {}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
platform = await async_prepare_setup_platform(hass, config, DOMAIN, p_type)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2017-01-18 06:08:03 +00:00
|
|
|
if platform is None:
|
2016-11-28 19:50:42 +00:00
|
|
|
_LOGGER.error("Unknown notification service specified")
|
2017-01-18 06:08:03 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
_LOGGER.info("Setting up %s.%s", DOMAIN, p_type)
|
|
|
|
notify_service = None
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
if hasattr(platform, "async_get_service"):
|
|
|
|
notify_service = await platform.async_get_service(
|
|
|
|
hass, p_config, discovery_info
|
|
|
|
)
|
|
|
|
elif hasattr(platform, "get_service"):
|
2018-10-01 06:58:21 +00:00
|
|
|
notify_service = await hass.async_add_job(
|
2019-07-31 19:25:30 +00:00
|
|
|
platform.get_service, hass, p_config, discovery_info
|
|
|
|
)
|
2017-01-18 06:08:03 +00:00
|
|
|
else:
|
|
|
|
raise HomeAssistantError("Invalid notify platform.")
|
|
|
|
|
|
|
|
if notify_service is None:
|
2017-09-07 07:11:55 +00:00
|
|
|
# Platforms can decide not to create a service based
|
|
|
|
# on discovery data.
|
|
|
|
if discovery_info is None:
|
|
|
|
_LOGGER.error(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Failed to initialize notification service %s", p_type
|
|
|
|
)
|
2017-01-18 06:08:03 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
except Exception: # pylint: disable=broad-except
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.exception("Error setting up platform %s", p_type)
|
2017-01-18 06:08:03 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
notify_service.hass = hass
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
discovery_info = {}
|
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_notify_message(service):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Handle sending notification message service calls."""
|
2016-09-10 14:36:55 +00:00
|
|
|
kwargs = {}
|
2017-01-18 06:08:03 +00:00
|
|
|
message = service.data[ATTR_MESSAGE]
|
|
|
|
title = service.data.get(ATTR_TITLE)
|
2016-09-10 14:36:55 +00:00
|
|
|
|
|
|
|
if title:
|
2016-09-28 04:29:55 +00:00
|
|
|
title.hass = hass
|
2017-01-18 06:08:03 +00:00
|
|
|
kwargs[ATTR_TITLE] = title.async_render()
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2017-01-18 06:08:03 +00:00
|
|
|
if targets.get(service.service) is not None:
|
|
|
|
kwargs[ATTR_TARGET] = [targets[service.service]]
|
|
|
|
elif service.data.get(ATTR_TARGET) is not None:
|
|
|
|
kwargs[ATTR_TARGET] = service.data.get(ATTR_TARGET)
|
2016-09-10 14:36:55 +00:00
|
|
|
|
2016-09-28 04:29:55 +00:00
|
|
|
message.hass = hass
|
2017-01-18 06:08:03 +00:00
|
|
|
kwargs[ATTR_MESSAGE] = message.async_render()
|
|
|
|
kwargs[ATTR_DATA] = service.data.get(ATTR_DATA)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
await notify_service.async_send_message(**kwargs)
|
2016-08-17 05:05:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if hasattr(notify_service, "targets"):
|
2017-01-15 02:53:14 +00:00
|
|
|
platform_name = (
|
2019-07-31 19:25:30 +00:00
|
|
|
p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or p_type
|
|
|
|
)
|
2016-09-25 16:41:11 +00:00
|
|
|
for name, target in notify_service.targets.items():
|
2019-09-03 18:35:00 +00:00
|
|
|
target_name = slugify(f"{platform_name}_{name}")
|
2016-08-17 05:05:41 +00:00
|
|
|
targets[target_name] = target
|
2017-01-18 06:08:03 +00:00
|
|
|
hass.services.async_register(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
target_name,
|
|
|
|
async_notify_message,
|
|
|
|
schema=NOTIFY_SERVICE_SCHEMA,
|
|
|
|
)
|
2016-08-17 05:05:41 +00:00
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
platform_name = (
|
2019-07-31 19:25:30 +00:00
|
|
|
p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or SERVICE_NOTIFY
|
|
|
|
)
|
2016-08-17 05:05:41 +00:00
|
|
|
platform_name_slug = slugify(platform_name)
|
|
|
|
|
2017-01-18 06:08:03 +00:00
|
|
|
hass.services.async_register(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
platform_name_slug,
|
|
|
|
async_notify_message,
|
|
|
|
schema=NOTIFY_SERVICE_SCHEMA,
|
|
|
|
)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2019-09-03 18:35:00 +00:00
|
|
|
hass.config.components.add(f"{DOMAIN}.{p_type}")
|
2018-08-19 16:56:31 +00:00
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
return True
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
setup_tasks = [
|
|
|
|
async_setup_platform(p_type, p_config)
|
|
|
|
for p_type, p_config in config_per_platform(config, DOMAIN)
|
|
|
|
]
|
2017-01-18 06:08:03 +00:00
|
|
|
|
|
|
|
if setup_tasks:
|
2019-05-23 04:09:59 +00:00
|
|
|
await asyncio.wait(setup_tasks)
|
2017-01-15 02:53:14 +00:00
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_platform_discovered(platform, info):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Handle for discovered platform."""
|
2018-10-01 06:58:21 +00:00
|
|
|
await async_setup_platform(platform, discovery_info=info)
|
2017-01-15 02:53:14 +00:00
|
|
|
|
2017-01-18 06:08:03 +00:00
|
|
|
discovery.async_listen_platform(hass, DOMAIN, async_platform_discovered)
|
2017-01-15 02:53:14 +00:00
|
|
|
|
|
|
|
return True
|
2015-01-04 09:01:49 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class BaseNotificationService:
|
2016-03-08 10:46:32 +00:00
|
|
|
"""An abstract class for notification services."""
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2019-08-12 03:38:18 +00:00
|
|
|
hass: Optional[HomeAssistantType] = None
|
2017-01-18 06:08:03 +00:00
|
|
|
|
2015-01-04 09:01:49 +00:00
|
|
|
def send_message(self, message, **kwargs):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Send a message.
|
|
|
|
|
2015-01-04 09:01:49 +00:00
|
|
|
kwargs can contain ATTR_TITLE to specify a title.
|
|
|
|
"""
|
2017-01-18 06:08:03 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def async_send_message(self, message, **kwargs):
|
|
|
|
"""Send a message.
|
|
|
|
|
|
|
|
kwargs can contain ATTR_TITLE to specify a title.
|
|
|
|
This method must be run in the event loop and returns a coroutine.
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.hass.async_add_job(partial(self.send_message, message, **kwargs))
|