2019-04-03 15:40:03 +00:00
|
|
|
"""Pushover platform for notify component."""
|
2015-03-22 03:36:58 +00:00
|
|
|
import logging
|
|
|
|
|
2016-08-27 00:43:59 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2015-03-22 04:13:57 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
2016-08-27 00:43:59 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-03-22 03:36:58 +00:00
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
from homeassistant.components.notify import (
|
2019-03-21 05:56:46 +00:00
|
|
|
ATTR_DATA, ATTR_TARGET, ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService)
|
|
|
|
|
2015-03-22 03:36:58 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-09-07 00:00:33 +00:00
|
|
|
CONF_USER_KEY = 'user_key'
|
|
|
|
|
2019-02-14 20:06:25 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2016-09-07 00:00:33 +00:00
|
|
|
vol.Required(CONF_USER_KEY): cv.string,
|
2016-08-27 00:43:59 +00:00
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def get_service(hass, config, discovery_info=None):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Get the Pushover notification service."""
|
2015-11-09 06:15:34 +00:00
|
|
|
from pushover import InitError
|
2015-03-22 03:36:58 +00:00
|
|
|
|
|
|
|
try:
|
2017-05-02 16:18:47 +00:00
|
|
|
return PushoverNotificationService(
|
|
|
|
config[CONF_USER_KEY], config[CONF_API_KEY])
|
2015-03-22 04:13:57 +00:00
|
|
|
except InitError:
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.error("Wrong API key supplied")
|
2015-11-09 06:15:34 +00:00
|
|
|
return None
|
2015-03-22 03:36:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PushoverNotificationService(BaseNotificationService):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Implement the notification service for Pushover."""
|
2015-03-22 03:36:58 +00:00
|
|
|
|
2015-03-22 04:13:57 +00:00
|
|
|
def __init__(self, user_key, api_token):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Initialize the service."""
|
2015-03-22 04:32:47 +00:00
|
|
|
from pushover import Client
|
2015-03-22 04:13:57 +00:00
|
|
|
self._user_key = user_key
|
|
|
|
self._api_token = api_token
|
2015-03-22 04:32:47 +00:00
|
|
|
self.pushover = Client(
|
|
|
|
self._user_key, api_token=self._api_token)
|
2015-03-22 03:36:58 +00:00
|
|
|
|
2016-08-27 00:43:59 +00:00
|
|
|
def send_message(self, message='', **kwargs):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Send a message to a user."""
|
2015-03-22 04:32:47 +00:00
|
|
|
from pushover import RequestError
|
2015-11-09 06:15:34 +00:00
|
|
|
|
2016-06-22 15:54:44 +00:00
|
|
|
# Make a copy and use empty dict if necessary
|
|
|
|
data = dict(kwargs.get(ATTR_DATA) or {})
|
2016-06-20 06:08:30 +00:00
|
|
|
|
2016-09-01 13:35:46 +00:00
|
|
|
data['title'] = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
2016-06-22 15:54:44 +00:00
|
|
|
|
2016-10-02 05:19:17 +00:00
|
|
|
targets = kwargs.get(ATTR_TARGET)
|
|
|
|
|
2016-10-12 04:59:34 +00:00
|
|
|
if not isinstance(targets, list):
|
|
|
|
targets = [targets]
|
|
|
|
|
2016-10-02 05:19:17 +00:00
|
|
|
for target in targets:
|
|
|
|
if target is not None:
|
|
|
|
data['device'] = target
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.pushover.send_message(message, **data)
|
|
|
|
except ValueError as val_err:
|
|
|
|
_LOGGER.error(str(val_err))
|
|
|
|
except RequestError:
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.exception("Could not send pushover notification")
|