core/homeassistant/components/notify/__init__.py

105 lines
3.0 KiB
Python
Raw Normal View History

"""
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/
"""
from functools import partial
import logging
2015-09-27 06:17:04 +00:00
import os
import homeassistant.bootstrap as bootstrap
2015-09-27 06:17:04 +00:00
from homeassistant.config import load_yaml_config_file
from homeassistant.helpers import config_per_platform
2016-02-23 20:06:50 +00:00
from homeassistant.helpers import template
from homeassistant.const import CONF_NAME
DOMAIN = "notify"
# Title of notification
ATTR_TITLE = "title"
ATTR_TITLE_DEFAULT = "Home Assistant"
# Target of the notification (user, device, etc)
ATTR_TARGET = 'target'
# Text to notify user of
ATTR_MESSAGE = "message"
SERVICE_NOTIFY = "notify"
_LOGGER = logging.getLogger(__name__)
2015-12-10 07:46:50 +00:00
def send_message(hass, message, title=None):
2016-02-23 20:06:50 +00:00
"""Send a notification message."""
2015-12-10 07:46:50 +00:00
data = {
ATTR_MESSAGE: message
}
if title is not None:
data[ATTR_TITLE] = title
hass.services.call(DOMAIN, SERVICE_NOTIFY, data)
def setup(hass, config):
2016-03-08 10:46:32 +00:00
"""Setup the notify services."""
success = False
2015-09-27 06:17:04 +00:00
descriptions = load_yaml_config_file(
os.path.join(os.path.dirname(__file__), 'services.yaml'))
for platform, p_config in config_per_platform(config, DOMAIN, _LOGGER):
notify_implementation = bootstrap.prepare_setup_platform(
hass, config, DOMAIN, platform)
if notify_implementation is None:
_LOGGER.error("Unknown notification service specified.")
continue
2015-11-09 06:15:34 +00:00
notify_service = notify_implementation.get_service(hass, p_config)
if notify_service is None:
_LOGGER.error("Failed to initialize notification service %s",
platform)
continue
def notify_message(notify_service, call):
2016-02-23 20:06:50 +00:00
"""Handle sending notification message service calls."""
message = call.data.get(ATTR_MESSAGE)
if message is None:
_LOGGER.error(
'Received call to %s without attribute %s',
call.service, ATTR_MESSAGE)
return
2015-12-10 07:46:50 +00:00
title = template.render(
hass, call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT))
target = call.data.get(ATTR_TARGET)
2015-12-10 07:46:50 +00:00
message = template.render(hass, message)
notify_service.send_message(message, title=title, target=target)
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))
success = True
return success
# pylint: disable=too-few-public-methods
class BaseNotificationService(object):
2016-03-08 10:46:32 +00:00
"""An abstract class for notification services."""
def send_message(self, message, **kwargs):
2016-03-08 10:46:32 +00:00
"""Send a message.
kwargs can contain ATTR_TITLE to specify a title.
"""
raise NotImplementedError