core/homeassistant/components/notify/nma.py

98 lines
2.6 KiB
Python
Raw Normal View History

2015-04-24 20:27:16 +00:00
"""
homeassistant.components.notify.nma
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NMA (Notify My Android) notification service.
Configuration:
To use the NMA notifier you will need to add something like the following
to your config/configuration.yaml
notify:
platform: nma
api_key: YOUR_API_KEY
VARIABLES:
api_key
*Required
Enter the API key for NMA. Go to https://www.notifymyandroid.com and create a
new API key to use with Home Assistant.
Details for the API : https://www.notifymyandroid.com/api.jsp
2015-04-24 20:27:16 +00:00
"""
import logging
import xml.etree.ElementTree as ET
2015-04-24 20:27:16 +00:00
from homeassistant.helpers import validate_config
from homeassistant.components.notify import (
DOMAIN, ATTR_TITLE, BaseNotificationService)
from homeassistant.const import CONF_API_KEY
_LOGGER = logging.getLogger(__name__)
_RESOURCE = 'https://www.notifymyandroid.com/publicapi/'
2015-04-24 20:27:16 +00:00
def get_service(hass, config):
""" Get the NMA notification service. """
if not validate_config(config,
{DOMAIN: [CONF_API_KEY]},
_LOGGER):
return None
try:
# pylint: disable=unused-variable
2015-04-27 21:34:17 +00:00
from requests import Session
2015-04-24 20:27:16 +00:00
except ImportError:
_LOGGER.exception(
"Unable to import requests. "
"Did you maybe not install the 'Requests' package?")
2015-04-24 20:27:16 +00:00
return None
nma = Session()
2015-04-27 12:55:51 +00:00
response = nma.get(_RESOURCE + 'verify',
2015-04-27 21:34:17 +00:00
params={"apikey": config[DOMAIN][CONF_API_KEY]})
tree = ET.fromstring(response.content)
2015-04-28 06:45:40 +00:00
if tree[0].tag == 'error':
2015-04-28 06:45:40 +00:00
_LOGGER.error("Wrong API key supplied. %s", tree[0].text)
else:
return NmaNotificationService(config[DOMAIN][CONF_API_KEY])
2015-04-24 20:27:16 +00:00
# pylint: disable=too-few-public-methods
class NmaNotificationService(BaseNotificationService):
""" Implements notification service for NMA. """
def __init__(self, api_key):
# pylint: disable=no-name-in-module, unused-variable
2015-04-27 21:34:17 +00:00
from requests import Session
self._api_key = api_key
2015-04-27 21:34:17 +00:00
self._data = {"apikey": self._api_key}
2015-04-24 20:27:16 +00:00
self.nma = Session()
2015-04-24 20:27:16 +00:00
def send_message(self, message="", **kwargs):
""" Send a message to a user. """
title = kwargs.get(ATTR_TITLE)
self._data['application'] = 'home-assistant'
self._data['event'] = title
self._data['description'] = message
self._data['priority'] = 0
2015-04-27 12:55:51 +00:00
response = self.nma.get(_RESOURCE + 'notify',
params=self._data)
tree = ET.fromstring(response.content)
2015-04-28 06:45:40 +00:00
if tree[0].tag == 'error':
_LOGGER.exception(
2015-04-28 06:45:40 +00:00
"Unable to perform request. Error: %s", tree[0].text)