2016-06-25 04:43:44 +00:00
|
|
|
"""
|
|
|
|
A component which is collecting configuration errors.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/persistent_notification/
|
|
|
|
"""
|
2016-06-25 23:40:33 +00:00
|
|
|
import logging
|
2016-06-25 04:43:44 +00:00
|
|
|
|
2016-06-25 23:40:33 +00:00
|
|
|
import voluptuous as vol
|
2016-06-25 04:43:44 +00:00
|
|
|
|
2016-06-25 23:40:33 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
|
|
|
from homeassistant.helpers import template, config_validation as cv
|
|
|
|
from homeassistant.helpers.entity import generate_entity_id
|
|
|
|
from homeassistant.util import slugify
|
2016-06-25 04:43:44 +00:00
|
|
|
|
2016-06-25 23:40:33 +00:00
|
|
|
DOMAIN = 'persistent_notification'
|
|
|
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
|
|
|
|
|
|
|
SERVICE_CREATE = 'create'
|
|
|
|
ATTR_TITLE = 'title'
|
|
|
|
ATTR_MESSAGE = 'message'
|
|
|
|
ATTR_NOTIFICATION_ID = 'notification_id'
|
|
|
|
|
|
|
|
SCHEMA_SERVICE_CREATE = vol.Schema({
|
|
|
|
vol.Required(ATTR_MESSAGE): cv.template,
|
|
|
|
vol.Optional(ATTR_TITLE): cv.template,
|
|
|
|
vol.Optional(ATTR_NOTIFICATION_ID): cv.string,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_OBJECT_ID = 'notification'
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def create(hass, message, title=None, notification_id=None):
|
2016-07-01 19:39:30 +00:00
|
|
|
"""Generate a notification."""
|
2016-06-25 23:40:33 +00:00
|
|
|
data = {
|
|
|
|
key: value for key, value in [
|
|
|
|
(ATTR_TITLE, title),
|
|
|
|
(ATTR_MESSAGE, message),
|
|
|
|
(ATTR_NOTIFICATION_ID, notification_id),
|
|
|
|
] if value is not None
|
|
|
|
}
|
|
|
|
|
|
|
|
hass.services.call(DOMAIN, SERVICE_CREATE, data)
|
2016-06-25 04:43:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
"""Setup the persistent notification component."""
|
2016-06-25 23:40:33 +00:00
|
|
|
def create_service(call):
|
|
|
|
"""Handle a create notification service call."""
|
|
|
|
title = call.data.get(ATTR_TITLE)
|
|
|
|
message = call.data.get(ATTR_MESSAGE)
|
|
|
|
notification_id = call.data.get(ATTR_NOTIFICATION_ID)
|
|
|
|
|
|
|
|
if notification_id is not None:
|
|
|
|
entity_id = ENTITY_ID_FORMAT.format(slugify(notification_id))
|
|
|
|
else:
|
|
|
|
entity_id = generate_entity_id(ENTITY_ID_FORMAT, DEFAULT_OBJECT_ID,
|
|
|
|
hass=hass)
|
|
|
|
attr = {}
|
|
|
|
if title is not None:
|
|
|
|
try:
|
|
|
|
title = template.render(hass, title)
|
|
|
|
except TemplateError as ex:
|
|
|
|
_LOGGER.error('Error rendering title %s: %s', title, ex)
|
|
|
|
|
|
|
|
attr[ATTR_TITLE] = title
|
|
|
|
|
|
|
|
try:
|
|
|
|
message = template.render(hass, message)
|
|
|
|
except TemplateError as ex:
|
|
|
|
_LOGGER.error('Error rendering message %s: %s', message, ex)
|
|
|
|
|
|
|
|
hass.states.set(entity_id, message, attr)
|
|
|
|
|
|
|
|
hass.services.register(DOMAIN, SERVICE_CREATE, create_service, {},
|
|
|
|
SCHEMA_SERVICE_CREATE)
|
|
|
|
|
2016-06-25 04:43:44 +00:00
|
|
|
return True
|