From 07f62fda32ee9a1f9eec27d44a9276db6cde07e7 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 24 Apr 2015 22:27:16 +0200 Subject: [PATCH 1/6] inital nma notify component --- homeassistant/components/notify/nma.py | 68 ++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 homeassistant/components/notify/nma.py diff --git a/homeassistant/components/notify/nma.py b/homeassistant/components/notify/nma.py new file mode 100644 index 00000000000..280fbe2ef6d --- /dev/null +++ b/homeassistant/components/notify/nma.py @@ -0,0 +1,68 @@ +""" +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 for NMA. Go to https://www.notifymyandroid.com and create a +new API to use with Home Assistant. + +""" +import logging + +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__) + + +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 + from homeassistant.external.pynma.pynma import PyNMA + + except ImportError: + _LOGGER.exception( + "Unable to import pyNMA. " + "Did you maybe not install the 'PyNMA' package?") + + return None + + +# pylint: disable=too-few-public-methods +class NmaNotificationService(BaseNotificationService): + """ Implements notification service for NMA. """ + + def __init__(self, api_key): + from homeassistant.external.pynma.pynma import PyNMA + + self.nma = PyNMA(api_key) + + def send_message(self, message="", **kwargs): + """ Send a message to a user. """ + + title = kwargs.get(ATTR_TITLE) + + self.nma.push('home-assistant', title, message) From 4a5c32e3759919d3eee21dd1621e984ddb1fe2dd Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 27 Apr 2015 14:52:22 +0200 Subject: [PATCH 2/6] no longer use pynma and check if keys are ok --- homeassistant/components/notify/nma.py | 44 +++++++++++++++++++++----- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/notify/nma.py b/homeassistant/components/notify/nma.py index 280fbe2ef6d..253218d653e 100644 --- a/homeassistant/components/notify/nma.py +++ b/homeassistant/components/notify/nma.py @@ -17,11 +17,14 @@ VARIABLES: api_key *Required -Enter the API for NMA. Go to https://www.notifymyandroid.com and create a -new API to use with Home Assistant. +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 """ import logging +import xml.etree.ElementTree as ET from homeassistant.helpers import validate_config from homeassistant.components.notify import ( @@ -29,6 +32,7 @@ from homeassistant.components.notify import ( from homeassistant.const import CONF_API_KEY _LOGGER = logging.getLogger(__name__) +_RESOURCE = 'https://www.notifymyandroid.com/publicapi/' def get_service(hass, config): @@ -41,28 +45,52 @@ def get_service(hass, config): try: # pylint: disable=unused-variable - from homeassistant.external.pynma.pynma import PyNMA + from requests import Request, Session except ImportError: _LOGGER.exception( - "Unable to import pyNMA. " - "Did you maybe not install the 'PyNMA' package?") + "Unable to import requests. " + "Did you maybe not install the 'Requests' package?") return None + nma = Session() + response = nma.get(_RESOURCE + 'verify', params={"apikey" : config[DOMAIN][CONF_API_KEY]}) + tree = ET.fromstring(response.content) + if tree[0].tag == 'error': + _LOGGER.error( + "Wrong API key supplied. " + "Error: {}".format(tree[0].text)) + else: + return NmaNotificationService(config[DOMAIN][CONF_API_KEY]) + # pylint: disable=too-few-public-methods class NmaNotificationService(BaseNotificationService): """ Implements notification service for NMA. """ def __init__(self, api_key): - from homeassistant.external.pynma.pynma import PyNMA + # pylint: disable=no-name-in-module, unused-variable + from requests import Request, Session - self.nma = PyNMA(api_key) + self._api_key = api_key + self._data = {"apikey" : self._api_key} + + self.nma = Session() def send_message(self, message="", **kwargs): """ Send a message to a user. """ title = kwargs.get(ATTR_TITLE) - self.nma.push('home-assistant', title, message) + self._data['application'] = 'home-assistant' + self._data['event'] = title + self._data['description'] = message + self._data['priority'] = 0 + + response = self.nma.get(_RESOURCE + 'notify', params=self._data) + tree = ET.fromstring(response.content) + if tree[0].tag == 'error': + _LOGGER.exception( + "Unable to perform request. " + "Error: {}".format(tree[0].text)) \ No newline at end of file From 69799bd11e7ffba2183a5b63f0aef093343fbd1e Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 27 Apr 2015 14:55:51 +0200 Subject: [PATCH 3/6] minor layout changes --- homeassistant/components/notify/nma.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/notify/nma.py b/homeassistant/components/notify/nma.py index 253218d653e..a382a480e48 100644 --- a/homeassistant/components/notify/nma.py +++ b/homeassistant/components/notify/nma.py @@ -55,7 +55,8 @@ def get_service(hass, config): return None nma = Session() - response = nma.get(_RESOURCE + 'verify', params={"apikey" : config[DOMAIN][CONF_API_KEY]}) + response = nma.get(_RESOURCE + 'verify', + params={"apikey" : config[DOMAIN][CONF_API_KEY]}) tree = ET.fromstring(response.content) if tree[0].tag == 'error': _LOGGER.error( @@ -88,9 +89,10 @@ class NmaNotificationService(BaseNotificationService): self._data['description'] = message self._data['priority'] = 0 - response = self.nma.get(_RESOURCE + 'notify', params=self._data) + response = self.nma.get(_RESOURCE + 'notify', + params=self._data) tree = ET.fromstring(response.content) if tree[0].tag == 'error': _LOGGER.exception( "Unable to perform request. " - "Error: {}".format(tree[0].text)) \ No newline at end of file + "Error: {}".format(tree[0].text)) From 8b64e905b8a4e2889393b58a1700d86b2e3def09 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 27 Apr 2015 23:34:17 +0200 Subject: [PATCH 4/6] fix pylint error --- homeassistant/components/notify/nma.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/notify/nma.py b/homeassistant/components/notify/nma.py index a382a480e48..9bc24c12353 100644 --- a/homeassistant/components/notify/nma.py +++ b/homeassistant/components/notify/nma.py @@ -45,7 +45,7 @@ def get_service(hass, config): try: # pylint: disable=unused-variable - from requests import Request, Session + from requests import Session except ImportError: _LOGGER.exception( @@ -56,12 +56,13 @@ def get_service(hass, config): nma = Session() response = nma.get(_RESOURCE + 'verify', - params={"apikey" : config[DOMAIN][CONF_API_KEY]}) + params={"apikey": config[DOMAIN][CONF_API_KEY]}) tree = ET.fromstring(response.content) + # pylint: disable=logging-not-lazy if tree[0].tag == 'error': _LOGGER.error( "Wrong API key supplied. " - "Error: {}".format(tree[0].text)) + "{}".format(tree[0].text)) else: return NmaNotificationService(config[DOMAIN][CONF_API_KEY]) @@ -72,10 +73,10 @@ class NmaNotificationService(BaseNotificationService): def __init__(self, api_key): # pylint: disable=no-name-in-module, unused-variable - from requests import Request, Session + from requests import Session self._api_key = api_key - self._data = {"apikey" : self._api_key} + self._data = {"apikey": self._api_key} self.nma = Session() @@ -92,6 +93,7 @@ class NmaNotificationService(BaseNotificationService): response = self.nma.get(_RESOURCE + 'notify', params=self._data) tree = ET.fromstring(response.content) + # pylint: disable=logging-not-lazy if tree[0].tag == 'error': _LOGGER.exception( "Unable to perform request. " From b76b002966a6482fbb0d8a02046d2450349381a0 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 27 Apr 2015 23:51:59 +0200 Subject: [PATCH 5/6] try to pass travis build --- homeassistant/components/notify/nma.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/notify/nma.py b/homeassistant/components/notify/nma.py index 9bc24c12353..3c0b54111d0 100644 --- a/homeassistant/components/notify/nma.py +++ b/homeassistant/components/notify/nma.py @@ -58,7 +58,7 @@ def get_service(hass, config): response = nma.get(_RESOURCE + 'verify', params={"apikey": config[DOMAIN][CONF_API_KEY]}) tree = ET.fromstring(response.content) - # pylint: disable=logging-not-lazy + # pylint: disable=bad-option-value if tree[0].tag == 'error': _LOGGER.error( "Wrong API key supplied. " @@ -93,7 +93,7 @@ class NmaNotificationService(BaseNotificationService): response = self.nma.get(_RESOURCE + 'notify', params=self._data) tree = ET.fromstring(response.content) - # pylint: disable=logging-not-lazy + # pylint: disable=bad-option-value if tree[0].tag == 'error': _LOGGER.exception( "Unable to perform request. " From 14d0c1325c7691d12a72f07a52f7c85aeb748b77 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Tue, 28 Apr 2015 08:45:40 +0200 Subject: [PATCH 6/6] fix format --- homeassistant/components/notify/nma.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/notify/nma.py b/homeassistant/components/notify/nma.py index 3c0b54111d0..db6c91d8fed 100644 --- a/homeassistant/components/notify/nma.py +++ b/homeassistant/components/notify/nma.py @@ -58,11 +58,9 @@ def get_service(hass, config): response = nma.get(_RESOURCE + 'verify', params={"apikey": config[DOMAIN][CONF_API_KEY]}) tree = ET.fromstring(response.content) - # pylint: disable=bad-option-value + if tree[0].tag == 'error': - _LOGGER.error( - "Wrong API key supplied. " - "{}".format(tree[0].text)) + _LOGGER.error("Wrong API key supplied. %s", tree[0].text) else: return NmaNotificationService(config[DOMAIN][CONF_API_KEY]) @@ -93,8 +91,7 @@ class NmaNotificationService(BaseNotificationService): response = self.nma.get(_RESOURCE + 'notify', params=self._data) tree = ET.fromstring(response.content) - # pylint: disable=bad-option-value + if tree[0].tag == 'error': _LOGGER.exception( - "Unable to perform request. " - "Error: {}".format(tree[0].text)) + "Unable to perform request. Error: %s", tree[0].text)