2015-01-04 09:01:49 +00:00
|
|
|
"""
|
|
|
|
Provides functionality to notify people.
|
2015-11-09 17:33:11 +00:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/notify/
|
2015-01-04 09:01:49 +00:00
|
|
|
"""
|
2015-08-19 02:28:40 +00:00
|
|
|
from functools import partial
|
2015-01-04 09:01:49 +00:00
|
|
|
import logging
|
2015-09-27 06:17:04 +00:00
|
|
|
import os
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2016-04-12 06:51:51 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2015-11-15 10:04:57 +00:00
|
|
|
import homeassistant.bootstrap as bootstrap
|
2015-09-27 06:17:04 +00:00
|
|
|
from homeassistant.config import load_yaml_config_file
|
2016-09-28 04:29:55 +00:00
|
|
|
from homeassistant.helpers import config_per_platform
|
2016-04-12 06:51:51 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-08-14 08:10:07 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_PLATFORM
|
|
|
|
from homeassistant.util import slugify
|
2015-01-04 09:01:49 +00:00
|
|
|
|
|
|
|
DOMAIN = "notify"
|
|
|
|
|
|
|
|
# Title of notification
|
|
|
|
ATTR_TITLE = "title"
|
2015-01-04 22:20:16 +00:00
|
|
|
ATTR_TITLE_DEFAULT = "Home Assistant"
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-11-15 17:50:36 +00:00
|
|
|
# Target of the notification (user, device, etc)
|
|
|
|
ATTR_TARGET = 'target'
|
|
|
|
|
2015-01-04 09:01:49 +00:00
|
|
|
# Text to notify user of
|
|
|
|
ATTR_MESSAGE = "message"
|
|
|
|
|
2016-04-04 00:54:58 +00:00
|
|
|
# Platform specific data
|
|
|
|
ATTR_DATA = 'data'
|
|
|
|
|
2015-01-04 09:01:49 +00:00
|
|
|
SERVICE_NOTIFY = "notify"
|
|
|
|
|
2016-08-14 08:10:07 +00:00
|
|
|
PLATFORM_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): cv.string,
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2016-04-12 06:51:51 +00:00
|
|
|
NOTIFY_SERVICE_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(ATTR_MESSAGE): cv.template,
|
2016-09-28 04:29:55 +00:00
|
|
|
vol.Optional(ATTR_TITLE): cv.template,
|
2016-10-02 05:19:17 +00:00
|
|
|
vol.Optional(ATTR_TARGET): vol.All(cv.ensure_list, [cv.string]),
|
2016-07-13 04:48:33 +00:00
|
|
|
vol.Optional(ATTR_DATA): dict,
|
2016-04-12 06:51:51 +00:00
|
|
|
})
|
|
|
|
|
2015-01-04 09:01:49 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-08-10 03:23:57 +00:00
|
|
|
def send_message(hass, message, title=None, data=None):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Send a notification message."""
|
2016-08-10 03:23:57 +00:00
|
|
|
info = {
|
2015-12-10 07:46:50 +00:00
|
|
|
ATTR_MESSAGE: message
|
|
|
|
}
|
|
|
|
|
|
|
|
if title is not None:
|
2016-08-10 03:23:57 +00:00
|
|
|
info[ATTR_TITLE] = title
|
2015-12-10 07:46:50 +00:00
|
|
|
|
2016-08-10 03:23:57 +00:00
|
|
|
if data is not None:
|
|
|
|
info[ATTR_DATA] = data
|
|
|
|
|
|
|
|
hass.services.call(DOMAIN, SERVICE_NOTIFY, info)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
|
|
|
|
2016-08-17 05:05:41 +00:00
|
|
|
# pylint: disable=too-many-locals
|
2015-01-04 09:01:49 +00:00
|
|
|
def setup(hass, config):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Setup the notify services."""
|
2015-08-19 02:28:40 +00:00
|
|
|
success = False
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-09-27 06:17:04 +00:00
|
|
|
descriptions = load_yaml_config_file(
|
|
|
|
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
|
|
|
|
2016-08-17 05:05:41 +00:00
|
|
|
targets = {}
|
|
|
|
|
2016-03-28 01:48:51 +00:00
|
|
|
for platform, p_config in config_per_platform(config, DOMAIN):
|
2015-11-15 10:04:57 +00:00
|
|
|
notify_implementation = bootstrap.prepare_setup_platform(
|
|
|
|
hass, config, DOMAIN, platform)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-08-19 02:28:40 +00:00
|
|
|
if notify_implementation is None:
|
|
|
|
_LOGGER.error("Unknown notification service specified.")
|
|
|
|
continue
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-11-09 06:15:34 +00:00
|
|
|
notify_service = notify_implementation.get_service(hass, p_config)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-08-19 02:28:40 +00:00
|
|
|
if notify_service is None:
|
|
|
|
_LOGGER.error("Failed to initialize notification service %s",
|
|
|
|
platform)
|
|
|
|
continue
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-08-19 02:28:40 +00:00
|
|
|
def notify_message(notify_service, call):
|
2016-02-23 20:06:50 +00:00
|
|
|
"""Handle sending notification message service calls."""
|
2016-09-10 14:36:55 +00:00
|
|
|
kwargs = {}
|
2016-04-12 06:51:51 +00:00
|
|
|
message = call.data[ATTR_MESSAGE]
|
2016-09-10 14:36:55 +00:00
|
|
|
title = call.data.get(ATTR_TITLE)
|
|
|
|
|
|
|
|
if title:
|
2016-09-28 04:29:55 +00:00
|
|
|
title.hass = hass
|
|
|
|
kwargs[ATTR_TITLE] = title.render()
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2016-08-17 05:05:41 +00:00
|
|
|
if targets.get(call.service) is not None:
|
2016-09-10 14:36:55 +00:00
|
|
|
kwargs[ATTR_TARGET] = targets[call.service]
|
2016-08-17 05:05:41 +00:00
|
|
|
else:
|
2016-09-10 14:36:55 +00:00
|
|
|
kwargs[ATTR_TARGET] = call.data.get(ATTR_TARGET)
|
|
|
|
|
2016-09-28 04:29:55 +00:00
|
|
|
message.hass = hass
|
|
|
|
kwargs[ATTR_MESSAGE] = message.render()
|
2016-09-10 14:36:55 +00:00
|
|
|
kwargs[ATTR_DATA] = call.data.get(ATTR_DATA)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2016-09-10 14:36:55 +00:00
|
|
|
notify_service.send_message(**kwargs)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-08-19 02:28:40 +00:00
|
|
|
service_call_handler = partial(notify_message, notify_service)
|
2016-08-17 05:05:41 +00:00
|
|
|
|
|
|
|
if hasattr(notify_service, 'targets'):
|
|
|
|
platform_name = (p_config.get(CONF_NAME) or platform)
|
2016-09-25 16:41:11 +00:00
|
|
|
for name, target in notify_service.targets.items():
|
|
|
|
target_name = slugify("{}_{}".format(platform_name, name))
|
2016-08-17 05:05:41 +00:00
|
|
|
targets[target_name] = target
|
|
|
|
hass.services.register(DOMAIN, target_name,
|
|
|
|
service_call_handler,
|
|
|
|
descriptions.get(SERVICE_NOTIFY),
|
|
|
|
schema=NOTIFY_SERVICE_SCHEMA)
|
|
|
|
|
|
|
|
platform_name = (p_config.get(CONF_NAME) or SERVICE_NOTIFY)
|
|
|
|
platform_name_slug = slugify(platform_name)
|
|
|
|
|
|
|
|
hass.services.register(DOMAIN, platform_name_slug,
|
|
|
|
service_call_handler,
|
2016-04-12 06:51:51 +00:00
|
|
|
descriptions.get(SERVICE_NOTIFY),
|
|
|
|
schema=NOTIFY_SERVICE_SCHEMA)
|
2015-08-19 02:28:40 +00:00
|
|
|
success = True
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-08-19 02:28:40 +00:00
|
|
|
return success
|
2015-01-04 09:01:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class BaseNotificationService(object):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""An abstract class for notification services."""
|
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.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|