2015-01-04 09:01:49 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.notify
|
2015-05-13 06:14:00 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-01-04 09:01:49 +00:00
|
|
|
|
|
|
|
Provides functionality to notify people.
|
|
|
|
"""
|
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
|
|
|
|
2015-09-27 06:17:04 +00:00
|
|
|
from homeassistant.config import load_yaml_config_file
|
2015-01-04 09:01:49 +00:00
|
|
|
from homeassistant.loader import get_component
|
2015-08-19 02:28:40 +00:00
|
|
|
from homeassistant.helpers import config_per_platform
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-08-19 02:49:27 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2015-01-04 09:01:49 +00:00
|
|
|
|
|
|
|
DOMAIN = "notify"
|
|
|
|
DEPENDENCIES = []
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
# Text to notify user of
|
|
|
|
ATTR_MESSAGE = "message"
|
|
|
|
|
|
|
|
SERVICE_NOTIFY = "notify"
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def send_message(hass, message):
|
|
|
|
""" Send a notification message. """
|
|
|
|
hass.services.call(DOMAIN, SERVICE_NOTIFY, {ATTR_MESSAGE: message})
|
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
""" Sets up 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'))
|
|
|
|
|
2015-08-19 02:28:40 +00:00
|
|
|
for platform, p_config in config_per_platform(config, DOMAIN, _LOGGER):
|
2015-08-19 02:42:01 +00:00
|
|
|
# get platform
|
2015-08-19 02:28:40 +00:00
|
|
|
notify_implementation = get_component(
|
|
|
|
'notify.{}'.format(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-08-19 02:28:40 +00:00
|
|
|
# create platform service
|
|
|
|
notify_service = notify_implementation.get_service(
|
|
|
|
hass, {DOMAIN: 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
|
|
|
# create service handler
|
|
|
|
def notify_message(notify_service, call):
|
|
|
|
""" Handle sending notification message service calls. """
|
|
|
|
message = call.data.get(ATTR_MESSAGE)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-08-19 02:28:40 +00:00
|
|
|
if message is None:
|
|
|
|
return
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-08-19 02:28:40 +00:00
|
|
|
title = call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-08-19 02:28:40 +00:00
|
|
|
notify_service.send_message(message, title=title)
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-08-19 02:28:40 +00:00
|
|
|
# register service
|
|
|
|
service_call_handler = partial(notify_message, notify_service)
|
|
|
|
service_notify = p_config.get(CONF_NAME, SERVICE_NOTIFY)
|
2015-09-27 06:17:04 +00:00
|
|
|
hass.services.register(DOMAIN, service_notify, service_call_handler,
|
|
|
|
descriptions.get(service_notify))
|
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):
|
2015-05-13 06:14:00 +00:00
|
|
|
""" Provides an ABC for notification services. """
|
2015-01-04 09:01:49 +00:00
|
|
|
|
|
|
|
def send_message(self, message, **kwargs):
|
|
|
|
"""
|
|
|
|
Send a message.
|
|
|
|
kwargs can contain ATTR_TITLE to specify a title.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|