core/homeassistant/components/notify/instapush.py

91 lines
2.8 KiB
Python
Raw Normal View History

2015-04-25 22:53:36 +00:00
"""
Instapush notification service.
2015-10-13 21:08:56 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 17:33:11 +00:00
https://home-assistant.io/components/notify.instapush/
2015-04-25 22:53:36 +00:00
"""
import json
2016-02-19 05:27:50 +00:00
import logging
2015-04-25 22:53:36 +00:00
2015-11-09 06:15:34 +00:00
import requests
2016-09-02 09:14:18 +00:00
import voluptuous as vol
2015-11-09 06:15:34 +00:00
2016-09-02 09:14:18 +00:00
import homeassistant.helpers.config_validation as cv
2015-04-25 22:53:36 +00:00
from homeassistant.components.notify import (
2016-09-02 09:14:18 +00:00
ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService)
2015-04-25 22:53:36 +00:00
from homeassistant.const import CONF_API_KEY
2016-09-02 09:14:18 +00:00
CONF_APP_SECRET = 'app_secret'
CONF_EVENT = 'event'
CONF_TRACKER = 'tracker'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_APP_SECRET): cv.string,
vol.Required(CONF_EVENT): cv.string,
vol.Required(CONF_TRACKER): cv.string,
})
2015-04-25 22:53:36 +00:00
_LOGGER = logging.getLogger(__name__)
2015-04-27 17:34:34 +00:00
_RESOURCE = 'https://api.instapush.im/v1/'
2015-04-25 22:53:36 +00:00
def get_service(hass, config):
2016-03-08 10:46:32 +00:00
"""Get the instapush notification service."""
2015-11-09 06:15:34 +00:00
headers = {'x-instapush-appid': config[CONF_API_KEY],
2016-09-02 09:14:18 +00:00
'x-instapush-appsecret': config[CONF_APP_SECRET]}
2015-04-25 22:53:36 +00:00
2015-11-09 06:15:34 +00:00
try:
response = requests.get(_RESOURCE + 'events/list',
headers=headers).json()
except ValueError:
_LOGGER.error('Unexpected answer from Instapush API.')
2015-04-25 22:53:36 +00:00
return None
2015-11-09 06:15:34 +00:00
if 'error' in response:
_LOGGER.error(response['msg'])
return None
2015-04-25 22:53:36 +00:00
2016-09-02 09:14:18 +00:00
if len([app for app in response
if app['title'] == config[CONF_EVENT]]) == 0:
2015-04-25 22:53:36 +00:00
_LOGGER.error(
2015-11-09 06:15:34 +00:00
"No app match your given value. "
"Please create an app at https://instapush.im")
2015-04-25 22:53:36 +00:00
return None
2015-11-09 06:15:34 +00:00
return InstapushNotificationService(
2016-09-02 09:14:18 +00:00
config[CONF_API_KEY], config[CONF_APP_SECRET], config[CONF_EVENT],
config[CONF_TRACKER])
2015-04-25 22:53:36 +00:00
# pylint: disable=too-few-public-methods
class InstapushNotificationService(BaseNotificationService):
2016-03-08 10:46:32 +00:00
"""Implement the notification service for Instapush."""
2015-04-25 22:53:36 +00:00
def __init__(self, api_key, app_secret, event, tracker):
2016-03-08 10:46:32 +00:00
"""Initialize the service."""
2015-04-25 22:53:36 +00:00
self._api_key = api_key
self._app_secret = app_secret
self._event = event
self._tracker = tracker
self._headers = {
2015-04-27 17:34:34 +00:00
'x-instapush-appid': self._api_key,
'x-instapush-appsecret': self._app_secret,
'Content-Type': 'application/json'}
2015-04-25 22:53:36 +00:00
def send_message(self, message="", **kwargs):
2016-03-08 10:46:32 +00:00
"""Send a message to a user."""
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
2015-04-27 17:34:34 +00:00
data = {"event": self._event,
"trackers": {self._tracker: title + " : " + message}}
2015-04-25 22:53:36 +00:00
2015-11-09 06:15:34 +00:00
response = requests.post(_RESOURCE + 'post', data=json.dumps(data),
headers=self._headers)
2015-04-25 22:53:36 +00:00
2015-04-27 17:34:34 +00:00
if response.json()['status'] == 401:
2015-04-25 22:53:36 +00:00
_LOGGER.error(
2015-04-27 17:34:34 +00:00
response.json()['msg'],
"Please check your details at https://instapush.im/")