2019-04-03 15:40:03 +00:00
|
|
|
"""Pushover platform for notify component."""
|
2015-03-22 03:36:58 +00:00
|
|
|
import logging
|
|
|
|
|
2020-02-10 22:55:17 +00:00
|
|
|
from pushover_complete import PushoverAPI
|
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"
|
2020-02-10 22:55:17 +00:00
|
|
|
ATTR_URL = "url"
|
|
|
|
ATTR_URL_TITLE = "url_title"
|
|
|
|
ATTR_PRIORITY = "priority"
|
|
|
|
ATTR_RETRY = "retry"
|
|
|
|
ATTR_SOUND = "sound"
|
|
|
|
ATTR_HTML = "html"
|
|
|
|
ATTR_CALLBACK_URL = "callback_url"
|
|
|
|
ATTR_EXPIRE = "expire"
|
|
|
|
ATTR_TIMESTAMP = "timestamp"
|
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."""
|
2020-02-10 22:55:17 +00:00
|
|
|
return PushoverNotificationService(
|
|
|
|
hass, config[CONF_USER_KEY], config[CONF_API_KEY]
|
|
|
|
)
|
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
|
2020-02-10 22:55:17 +00:00
|
|
|
self.pushover = PushoverAPI(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
|
|
|
|
2020-02-10 22:55:17 +00:00
|
|
|
# Extract params from data dict
|
|
|
|
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
|
|
|
data = dict(kwargs.get(ATTR_DATA) or {})
|
2020-04-07 19:06:05 +00:00
|
|
|
url = data.get(ATTR_URL)
|
|
|
|
url_title = data.get(ATTR_URL_TITLE)
|
|
|
|
priority = data.get(ATTR_PRIORITY)
|
|
|
|
retry = data.get(ATTR_RETRY)
|
|
|
|
expire = data.get(ATTR_EXPIRE)
|
|
|
|
callback_url = data.get(ATTR_CALLBACK_URL)
|
|
|
|
timestamp = data.get(ATTR_TIMESTAMP)
|
|
|
|
sound = data.get(ATTR_SOUND)
|
2020-02-10 22:55:17 +00:00
|
|
|
html = 1 if data.get(ATTR_HTML, False) else 0
|
|
|
|
|
|
|
|
# Check for attachment
|
2021-10-22 09:29:21 +00:00
|
|
|
if (image := data.get(ATTR_ATTACHMENT)) is not None:
|
2020-02-10 22:55:17 +00:00
|
|
|
# Only allow attachments from whitelisted paths, check valid path
|
|
|
|
if self._hass.config.is_allowed_path(data[ATTR_ATTACHMENT]):
|
|
|
|
# try to open it as a normal file.
|
2019-07-02 15:56:12 +00:00
|
|
|
try:
|
2021-04-25 00:39:24 +00:00
|
|
|
# pylint: disable=consider-using-with
|
2020-02-10 22:55:17 +00:00
|
|
|
file_handle = open(data[ATTR_ATTACHMENT], "rb")
|
|
|
|
# Replace the attachment identifier with file object.
|
|
|
|
image = file_handle
|
|
|
|
except OSError as ex_val:
|
2019-07-02 15:56:12 +00:00
|
|
|
_LOGGER.error(ex_val)
|
|
|
|
# Remove attachment key to send without attachment.
|
2020-02-10 22:55:17 +00:00
|
|
|
image = None
|
|
|
|
else:
|
|
|
|
_LOGGER.error("Path is not whitelisted")
|
|
|
|
# Remove attachment key to send without attachment.
|
|
|
|
image = None
|
2019-07-02 15:56:12 +00:00
|
|
|
|
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:
|
|
|
|
try:
|
2020-02-10 22:55:17 +00:00
|
|
|
self.pushover.send_message(
|
|
|
|
self._user_key,
|
|
|
|
message,
|
|
|
|
target,
|
|
|
|
title,
|
|
|
|
url,
|
|
|
|
url_title,
|
|
|
|
image,
|
|
|
|
priority,
|
|
|
|
retry,
|
|
|
|
expire,
|
|
|
|
callback_url,
|
|
|
|
timestamp,
|
|
|
|
sound,
|
|
|
|
html,
|
|
|
|
)
|
2016-10-02 05:19:17 +00:00
|
|
|
except ValueError as val_err:
|
2019-07-02 15:56:12 +00:00
|
|
|
_LOGGER.error(val_err)
|