Files
home-assistant-core/homeassistant/components/notify/__init__.py
T
Paulus Schoutsen dc68f61261 Html5 push notifications notify platform (#2807)
* Initial work to add Chrome Push Notification support

* Remove push.js from home-assistant since it is now in Polymer

* Chrome->HTML5, general cleanup/fixes

* Make html5 generic, move manifest.json into frontend so that we can dynamically add the gcm_sender_id

* Pylint, flake8, pydocstyle frontend init

* HTML5 push fixes

* Update polymer

* Remove crypto req

* Add notify default platform.

* Fix HTML5 push

* Registration fixes

* Linting fix

* pep257 fix

* Add tests

* pep257 fix

* Update frontend
2016-08-14 01:10:07 -07:00

122 lines
3.5 KiB
Python

"""
Provides functionality to notify people.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/notify/
"""
from functools import partial
import logging
import os
import voluptuous as vol
import homeassistant.bootstrap as bootstrap
from homeassistant.config import load_yaml_config_file
from homeassistant.helpers import config_per_platform, template
import homeassistant.helpers.config_validation as cv
from homeassistant.const import CONF_NAME, CONF_PLATFORM
from homeassistant.util import slugify
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"
# Platform specific data
ATTR_DATA = 'data'
SERVICE_NOTIFY = "notify"
PLATFORM_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): cv.string,
vol.Optional(CONF_NAME): cv.string,
}, extra=vol.ALLOW_EXTRA)
NOTIFY_SERVICE_SCHEMA = vol.Schema({
vol.Required(ATTR_MESSAGE): cv.template,
vol.Optional(ATTR_TITLE, default=ATTR_TITLE_DEFAULT): cv.string,
vol.Optional(ATTR_TARGET): cv.string,
vol.Optional(ATTR_DATA): dict,
})
_LOGGER = logging.getLogger(__name__)
def send_message(hass, message, title=None, data=None):
"""Send a notification message."""
info = {
ATTR_MESSAGE: message
}
if title is not None:
info[ATTR_TITLE] = title
if data is not None:
info[ATTR_DATA] = data
hass.services.call(DOMAIN, SERVICE_NOTIFY, info)
def setup(hass, config):
"""Setup the notify services."""
success = False
descriptions = load_yaml_config_file(
os.path.join(os.path.dirname(__file__), 'services.yaml'))
for platform, p_config in config_per_platform(config, DOMAIN):
notify_implementation = bootstrap.prepare_setup_platform(
hass, config, DOMAIN, platform)
if notify_implementation is None:
_LOGGER.error("Unknown notification service specified.")
continue
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):
"""Handle sending notification message service calls."""
message = call.data[ATTR_MESSAGE]
title = template.render(
hass, call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT))
target = call.data.get(ATTR_TARGET)
message = template.render(hass, message)
data = call.data.get(ATTR_DATA)
notify_service.send_message(message, title=title, target=target,
data=data)
service_call_handler = partial(notify_message, notify_service)
service_name = slugify(p_config.get(CONF_NAME) or SERVICE_NOTIFY)
hass.services.register(DOMAIN, service_name, service_call_handler,
descriptions.get(SERVICE_NOTIFY),
schema=NOTIFY_SERVICE_SCHEMA)
success = True
return success
# pylint: disable=too-few-public-methods
class BaseNotificationService(object):
"""An abstract class for notification services."""
def send_message(self, message, **kwargs):
"""Send a message.
kwargs can contain ATTR_TITLE to specify a title.
"""
raise NotImplementedError