From a0f1c1d17a02ea8f07da084662ef779af06c3c97 Mon Sep 17 00:00:00 2001 From: jamespcole Date: Sun, 22 Mar 2015 14:36:58 +1100 Subject: [PATCH 1/4] Added support for pushover notifications --- homeassistant/components/notify/pushover.py | 72 +++++++++++++++++++++ requirements.txt | 3 + 2 files changed, 75 insertions(+) create mode 100644 homeassistant/components/notify/pushover.py diff --git a/homeassistant/components/notify/pushover.py b/homeassistant/components/notify/pushover.py new file mode 100644 index 00000000000..3ebd9030683 --- /dev/null +++ b/homeassistant/components/notify/pushover.py @@ -0,0 +1,72 @@ +""" +Pushover platform for notify component. + +Configuration: + +To use the Pushover notifier you will need to add something like the following +to your config/configuration.yaml + +notify: + platform: pushover + user_key: ABCDEFGHJKLMNOPQRSTUVXYZ + +VARIABLES: + +user_key +*Required +To retrieve this value log into your account at http://pushover.com + +""" +import logging + +from homeassistant.helpers import validate_config +from homeassistant.components.notify import ( + DOMAIN, ATTR_TITLE, BaseNotificationService) + +_LOGGER = logging.getLogger(__name__) + +_API_TOKEN = 'a99PmRoFWhchykrsS6MQwgM3mPdhEv' + +def get_service(hass, config): + """ Get the pushover notification service. """ + + if not validate_config(config, + {DOMAIN: ['user_key']}, + _LOGGER): + return None + + try: + # pylint: disable=unused-variable + from pushover import Client + + except ImportError: + _LOGGER.exception( + "Unable to import pushover. " + "Did you maybe not install the 'python-pushover.py' package?") + + return None + + try: + return PushoverNotificationService(config[DOMAIN]['user_key']) + + except InvalidKeyError: + _LOGGER.error( + "Wrong API key supplied. " + "Get it at https://www.pushover.com") + + +# pylint: disable=too-few-public-methods +class PushoverNotificationService(BaseNotificationService): + """ Implements notification service for Pushover. """ + + def __init__(self, user_key): + from pushover import Client + self.user_key = user_key + self.pushover = client = Client(self.user_key, api_token=_API_TOKEN) + + def send_message(self, message="", **kwargs): + """ Send a message to a user. """ + + title = kwargs.get(ATTR_TITLE) + + self.pushover.send_message(message, title=title) diff --git a/requirements.txt b/requirements.txt index c4b2160c38c..9f0352ccef1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,3 +36,6 @@ pydispatcher>=2.0.5 # sensor.systemmonitor psutil>=2.2.1 + +#pushover notifications +python-pushover>=0.2 \ No newline at end of file From 20f52a7fb1a18620c54382abb1421761804503a6 Mon Sep 17 00:00:00 2001 From: jamespcole Date: Sun, 22 Mar 2015 15:13:57 +1100 Subject: [PATCH 2/4] Made API Key a required config variable --- homeassistant/components/notify/pushover.py | 44 +++++++++++++++------ 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/notify/pushover.py b/homeassistant/components/notify/pushover.py index 3ebd9030683..a82c8439779 100644 --- a/homeassistant/components/notify/pushover.py +++ b/homeassistant/components/notify/pushover.py @@ -8,10 +8,25 @@ to your config/configuration.yaml notify: platform: pushover + api_key: ABCDEFGHJKLMNOPQRSTUVXYZ user_key: ABCDEFGHJKLMNOPQRSTUVXYZ VARIABLES: +api_key +*Required +This parameter is optional but should be configured, in order to get an API +key you should go to pushover.com and register a new application. + +This is a quote from the pushover website regarding free/open source apps: +"If you are creating a client-side library, application, or open source project +that will be redistributed and installed by end-users, you may want to require +each of your users to register their own application rather than including your +own API token with the software." + +When setting up the application I recommend using the icon located here: +https://home-assistant.io/images/favicon-192x192.png + user_key *Required To retrieve this value log into your account at http://pushover.com @@ -22,22 +37,22 @@ 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__) -_API_TOKEN = 'a99PmRoFWhchykrsS6MQwgM3mPdhEv' def get_service(hass, config): """ Get the pushover notification service. """ if not validate_config(config, - {DOMAIN: ['user_key']}, + {DOMAIN: ['user_key', CONF_API_KEY]}, _LOGGER): return None try: # pylint: disable=unused-variable - from pushover import Client + from pushover import Client, InitError, RequestError except ImportError: _LOGGER.exception( @@ -47,9 +62,13 @@ def get_service(hass, config): return None try: - return PushoverNotificationService(config[DOMAIN]['user_key']) + api_token = config[DOMAIN].get(CONF_API_KEY) + return PushoverNotificationService( + config[DOMAIN]['user_key'], + api_token + ) - except InvalidKeyError: + except InitError: _LOGGER.error( "Wrong API key supplied. " "Get it at https://www.pushover.com") @@ -59,14 +78,17 @@ def get_service(hass, config): class PushoverNotificationService(BaseNotificationService): """ Implements notification service for Pushover. """ - def __init__(self, user_key): - from pushover import Client - self.user_key = user_key - self.pushover = client = Client(self.user_key, api_token=_API_TOKEN) + def __init__(self, user_key, api_token): + from pushover import Client, RequestError + self._user_key = user_key + self._api_token = api_token + self.pushover = client = Client(self._user_key, api_token=self._api_token) def send_message(self, message="", **kwargs): """ Send a message to a user. """ title = kwargs.get(ATTR_TITLE) - - self.pushover.send_message(message, title=title) + try: + self.pushover.send_message(message, title=title) + except RequestError: + _LOGGER.exception("Could not send pushover notification") From 58fd07e4c650964ca6bb7dc1a05fbdc81c17fea9 Mon Sep 17 00:00:00 2001 From: jamespcole Date: Sun, 22 Mar 2015 15:32:47 +1100 Subject: [PATCH 3/4] fixed the flake8 and pylint warnings --- homeassistant/components/notify/pushover.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/notify/pushover.py b/homeassistant/components/notify/pushover.py index a82c8439779..ca3bf47819f 100644 --- a/homeassistant/components/notify/pushover.py +++ b/homeassistant/components/notify/pushover.py @@ -42,6 +42,7 @@ from homeassistant.const import CONF_API_KEY _LOGGER = logging.getLogger(__name__) +# pylint: disable=unused-variable def get_service(hass, config): """ Get the pushover notification service. """ @@ -51,8 +52,8 @@ def get_service(hass, config): return None try: - # pylint: disable=unused-variable - from pushover import Client, InitError, RequestError + # pylint: disable=no-name-in-module, unused-variable + from pushover import InitError except ImportError: _LOGGER.exception( @@ -64,9 +65,8 @@ def get_service(hass, config): try: api_token = config[DOMAIN].get(CONF_API_KEY) return PushoverNotificationService( - config[DOMAIN]['user_key'], - api_token - ) + config[DOMAIN]['user_key'], + api_token) except InitError: _LOGGER.error( @@ -79,14 +79,18 @@ class PushoverNotificationService(BaseNotificationService): """ Implements notification service for Pushover. """ def __init__(self, user_key, api_token): - from pushover import Client, RequestError + # pylint: disable=no-name-in-module, unused-variable + from pushover import Client self._user_key = user_key self._api_token = api_token - self.pushover = client = Client(self._user_key, api_token=self._api_token) + self.pushover = Client( + self._user_key, api_token=self._api_token) def send_message(self, message="", **kwargs): """ Send a message to a user. """ + # pylint: disable=no-name-in-module + from pushover import RequestError title = kwargs.get(ATTR_TITLE) try: self.pushover.send_message(message, title=title) From 7a2aa43caab56fc499085fafb6c15e5cbe129b12 Mon Sep 17 00:00:00 2001 From: jamespcole Date: Sun, 22 Mar 2015 15:37:43 +1100 Subject: [PATCH 4/4] Fixed incorrect URL in config instructions --- homeassistant/components/notify/pushover.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/notify/pushover.py b/homeassistant/components/notify/pushover.py index ca3bf47819f..466cfce8ea0 100644 --- a/homeassistant/components/notify/pushover.py +++ b/homeassistant/components/notify/pushover.py @@ -16,7 +16,7 @@ VARIABLES: api_key *Required This parameter is optional but should be configured, in order to get an API -key you should go to pushover.com and register a new application. +key you should go to https://pushover.net and register a new application. This is a quote from the pushover website regarding free/open source apps: "If you are creating a client-side library, application, or open source project @@ -29,7 +29,7 @@ https://home-assistant.io/images/favicon-192x192.png user_key *Required -To retrieve this value log into your account at http://pushover.com +To retrieve this value log into your account at https://pushover.net """ import logging @@ -71,7 +71,7 @@ def get_service(hass, config): except InitError: _LOGGER.error( "Wrong API key supplied. " - "Get it at https://www.pushover.com") + "Get it at https://pushover.net") # pylint: disable=too-few-public-methods