2019-04-03 15:40:03 +00:00
|
|
|
"""Pushbullet platform for notify component."""
|
2015-01-04 09:01:49 +00:00
|
|
|
import logging
|
2017-08-30 19:42:27 +00:00
|
|
|
import mimetypes
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2019-12-09 12:06:25 +00:00
|
|
|
from pushbullet import InvalidKeyError, PushBullet, PushError
|
2016-09-07 00:00:33 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
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 12:06:25 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_URL = "url"
|
|
|
|
ATTR_FILE = "file"
|
|
|
|
ATTR_FILE_URL = "file_url"
|
|
|
|
ATTR_LIST = "list"
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_API_KEY): cv.string})
|
2016-09-07 00:00:33 +00:00
|
|
|
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def get_service(hass, config, discovery_info=None):
|
2017-03-27 08:35:27 +00:00
|
|
|
"""Get the Pushbullet notification service."""
|
2015-01-04 09:01:49 +00:00
|
|
|
|
|
|
|
try:
|
2015-11-15 18:57:16 +00:00
|
|
|
pushbullet = PushBullet(config[CONF_API_KEY])
|
2015-01-11 16:09:25 +00:00
|
|
|
except InvalidKeyError:
|
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-01-04 09:01:49 +00:00
|
|
|
|
2015-11-15 18:57:16 +00:00
|
|
|
return PushBulletNotificationService(pushbullet)
|
2015-11-15 17:50:36 +00:00
|
|
|
|
2015-01-04 09:01:49 +00:00
|
|
|
|
|
|
|
class PushBulletNotificationService(BaseNotificationService):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Implement the notification service for Pushbullet."""
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-11-15 17:50:36 +00:00
|
|
|
def __init__(self, pb):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Initialize the service."""
|
2015-11-15 17:50:36 +00:00
|
|
|
self.pushbullet = pb
|
2015-11-15 18:57:16 +00:00
|
|
|
self.pbtargets = {}
|
2015-11-15 17:50:36 +00:00
|
|
|
self.refresh()
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-11-15 17:50:36 +00:00
|
|
|
def refresh(self):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Refresh devices, contacts, etc.
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2017-03-27 08:35:27 +00:00
|
|
|
pbtargets stores all targets available from this Pushbullet instance
|
|
|
|
into a dict. These are Pushbullet objects!. It sacrifices a bit of
|
|
|
|
memory for faster processing at send_message.
|
2015-11-19 22:14:41 +00:00
|
|
|
|
|
|
|
As of sept 2015, contacts were replaced by chats. This is not
|
2015-11-25 07:56:50 +00:00
|
|
|
implemented in the module yet.
|
|
|
|
"""
|
2015-11-15 20:49:42 +00:00
|
|
|
self.pushbullet.refresh()
|
2015-11-15 17:50:36 +00:00
|
|
|
self.pbtargets = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"device": {tgt.nickname.lower(): tgt for tgt in self.pushbullet.devices},
|
|
|
|
"channel": {
|
|
|
|
tgt.channel_tag.lower(): tgt for tgt in self.pushbullet.channels
|
|
|
|
},
|
2015-11-15 17:50:36 +00:00
|
|
|
}
|
2015-01-04 09:01:49 +00:00
|
|
|
|
2015-11-15 17:50:36 +00:00
|
|
|
def send_message(self, message=None, **kwargs):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Send a message to a specified target.
|
|
|
|
|
2015-11-15 20:49:42 +00:00
|
|
|
If no target specified, a 'normal' push will be sent to all devices
|
2017-03-27 08:35:27 +00:00
|
|
|
linked to the Pushbullet account.
|
2015-11-19 22:14:41 +00:00
|
|
|
Email is special, these are assumed to always exist. We use a special
|
2015-11-25 07:56:50 +00:00
|
|
|
call which doesn't require a push object.
|
2015-11-15 20:49:42 +00:00
|
|
|
"""
|
2015-11-15 17:50:36 +00:00
|
|
|
targets = kwargs.get(ATTR_TARGET)
|
2016-09-01 13:35:46 +00:00
|
|
|
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
2016-10-14 07:11:48 +00:00
|
|
|
data = kwargs.get(ATTR_DATA)
|
2015-11-15 23:46:16 +00:00
|
|
|
refreshed = False
|
|
|
|
|
|
|
|
if not targets:
|
2017-11-26 20:12:47 +00:00
|
|
|
# Backward compatibility, notify all devices in own account.
|
2017-08-30 19:42:27 +00:00
|
|
|
self._push_data(message, title, data, self.pushbullet)
|
2017-03-27 08:35:27 +00:00
|
|
|
_LOGGER.info("Sent notification to self")
|
2015-11-15 23:46:16 +00:00
|
|
|
return
|
|
|
|
|
2017-11-26 20:12:47 +00:00
|
|
|
# Main loop, process all targets specified.
|
2015-11-15 23:46:16 +00:00
|
|
|
for target in targets:
|
2015-11-16 00:29:04 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
ttype, tname = target.split("/", 1)
|
2015-11-16 00:29:04 +00:00
|
|
|
except ValueError:
|
2017-03-27 08:35:27 +00:00
|
|
|
_LOGGER.error("Invalid target syntax: %s", target)
|
2015-11-16 00:29:04 +00:00
|
|
|
continue
|
2015-11-15 23:46:16 +00:00
|
|
|
|
2017-11-26 20:12:47 +00:00
|
|
|
# Target is email, send directly, don't use a target object.
|
2018-01-12 19:06:42 +00:00
|
|
|
# This also seems to work to send to all devices in own account.
|
2019-07-31 19:25:30 +00:00
|
|
|
if ttype == "email":
|
2017-08-30 19:42:27 +00:00
|
|
|
self._push_data(message, title, data, self.pushbullet, tname)
|
2017-03-27 08:35:27 +00:00
|
|
|
_LOGGER.info("Sent notification to email %s", tname)
|
2015-11-19 22:14:41 +00:00
|
|
|
continue
|
|
|
|
|
2015-11-16 00:29:04 +00:00
|
|
|
# Refresh if name not found. While awaiting periodic refresh
|
2017-11-26 20:12:47 +00:00
|
|
|
# solution in component, poor mans refresh.
|
2015-11-16 00:29:04 +00:00
|
|
|
if ttype not in self.pbtargets:
|
2017-03-27 08:35:27 +00:00
|
|
|
_LOGGER.error("Invalid target syntax: %s", target)
|
2015-11-16 00:29:04 +00:00
|
|
|
continue
|
2015-11-28 23:41:30 +00:00
|
|
|
|
|
|
|
tname = tname.lower()
|
|
|
|
|
|
|
|
if tname not in self.pbtargets[ttype] and not refreshed:
|
2015-11-15 23:46:16 +00:00
|
|
|
self.refresh()
|
|
|
|
refreshed = True
|
|
|
|
|
|
|
|
# Attempt push_note on a dict value. Keys are types & target
|
|
|
|
# name. Dict pbtargets has all *actual* targets.
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._push_data(message, title, data, self.pbtargets[ttype][tname])
|
2017-03-27 08:35:27 +00:00
|
|
|
_LOGGER.info("Sent notification to %s/%s", ttype, tname)
|
2015-11-15 23:46:16 +00:00
|
|
|
except KeyError:
|
2017-03-27 08:35:27 +00:00
|
|
|
_LOGGER.error("No such target: %s/%s", ttype, tname)
|
2015-11-15 23:46:16 +00:00
|
|
|
continue
|
2017-08-25 09:25:06 +00:00
|
|
|
|
2018-01-12 19:06:42 +00:00
|
|
|
def _push_data(self, message, title, data, pusher, email=None):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Create the message content."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-08-31 12:19:33 +00:00
|
|
|
if data is None:
|
|
|
|
data = {}
|
2018-01-12 19:06:42 +00:00
|
|
|
data_list = data.get(ATTR_LIST)
|
2017-08-30 19:42:27 +00:00
|
|
|
url = data.get(ATTR_URL)
|
|
|
|
filepath = data.get(ATTR_FILE)
|
|
|
|
file_url = data.get(ATTR_FILE_URL)
|
2017-08-26 07:36:54 +00:00
|
|
|
try:
|
2018-01-24 20:06:35 +00:00
|
|
|
email_kwargs = {}
|
|
|
|
if email:
|
2019-07-31 19:25:30 +00:00
|
|
|
email_kwargs["email"] = email
|
2017-08-26 07:36:54 +00:00
|
|
|
if url:
|
2018-01-24 20:06:35 +00:00
|
|
|
pusher.push_link(title, url, body=message, **email_kwargs)
|
2017-08-31 12:19:33 +00:00
|
|
|
elif filepath:
|
|
|
|
if not self.hass.config.is_allowed_path(filepath):
|
2017-11-26 20:12:47 +00:00
|
|
|
_LOGGER.error("Filepath is not valid or allowed")
|
2017-08-31 12:19:33 +00:00
|
|
|
return
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(filepath, "rb") as fileh:
|
2017-08-26 07:36:54 +00:00
|
|
|
filedata = self.pushbullet.upload_file(fileh, filepath)
|
2019-07-31 19:25:30 +00:00
|
|
|
if filedata.get("file_type") == "application/x-empty":
|
2017-11-26 20:12:47 +00:00
|
|
|
_LOGGER.error("Can not send an empty file")
|
2017-08-26 07:36:54 +00:00
|
|
|
return
|
2018-01-24 20:06:35 +00:00
|
|
|
filedata.update(email_kwargs)
|
2019-07-31 19:25:30 +00:00
|
|
|
pusher.push_file(title=title, body=message, **filedata)
|
2017-08-30 19:42:27 +00:00
|
|
|
elif file_url:
|
2019-07-31 19:25:30 +00:00
|
|
|
if not file_url.startswith("http"):
|
2017-11-26 20:12:47 +00:00
|
|
|
_LOGGER.error("URL should start with http or https")
|
2017-08-30 19:42:27 +00:00
|
|
|
return
|
2019-07-31 19:25:30 +00:00
|
|
|
pusher.push_file(
|
|
|
|
title=title,
|
|
|
|
body=message,
|
|
|
|
file_name=file_url,
|
|
|
|
file_url=file_url,
|
|
|
|
file_type=(mimetypes.guess_type(file_url)[0]),
|
|
|
|
**email_kwargs,
|
|
|
|
)
|
2018-01-12 19:06:42 +00:00
|
|
|
elif data_list:
|
2018-01-24 20:06:35 +00:00
|
|
|
pusher.push_list(title, data_list, **email_kwargs)
|
2017-08-26 07:36:54 +00:00
|
|
|
else:
|
2018-01-24 20:06:35 +00:00
|
|
|
pusher.push_note(title, message, **email_kwargs)
|
2017-08-26 07:36:54 +00:00
|
|
|
except PushError as err:
|
|
|
|
_LOGGER.error("Notify failed: %s", err)
|