core/homeassistant/components/notify/instapush.py

88 lines
2.8 KiB
Python
Raw Normal View History

2015-04-25 22:53:36 +00:00
"""
homeassistant.components.notify.instapush
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
2015-04-25 22:53:36 +00:00
from homeassistant.components.notify import (
2016-02-19 05:27:50 +00:00
ATTR_TITLE, DOMAIN, BaseNotificationService)
2015-04-25 22:53:36 +00:00
from homeassistant.const import CONF_API_KEY
2016-02-19 05:27:50 +00:00
from homeassistant.helpers import validate_config
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):
""" Get the instapush notification service. """
2015-11-09 06:15:34 +00:00
if not validate_config({DOMAIN: config},
2015-04-27 17:34:34 +00:00
{DOMAIN: [CONF_API_KEY,
'app_secret',
'event',
'tracker']},
2015-04-25 22:53:36 +00:00
_LOGGER):
return None
2015-11-09 06:15:34 +00:00
headers = {'x-instapush-appid': config[CONF_API_KEY],
'x-instapush-appsecret': config['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
2015-11-09 06:15:34 +00:00
if len([app for app in response if app['title'] == config['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(
config[CONF_API_KEY], config['app_secret'], config['event'],
config['tracker'])
2015-04-25 22:53:36 +00:00
# pylint: disable=too-few-public-methods
class InstapushNotificationService(BaseNotificationService):
""" Implements notification service for Instapush. """
def __init__(self, api_key, app_secret, event, tracker):
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):
""" Send a message to a user. """
title = kwargs.get(ATTR_TITLE)
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/")