2015-04-24 20:27:16 +00:00
|
|
|
"""
|
|
|
|
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
|
2015-04-27 12:52:22 +00:00
|
|
|
import xml.etree.ElementTree as ET
|
2015-04-24 20:27:16 +00:00
|
|
|
|
2015-11-09 06:15:34 +00:00
|
|
|
import requests
|
2016-09-06 20:24:04 +00:00
|
|
|
import voluptuous as vol
|
2015-11-09 06:15:34 +00:00
|
|
|
|
2015-04-24 20:27:16 +00:00
|
|
|
from homeassistant.components.notify import (
|
2016-09-06 20:24:04 +00:00
|
|
|
ATTR_TITLE, ATTR_TITLE_DEFAULT, PLATFORM_SCHEMA, BaseNotificationService)
|
2015-04-24 20:27:16 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
2016-09-06 20:24:04 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-04-24 20:27:16 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-04-27 12:52:22 +00:00
|
|
|
_RESOURCE = 'https://www.notifymyandroid.com/publicapi/'
|
2015-04-24 20:27:16 +00:00
|
|
|
|
|
|
|
|
2016-09-06 20:24:04 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def get_service(hass, config, discovery_info=None):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Get the NMA notification service."""
|
2016-11-16 05:02:17 +00:00
|
|
|
parameters = {
|
|
|
|
'apikey': config[CONF_API_KEY],
|
|
|
|
}
|
|
|
|
response = requests.get(
|
|
|
|
'{}{}'.format(_RESOURCE, 'verify'), params=parameters, timeout=5)
|
2015-04-27 12:52:22 +00:00
|
|
|
tree = ET.fromstring(response.content)
|
2015-04-28 06:45:40 +00:00
|
|
|
|
2015-04-27 12:52:22 +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-27 12:52:22 +00:00
|
|
|
|
2015-04-24 20:27:16 +00:00
|
|
|
|
|
|
|
class NmaNotificationService(BaseNotificationService):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Implement the notification service for NMA."""
|
2015-04-24 20:27:16 +00:00
|
|
|
|
|
|
|
def __init__(self, api_key):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Initialize the service."""
|
2015-04-27 12:52:22 +00:00
|
|
|
self._api_key = api_key
|
2015-04-24 20:27:16 +00:00
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Send a message to a user."""
|
2015-11-09 06:15:34 +00:00
|
|
|
data = {
|
2016-11-16 05:02:17 +00:00
|
|
|
'apikey': self._api_key,
|
|
|
|
'application': 'home-assistant',
|
|
|
|
'event': kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT),
|
|
|
|
'description': message,
|
|
|
|
'priority': 0,
|
2015-11-09 06:15:34 +00:00
|
|
|
}
|
2015-04-27 12:52:22 +00:00
|
|
|
|
2016-11-16 05:02:17 +00:00
|
|
|
response = requests.get(
|
|
|
|
'{}{}'.format(_RESOURCE, 'notify'), params=data, timeout=5)
|
2015-04-27 12:52:22 +00:00
|
|
|
tree = ET.fromstring(response.content)
|
2015-04-28 06:45:40 +00:00
|
|
|
|
2015-04-27 12:52:22 +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)
|