core/homeassistant/components/notify/instapush.py

97 lines
3.0 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)
from homeassistant.const import (
CONF_API_KEY, HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON)
2016-09-02 09:14:18 +00:00
2016-09-11 07:21:01 +00:00
_LOGGER = logging.getLogger(__name__)
_RESOURCE = 'https://api.instapush.im/v1/'
2016-09-02 09:14:18 +00:00
CONF_APP_SECRET = 'app_secret'
CONF_EVENT = 'event'
CONF_TRACKER = 'tracker'
2016-09-11 07:21:01 +00:00
DEFAULT_TIMEOUT = 10
HTTP_HEADER_APPID = 'x-instapush-appid'
HTTP_HEADER_APPSECRET = 'x-instapush-appsecret'
2016-09-02 09:14:18 +00:00
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
def get_service(hass, config, discovery_info=None):
2016-09-11 07:21:01 +00:00
"""Get the Instapush notification service."""
headers = {
HTTP_HEADER_APPID: config[CONF_API_KEY],
HTTP_HEADER_APPSECRET: config[CONF_APP_SECRET],
}
2015-04-25 22:53:36 +00:00
2015-11-09 06:15:34 +00:00
try:
2016-09-11 07:21:01 +00:00
response = requests.get(
'{}{}'.format(_RESOURCE, 'events/list'), headers=headers,
timeout=DEFAULT_TIMEOUT).json()
2015-11-09 06:15:34 +00:00
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
if not [app for app in response if app['title'] == config[CONF_EVENT]]:
_LOGGER.error("No app match your given value")
2015-04-25 22:53:36 +00:00
return None
2015-11-09 06:15:34 +00:00
return InstapushNotificationService(
2016-09-11 07:21:01 +00:00
config.get(CONF_API_KEY), config.get(CONF_APP_SECRET),
config.get(CONF_EVENT), config.get(CONF_TRACKER))
2015-04-25 22:53:36 +00:00
class InstapushNotificationService(BaseNotificationService):
2016-09-11 07:21:01 +00:00
"""Implementation of 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 = {
HTTP_HEADER_APPID: self._api_key,
HTTP_HEADER_APPSECRET: self._app_secret,
HTTP_HEADER_CONTENT_TYPE: CONTENT_TYPE_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)
2016-09-11 07:21:01 +00:00
data = {
'event': self._event,
'trackers': {self._tracker: '{} : {}'.format(title, message)}
2016-09-11 07:21:01 +00:00
}
2015-04-25 22:53:36 +00:00
2016-09-11 07:21:01 +00:00
response = requests.post(
'{}{}'.format(_RESOURCE, 'post'), data=json.dumps(data),
headers=self._headers, timeout=DEFAULT_TIMEOUT)
2015-04-25 22:53:36 +00:00
2015-04-27 17:34:34 +00:00
if response.json()['status'] == 401:
2016-09-11 07:21:01 +00:00
_LOGGER.error(response.json()['msg'],
"Please check your Instapush settings")