pushbullet, send a file from url (#9189)

* pushbullet, send a file from url

* pushbullet, send a file from url

* Simplify
pull/9232/head
Daniel Høyer Iversen 2017-08-30 21:42:27 +02:00 committed by Fabian Affolter
parent f2551c08af
commit 214c92d787
1 changed files with 17 additions and 11 deletions

View File

@ -5,6 +5,7 @@ For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.pushbullet/
"""
import logging
import mimetypes
import voluptuous as vol
@ -20,6 +21,7 @@ _LOGGER = logging.getLogger(__name__)
ATTR_URL = 'url'
ATTR_FILE = 'file'
ATTR_FILE_URL = 'file_url'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
@ -80,16 +82,11 @@ class PushBulletNotificationService(BaseNotificationService):
targets = kwargs.get(ATTR_TARGET)
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
data = kwargs.get(ATTR_DATA)
url = None
filepath = None
if data:
url = data.get(ATTR_URL, None)
filepath = data.get(ATTR_FILE, None)
refreshed = False
if not targets:
# Backward compatibility, notify all devices in own account
self._push_data(filepath, message, title, url, self.pushbullet)
self._push_data(message, title, data, self.pushbullet)
_LOGGER.info("Sent notification to self")
return
@ -104,8 +101,7 @@ class PushBulletNotificationService(BaseNotificationService):
# Target is email, send directly, don't use a target object
# This also seems works to send to all devices in own account
if ttype == 'email':
self._push_data(filepath, message, title, url,
self.pushbullet, tname)
self._push_data(message, title, data, self.pushbullet, tname)
_LOGGER.info("Sent notification to email %s", tname)
continue
@ -124,16 +120,19 @@ class PushBulletNotificationService(BaseNotificationService):
# Attempt push_note on a dict value. Keys are types & target
# name. Dict pbtargets has all *actual* targets.
try:
self._push_data(filepath, message, title, url,
self._push_data(message, title, data,
self.pbtargets[ttype][tname])
_LOGGER.info("Sent notification to %s/%s", ttype, tname)
except KeyError:
_LOGGER.error("No such target: %s/%s", ttype, tname)
continue
def _push_data(self, filepath, message, title, url, pusher, tname=None):
def _push_data(self, title, message, data, pusher, tname=None):
from pushbullet import PushError
from pushbullet import Device
url = data.get(ATTR_URL)
filepath = data.get(ATTR_FILE)
file_url = data.get(ATTR_FILE_URL)
try:
if url:
if isinstance(pusher, Device):
@ -144,9 +143,16 @@ class PushBulletNotificationService(BaseNotificationService):
with open(filepath, "rb") as fileh:
filedata = self.pushbullet.upload_file(fileh, filepath)
if filedata.get('file_type') == 'application/x-empty':
_LOGGER.error("Failed to send an empty file.")
_LOGGER.error("Can not send an empty file.")
return
pusher.push_file(title=title, body=message, **filedata)
elif file_url:
if not file_url.startswith('http'):
_LOGGER.error("Url should start with http or https.")
return
pusher.push_file(title=title, body=message, file_name=file_url,
file_url=file_url,
file_type=mimetypes.guess_type(file_url)[0])
else:
if isinstance(pusher, Device):
pusher.push_note(title, message)