core/homeassistant/components/notify/nma.py

66 lines
1.9 KiB
Python
Raw Normal View History

2015-04-24 20:27:16 +00:00
"""
homeassistant.components.notify.nma
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NMA (Notify My Android) notification service.
2015-10-13 20:41:53 +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.nma/
2015-04-24 20:27:16 +00:00
"""
import logging
import xml.etree.ElementTree as ET
2015-04-24 20:27:16 +00:00
2015-11-09 06:15:34 +00:00
import requests
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. """
2015-11-09 06:15:34 +00:00
if not validate_config({DOMAIN: config},
2015-04-24 20:27:16 +00:00
{DOMAIN: [CONF_API_KEY]},
_LOGGER):
return None
2015-11-09 06:15:34 +00:00
response = requests.get(_RESOURCE + 'verify',
params={"apikey": config[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)
2015-11-09 06:15:34 +00:00
return None
return NmaNotificationService(config[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):
self._api_key = api_key
2015-04-24 20:27:16 +00:00
def send_message(self, message="", **kwargs):
""" Send a message to a user. """
2015-11-09 06:15:34 +00:00
data = {
"apikey": self._api_key,
"application": 'home-assistant',
"event": kwargs.get(ATTR_TITLE),
"description": message,
"priority": 0,
}
2015-11-09 06:15:34 +00:00
response = requests.get(_RESOURCE + 'notify', params=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)