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
|
2020-08-28 17:18:02 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
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
|
|
|
|
2020-08-28 17:18:02 +00:00
|
|
|
NOTIFY_SERVICES = "notify_services"
|
|
|
|
SERVICE = "service"
|
|
|
|
TARGETS = "targets"
|
|
|
|
FRIENDLY_NAME = "friendly_name"
|
|
|
|
TARGET_FRIENDLY_NAME = "target_friendly_name"
|
|
|
|
|
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
|
|
|
|
2020-08-28 17:18:02 +00:00
|
|
|
@bind_hass
|
|
|
|
async def async_reload(hass, integration_name):
|
|
|
|
"""Register notify services for an integration."""
|
2020-08-28 17:37:19 +00:00
|
|
|
if (
|
|
|
|
NOTIFY_SERVICES not in hass.data
|
|
|
|
or integration_name not in hass.data[NOTIFY_SERVICES]
|
|
|
|
):
|
2020-08-28 17:18:02 +00:00
|
|
|
return
|
|
|
|
|
2020-08-29 20:23:57 +00:00
|
|
|
tasks = [
|
|
|
|
_async_setup_notify_services(hass, data)
|
|
|
|
for data in hass.data[NOTIFY_SERVICES][integration_name]
|
|
|
|
]
|
|
|
|
await asyncio.gather(*tasks)
|
2020-08-28 19:08:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def _async_setup_notify_services(hass, data):
|
|
|
|
"""Create or remove the notify services."""
|
2020-08-28 17:18:02 +00:00
|
|
|
notify_service = data[SERVICE]
|
|
|
|
friendly_name = data[FRIENDLY_NAME]
|
|
|
|
targets = data[TARGETS]
|
|
|
|
|
|
|
|
async def _async_notify_message(service):
|
|
|
|
"""Handle sending notification message service calls."""
|
|
|
|
await _async_notify_message_service(hass, service, notify_service, targets)
|
|
|
|
|
|
|
|
if hasattr(notify_service, "targets"):
|
|
|
|
target_friendly_name = data[TARGET_FRIENDLY_NAME]
|
2020-08-28 17:37:19 +00:00
|
|
|
stale_targets = set(targets)
|
|
|
|
|
2020-08-28 17:18:02 +00:00
|
|
|
for name, target in notify_service.targets.items():
|
|
|
|
target_name = slugify(f"{target_friendly_name}_{name}")
|
2020-08-28 17:37:19 +00:00
|
|
|
if target_name in stale_targets:
|
|
|
|
stale_targets.remove(target_name)
|
2020-08-28 17:18:02 +00:00
|
|
|
if target_name in targets:
|
|
|
|
continue
|
|
|
|
targets[target_name] = target
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN,
|
|
|
|
target_name,
|
|
|
|
_async_notify_message,
|
|
|
|
schema=NOTIFY_SERVICE_SCHEMA,
|
|
|
|
)
|
|
|
|
|
2020-08-28 17:37:19 +00:00
|
|
|
for stale_target_name in stale_targets:
|
2020-08-28 19:08:09 +00:00
|
|
|
del targets[stale_target_name]
|
2020-08-28 17:37:19 +00:00
|
|
|
hass.services.async_remove(
|
|
|
|
DOMAIN,
|
|
|
|
stale_target_name,
|
|
|
|
)
|
|
|
|
|
2020-08-28 17:18:02 +00:00
|
|
|
friendly_name_slug = slugify(friendly_name)
|
|
|
|
if hass.services.has_service(DOMAIN, friendly_name_slug):
|
|
|
|
return
|
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN,
|
|
|
|
friendly_name_slug,
|
|
|
|
_async_notify_message,
|
|
|
|
schema=NOTIFY_SERVICE_SCHEMA,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def _async_notify_message_service(hass, service, notify_service, targets):
|
|
|
|
"""Handle sending notification message service calls."""
|
|
|
|
kwargs = {}
|
|
|
|
message = service.data[ATTR_MESSAGE]
|
|
|
|
title = service.data.get(ATTR_TITLE)
|
|
|
|
|
|
|
|
if title:
|
|
|
|
title.hass = hass
|
|
|
|
kwargs[ATTR_TITLE] = title.async_render()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
message.hass = hass
|
|
|
|
kwargs[ATTR_MESSAGE] = message.async_render()
|
|
|
|
kwargs[ATTR_DATA] = service.data.get(ATTR_DATA)
|
|
|
|
|
|
|
|
await notify_service.async_send_message(**kwargs)
|
|
|
|
|
|
|
|
|
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."""
|
2020-08-28 17:18:02 +00:00
|
|
|
hass.data.setdefault(NOTIFY_SERVICES, {})
|
2016-08-17 05:05:41 +00:00
|
|
|
|
2020-08-28 17:18:02 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
integration_name, 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 = {}
|
|
|
|
|
2020-08-28 17:18:02 +00:00
|
|
|
platform = await async_prepare_setup_platform(
|
|
|
|
hass, config, DOMAIN, integration_name
|
|
|
|
)
|
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
|
|
|
|
|
2020-08-28 17:18:02 +00:00
|
|
|
_LOGGER.info("Setting up %s.%s", DOMAIN, integration_name)
|
2017-01-18 06:08:03 +00:00
|
|
|
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(
|
2020-08-28 17:18:02 +00:00
|
|
|
"Failed to initialize notification service %s", integration_name
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-01-18 06:08:03 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
except Exception: # pylint: disable=broad-except
|
2020-08-28 17:18:02 +00:00
|
|
|
_LOGGER.exception("Error setting up platform %s", integration_name)
|
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 = {}
|
|
|
|
|
2020-08-28 17:18:02 +00:00
|
|
|
target_friendly_name = (
|
|
|
|
p_config.get(CONF_NAME) or discovery_info.get(CONF_NAME) or integration_name
|
|
|
|
)
|
|
|
|
friendly_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
|
|
|
|
2020-08-28 19:08:09 +00:00
|
|
|
data = {
|
2020-08-28 17:18:02 +00:00
|
|
|
FRIENDLY_NAME: friendly_name,
|
|
|
|
# The targets use a slightly different friendly name
|
|
|
|
# selection pattern than the base service
|
|
|
|
TARGET_FRIENDLY_NAME: target_friendly_name,
|
|
|
|
SERVICE: notify_service,
|
|
|
|
TARGETS: {},
|
|
|
|
}
|
2020-08-28 19:08:09 +00:00
|
|
|
hass.data[NOTIFY_SERVICES].setdefault(integration_name, [])
|
|
|
|
hass.data[NOTIFY_SERVICES][integration_name].append(data)
|
2020-08-28 17:18:02 +00:00
|
|
|
|
2020-08-28 19:08:09 +00:00
|
|
|
await _async_setup_notify_services(hass, data)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2020-08-28 17:18:02 +00:00
|
|
|
hass.config.components.add(f"{DOMAIN}.{integration_name}")
|
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 = [
|
2020-08-28 17:18:02 +00:00
|
|
|
async_setup_platform(integration_name, p_config)
|
|
|
|
for integration_name, p_config in config_per_platform(config, DOMAIN)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
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()
|
|
|
|
|
2020-01-29 21:59:45 +00:00
|
|
|
async def async_send_message(self, message, **kwargs):
|
2017-01-18 06:08:03 +00:00
|
|
|
"""Send a message.
|
|
|
|
|
|
|
|
kwargs can contain ATTR_TITLE to specify a title.
|
|
|
|
"""
|
2020-01-29 21:59:45 +00:00
|
|
|
await self.hass.async_add_job(partial(self.send_message, message, **kwargs))
|