core/homeassistant/components/notify/simplepush.py

60 lines
1.9 KiB
Python
Raw Normal View History

2016-09-13 17:26:47 +00:00
"""
Simplepush notification service.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.simplepush/
"""
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
2016-09-13 17:26:47 +00:00
from homeassistant.components.notify import (
ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService)
from homeassistant.const import CONF_PASSWORD
2017-11-05 21:52:58 +00:00
REQUIREMENTS = ['simplepush==1.1.4']
2016-09-13 17:26:47 +00:00
_LOGGER = logging.getLogger(__name__)
ATTR_ENCRYPTED = 'encrypted'
2016-09-13 17:26:47 +00:00
CONF_DEVICE_KEY = 'device_key'
CONF_EVENT = 'event'
CONF_SALT = 'salt'
2016-09-13 17:26:47 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_DEVICE_KEY): cv.string,
vol.Optional(CONF_EVENT): cv.string,
vol.Inclusive(CONF_PASSWORD, ATTR_ENCRYPTED): cv.string,
vol.Inclusive(CONF_SALT, ATTR_ENCRYPTED): cv.string,
2016-09-13 17:26:47 +00:00
})
def get_service(hass, config, discovery_info=None):
2016-09-13 17:26:47 +00:00
"""Get the Simplepush notification service."""
return SimplePushNotificationService(config)
2016-09-13 17:26:47 +00:00
class SimplePushNotificationService(BaseNotificationService):
"""Implementation of the notification service for Simplepush."""
2016-09-13 17:26:47 +00:00
def __init__(self, config):
"""Initialize the Simplepush notification service."""
self._device_key = config.get(CONF_DEVICE_KEY)
self._event = config.get(CONF_EVENT)
self._password = config.get(CONF_PASSWORD)
self._salt = config.get(CONF_SALT)
2016-09-13 17:26:47 +00:00
def send_message(self, message='', **kwargs):
"""Send a message to a Simplepush user."""
from simplepush import send, send_encrypted
2016-09-13 17:26:47 +00:00
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
if self._password:
send_encrypted(self._device_key, self._password, self._salt, title,
message, event=self._event)
else:
send(self._device_key, title, message, event=self._event)