2015-03-22 03:36:58 +00:00
|
|
|
"""
|
2015-05-13 17:18:30 +00:00
|
|
|
homeassistant.components.notify.pushover
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-03-22 03:36:58 +00:00
|
|
|
Pushover platform for notify component.
|
|
|
|
|
2015-10-13 21:07:26 +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.pushover/
|
2015-03-22 03:36:58 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.helpers import validate_config
|
|
|
|
from homeassistant.components.notify import (
|
|
|
|
DOMAIN, ATTR_TITLE, BaseNotificationService)
|
2015-03-22 04:13:57 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
2015-03-22 03:36:58 +00:00
|
|
|
|
2015-08-30 01:39:50 +00:00
|
|
|
REQUIREMENTS = ['python-pushover==0.2']
|
2015-03-22 03:36:58 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-03-22 04:32:47 +00:00
|
|
|
# pylint: disable=unused-variable
|
2015-03-22 03:36:58 +00:00
|
|
|
def get_service(hass, config):
|
|
|
|
""" Get the pushover notification service. """
|
|
|
|
|
2015-11-09 06:15:34 +00:00
|
|
|
if not validate_config({DOMAIN: config},
|
2015-03-22 04:13:57 +00:00
|
|
|
{DOMAIN: ['user_key', CONF_API_KEY]},
|
2015-03-22 03:36:58 +00:00
|
|
|
_LOGGER):
|
|
|
|
return None
|
|
|
|
|
2015-11-09 06:15:34 +00:00
|
|
|
from pushover import InitError
|
2015-03-22 03:36:58 +00:00
|
|
|
|
|
|
|
try:
|
2015-11-09 06:15:34 +00:00
|
|
|
return PushoverNotificationService(config['user_key'],
|
|
|
|
config[CONF_API_KEY])
|
2015-03-22 04:13:57 +00:00
|
|
|
except InitError:
|
2015-03-22 03:36:58 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Wrong API key supplied. "
|
2015-03-22 04:37:43 +00:00
|
|
|
"Get it at https://pushover.net")
|
2015-11-09 06:15:34 +00:00
|
|
|
return None
|
2015-03-22 03:36:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class PushoverNotificationService(BaseNotificationService):
|
|
|
|
""" Implements notification service for Pushover. """
|
|
|
|
|
2015-03-22 04:13:57 +00:00
|
|
|
def __init__(self, user_key, api_token):
|
2015-03-22 04:32:47 +00:00
|
|
|
from pushover import Client
|
2015-03-22 04:13:57 +00:00
|
|
|
self._user_key = user_key
|
|
|
|
self._api_token = api_token
|
2015-03-22 04:32:47 +00:00
|
|
|
self.pushover = Client(
|
|
|
|
self._user_key, api_token=self._api_token)
|
2015-03-22 03:36:58 +00:00
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
|
|
|
""" Send a message to a user. """
|
2015-03-22 04:32:47 +00:00
|
|
|
from pushover import RequestError
|
2015-11-09 06:15:34 +00:00
|
|
|
|
2015-03-22 04:13:57 +00:00
|
|
|
try:
|
2015-11-09 06:15:34 +00:00
|
|
|
self.pushover.send_message(message, title=kwargs.get(ATTR_TITLE))
|
2015-03-22 04:13:57 +00:00
|
|
|
except RequestError:
|
|
|
|
_LOGGER.exception("Could not send pushover notification")
|