mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
pushbullet, send a file from url (#9189)
* pushbullet, send a file from url * pushbullet, send a file from url * Simplify
This commit is contained in:
parent
f2551c08af
commit
214c92d787
@ -5,6 +5,7 @@ For more details about this platform, please refer to the documentation at
|
|||||||
https://home-assistant.io/components/notify.pushbullet/
|
https://home-assistant.io/components/notify.pushbullet/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
import mimetypes
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -20,6 +21,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
ATTR_URL = 'url'
|
ATTR_URL = 'url'
|
||||||
ATTR_FILE = 'file'
|
ATTR_FILE = 'file'
|
||||||
|
ATTR_FILE_URL = 'file_url'
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
vol.Required(CONF_API_KEY): cv.string,
|
||||||
@ -80,16 +82,11 @@ class PushBulletNotificationService(BaseNotificationService):
|
|||||||
targets = kwargs.get(ATTR_TARGET)
|
targets = kwargs.get(ATTR_TARGET)
|
||||||
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||||
data = kwargs.get(ATTR_DATA)
|
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
|
refreshed = False
|
||||||
|
|
||||||
if not targets:
|
if not targets:
|
||||||
# Backward compatibility, notify all devices in own account
|
# 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")
|
_LOGGER.info("Sent notification to self")
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -104,8 +101,7 @@ class PushBulletNotificationService(BaseNotificationService):
|
|||||||
# Target is email, send directly, don't use a target object
|
# Target is email, send directly, don't use a target object
|
||||||
# This also seems works to send to all devices in own account
|
# This also seems works to send to all devices in own account
|
||||||
if ttype == 'email':
|
if ttype == 'email':
|
||||||
self._push_data(filepath, message, title, url,
|
self._push_data(message, title, data, self.pushbullet, tname)
|
||||||
self.pushbullet, tname)
|
|
||||||
_LOGGER.info("Sent notification to email %s", tname)
|
_LOGGER.info("Sent notification to email %s", tname)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -124,16 +120,19 @@ class PushBulletNotificationService(BaseNotificationService):
|
|||||||
# Attempt push_note on a dict value. Keys are types & target
|
# Attempt push_note on a dict value. Keys are types & target
|
||||||
# name. Dict pbtargets has all *actual* targets.
|
# name. Dict pbtargets has all *actual* targets.
|
||||||
try:
|
try:
|
||||||
self._push_data(filepath, message, title, url,
|
self._push_data(message, title, data,
|
||||||
self.pbtargets[ttype][tname])
|
self.pbtargets[ttype][tname])
|
||||||
_LOGGER.info("Sent notification to %s/%s", ttype, tname)
|
_LOGGER.info("Sent notification to %s/%s", ttype, tname)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
_LOGGER.error("No such target: %s/%s", ttype, tname)
|
_LOGGER.error("No such target: %s/%s", ttype, tname)
|
||||||
continue
|
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 PushError
|
||||||
from pushbullet import Device
|
from pushbullet import Device
|
||||||
|
url = data.get(ATTR_URL)
|
||||||
|
filepath = data.get(ATTR_FILE)
|
||||||
|
file_url = data.get(ATTR_FILE_URL)
|
||||||
try:
|
try:
|
||||||
if url:
|
if url:
|
||||||
if isinstance(pusher, Device):
|
if isinstance(pusher, Device):
|
||||||
@ -144,9 +143,16 @@ class PushBulletNotificationService(BaseNotificationService):
|
|||||||
with open(filepath, "rb") as fileh:
|
with open(filepath, "rb") as fileh:
|
||||||
filedata = self.pushbullet.upload_file(fileh, filepath)
|
filedata = self.pushbullet.upload_file(fileh, filepath)
|
||||||
if filedata.get('file_type') == 'application/x-empty':
|
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
|
return
|
||||||
pusher.push_file(title=title, body=message, **filedata)
|
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:
|
else:
|
||||||
if isinstance(pusher, Device):
|
if isinstance(pusher, Device):
|
||||||
pusher.push_note(title, message)
|
pusher.push_note(title, message)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user