2019-04-03 15:40:03 +00:00
|
|
|
"""Pushover platform for notify component."""
|
2015-03-22 03:36:58 +00:00
|
|
|
import logging
|
|
|
|
|
2019-12-09 13:29:39 +00:00
|
|
|
from pushover import Client, InitError, RequestError
|
2019-10-14 08:56:40 +00:00
|
|
|
import requests
|
2016-08-27 00:43:59 +00:00
|
|
|
import voluptuous as vol
|
2015-03-22 03:36:58 +00:00
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
from homeassistant.components.notify import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DATA,
|
|
|
|
ATTR_TARGET,
|
|
|
|
ATTR_TITLE,
|
|
|
|
ATTR_TITLE_DEFAULT,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService,
|
|
|
|
)
|
2019-12-09 13:29:39 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2015-03-22 03:36:58 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ATTACHMENT = "attachment"
|
2015-03-22 03:36:58 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_USER_KEY = "user_key"
|
2016-09-07 00:00:33 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_USER_KEY): cv.string, vol.Required(CONF_API_KEY): cv.string}
|
|
|
|
)
|
2016-08-27 00:43:59 +00:00
|
|
|
|
|
|
|
|
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 Pushover notification service."""
|
2015-03-22 03:36:58 +00:00
|
|
|
try:
|
2017-05-02 16:18:47 +00:00
|
|
|
return PushoverNotificationService(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, config[CONF_USER_KEY], config[CONF_API_KEY]
|
|
|
|
)
|
2015-03-22 04:13:57 +00:00
|
|
|
except InitError:
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.error("Wrong API key supplied")
|
2015-11-09 06:15:34 +00:00
|
|
|
return None
|
2015-03-22 03:36:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PushoverNotificationService(BaseNotificationService):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Implement the notification service for Pushover."""
|
2015-03-22 03:36:58 +00:00
|
|
|
|
2019-07-02 15:56:12 +00:00
|
|
|
def __init__(self, hass, user_key, api_token):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Initialize the service."""
|
2019-07-02 15:56:12 +00:00
|
|
|
self._hass = hass
|
2015-03-22 04:13:57 +00:00
|
|
|
self._user_key = user_key
|
|
|
|
self._api_token = api_token
|
2019-07-31 19:25:30 +00:00
|
|
|
self.pushover = Client(self._user_key, api_token=self._api_token)
|
2015-03-22 03:36:58 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def send_message(self, message="", **kwargs):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Send a message to a user."""
|
2016-06-22 15:54:44 +00:00
|
|
|
# Make a copy and use empty dict if necessary
|
|
|
|
data = dict(kwargs.get(ATTR_DATA) or {})
|
2016-06-20 06:08:30 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data["title"] = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
2016-06-22 15:54:44 +00:00
|
|
|
|
2019-07-02 15:56:12 +00:00
|
|
|
# Check for attachment.
|
|
|
|
if ATTR_ATTACHMENT in data:
|
|
|
|
# If attachment is a URL, use requests to open it as a stream.
|
2019-07-31 19:25:30 +00:00
|
|
|
if data[ATTR_ATTACHMENT].startswith("http"):
|
2019-07-02 15:56:12 +00:00
|
|
|
try:
|
|
|
|
response = requests.get(
|
2019-07-31 19:25:30 +00:00
|
|
|
data[ATTR_ATTACHMENT], stream=True, timeout=5
|
|
|
|
)
|
2019-07-02 15:56:12 +00:00
|
|
|
if response.status_code == 200:
|
|
|
|
# Replace the attachment identifier with file object.
|
|
|
|
data[ATTR_ATTACHMENT] = response.content
|
|
|
|
else:
|
2020-01-11 09:36:50 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Failed to download image %s, response code: %d",
|
|
|
|
data[ATTR_ATTACHMENT],
|
|
|
|
response.status_code,
|
|
|
|
)
|
2019-07-02 15:56:12 +00:00
|
|
|
# Remove attachment key to send without attachment.
|
|
|
|
del data[ATTR_ATTACHMENT]
|
|
|
|
except requests.exceptions.RequestException as ex_val:
|
|
|
|
_LOGGER.error(ex_val)
|
|
|
|
# Remove attachment key to try sending without attachment
|
|
|
|
del data[ATTR_ATTACHMENT]
|
|
|
|
else:
|
|
|
|
# Not a URL, check valid path first
|
|
|
|
if self._hass.config.is_allowed_path(data[ATTR_ATTACHMENT]):
|
|
|
|
# try to open it as a normal file.
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
file_handle = open(data[ATTR_ATTACHMENT], "rb")
|
2019-07-02 15:56:12 +00:00
|
|
|
# Replace the attachment identifier with file object.
|
|
|
|
data[ATTR_ATTACHMENT] = file_handle
|
|
|
|
except OSError as ex_val:
|
|
|
|
_LOGGER.error(ex_val)
|
|
|
|
# Remove attachment key to send without attachment.
|
|
|
|
del data[ATTR_ATTACHMENT]
|
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Path is not whitelisted")
|
2019-07-02 15:56:12 +00:00
|
|
|
# Remove attachment key to send without attachment.
|
|
|
|
del data[ATTR_ATTACHMENT]
|
|
|
|
|
2016-10-02 05:19:17 +00:00
|
|
|
targets = kwargs.get(ATTR_TARGET)
|
|
|
|
|
2016-10-12 04:59:34 +00:00
|
|
|
if not isinstance(targets, list):
|
|
|
|
targets = [targets]
|
|
|
|
|
2016-10-02 05:19:17 +00:00
|
|
|
for target in targets:
|
|
|
|
if target is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["device"] = target
|
2016-10-02 05:19:17 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.pushover.send_message(message, **data)
|
|
|
|
except ValueError as val_err:
|
2019-07-02 15:56:12 +00:00
|
|
|
_LOGGER.error(val_err)
|
2016-10-02 05:19:17 +00:00
|
|
|
except RequestError:
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.exception("Could not send pushover notification")
|