Pushbullet email support (fix) (#11590)
* Simplified push calls * Cleaned up and added unittests * Fixed email parameter * Fixed email parameterpull/11589/head
parent
1802c0a922
commit
92bec562ab
|
@ -22,6 +22,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
ATTR_URL = 'url'
|
ATTR_URL = 'url'
|
||||||
ATTR_FILE = 'file'
|
ATTR_FILE = 'file'
|
||||||
ATTR_FILE_URL = 'file_url'
|
ATTR_FILE_URL = 'file_url'
|
||||||
|
ATTR_LIST = 'list'
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
vol.Required(CONF_API_KEY): cv.string,
|
||||||
|
@ -99,7 +100,7 @@ class PushBulletNotificationService(BaseNotificationService):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 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 to work to send to all devices in own account.
|
||||||
if ttype == 'email':
|
if ttype == 'email':
|
||||||
self._push_data(message, title, data, self.pushbullet, tname)
|
self._push_data(message, title, data, self.pushbullet, tname)
|
||||||
_LOGGER.info("Sent notification to email %s", tname)
|
_LOGGER.info("Sent notification to email %s", tname)
|
||||||
|
@ -127,20 +128,18 @@ class PushBulletNotificationService(BaseNotificationService):
|
||||||
_LOGGER.error("No such target: %s/%s", ttype, tname)
|
_LOGGER.error("No such target: %s/%s", ttype, tname)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
def _push_data(self, message, title, data, pusher, tname=None):
|
def _push_data(self, message, title, data, pusher, email=None):
|
||||||
"""Helper for creating the message content."""
|
"""Helper for creating the message content."""
|
||||||
from pushbullet import PushError
|
from pushbullet import PushError
|
||||||
if data is None:
|
if data is None:
|
||||||
data = {}
|
data = {}
|
||||||
|
data_list = data.get(ATTR_LIST)
|
||||||
url = data.get(ATTR_URL)
|
url = data.get(ATTR_URL)
|
||||||
filepath = data.get(ATTR_FILE)
|
filepath = data.get(ATTR_FILE)
|
||||||
file_url = data.get(ATTR_FILE_URL)
|
file_url = data.get(ATTR_FILE_URL)
|
||||||
try:
|
try:
|
||||||
if url:
|
if url:
|
||||||
if tname:
|
pusher.push_link(title, url, body=message, email=email)
|
||||||
pusher.push_link(title, url, body=message, email=tname)
|
|
||||||
else:
|
|
||||||
pusher.push_link(title, url, body=message)
|
|
||||||
elif filepath:
|
elif filepath:
|
||||||
if not self.hass.config.is_allowed_path(filepath):
|
if not self.hass.config.is_allowed_path(filepath):
|
||||||
_LOGGER.error("Filepath is not valid or allowed")
|
_LOGGER.error("Filepath is not valid or allowed")
|
||||||
|
@ -150,18 +149,20 @@ class PushBulletNotificationService(BaseNotificationService):
|
||||||
if filedata.get('file_type') == 'application/x-empty':
|
if filedata.get('file_type') == 'application/x-empty':
|
||||||
_LOGGER.error("Can not 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,
|
||||||
|
email=email, **filedata)
|
||||||
elif file_url:
|
elif file_url:
|
||||||
if not file_url.startswith('http'):
|
if not file_url.startswith('http'):
|
||||||
_LOGGER.error("URL should start with http or https")
|
_LOGGER.error("URL should start with http or https")
|
||||||
return
|
return
|
||||||
pusher.push_file(title=title, body=message, file_name=file_url,
|
pusher.push_file(title=title, body=message, email=email,
|
||||||
file_url=file_url,
|
file_name=file_url, file_url=file_url,
|
||||||
file_type=mimetypes.guess_type(file_url)[0])
|
file_type=(mimetypes
|
||||||
|
.guess_type(file_url)[0]))
|
||||||
|
elif data_list:
|
||||||
|
pusher.push_note(title, data_list, email=email)
|
||||||
else:
|
else:
|
||||||
if tname:
|
pusher.push_note(title, message, email=email)
|
||||||
pusher.push_note(title, message, email=tname)
|
|
||||||
else:
|
|
||||||
pusher.push_note(title, message)
|
|
||||||
except PushError as err:
|
except PushError as err:
|
||||||
_LOGGER.error("Notify failed: %s", err)
|
_LOGGER.error("Notify failed: %s", err)
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
"""The tests for the pushbullet notification platform."""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from homeassistant.setup import setup_component
|
||||||
|
import homeassistant.components.notify as notify
|
||||||
|
from tests.common import assert_setup_component, get_test_home_assistant
|
||||||
|
|
||||||
|
|
||||||
|
class TestPushbullet(unittest.TestCase):
|
||||||
|
"""Test the pushbullet notifications."""
|
||||||
|
|
||||||
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
|
"""Setup things to be run when tests are started."""
|
||||||
|
self.hass = get_test_home_assistant()
|
||||||
|
|
||||||
|
def tearDown(self): # pylint: disable=invalid-name
|
||||||
|
"""Stop down everything that was started."""
|
||||||
|
self.hass.stop()
|
||||||
|
|
||||||
|
def test_setup(self):
|
||||||
|
"""Test setup."""
|
||||||
|
with assert_setup_component(1) as handle_config:
|
||||||
|
assert setup_component(self.hass, 'notify', {
|
||||||
|
'notify': {
|
||||||
|
'name': 'test',
|
||||||
|
'platform': 'pushbullet',
|
||||||
|
'api_key': 'MYFAKEKEY', }
|
||||||
|
})
|
||||||
|
assert handle_config[notify.DOMAIN]
|
||||||
|
|
||||||
|
def test_bad_config(self):
|
||||||
|
"""Test set up the platform with bad/missing configuration."""
|
||||||
|
config = {
|
||||||
|
notify.DOMAIN: {
|
||||||
|
'name': 'test',
|
||||||
|
'platform': 'pushbullet',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
with assert_setup_component(0) as handle_config:
|
||||||
|
assert setup_component(self.hass, notify.DOMAIN, config)
|
||||||
|
assert not handle_config[notify.DOMAIN]
|
Loading…
Reference in New Issue