From 92bec562aba89ac5388561c3e2f2c0be94a10c6a Mon Sep 17 00:00:00 2001 From: tschmidty69 Date: Fri, 12 Jan 2018 14:06:42 -0500 Subject: [PATCH] Pushbullet email support (fix) (#11590) * Simplified push calls * Cleaned up and added unittests * Fixed email parameter * Fixed email parameter --- homeassistant/components/notify/pushbullet.py | 29 ++++++------- tests/components/notify/test_pushbullet.py | 42 +++++++++++++++++++ 2 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 tests/components/notify/test_pushbullet.py diff --git a/homeassistant/components/notify/pushbullet.py b/homeassistant/components/notify/pushbullet.py index 0e846ebaf84..359810bb6bc 100644 --- a/homeassistant/components/notify/pushbullet.py +++ b/homeassistant/components/notify/pushbullet.py @@ -22,6 +22,7 @@ _LOGGER = logging.getLogger(__name__) ATTR_URL = 'url' ATTR_FILE = 'file' ATTR_FILE_URL = 'file_url' +ATTR_LIST = 'list' PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_API_KEY): cv.string, @@ -99,7 +100,7 @@ class PushBulletNotificationService(BaseNotificationService): continue # 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': self._push_data(message, title, data, self.pushbullet, 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) 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.""" from pushbullet import PushError if data is None: data = {} + data_list = data.get(ATTR_LIST) url = data.get(ATTR_URL) filepath = data.get(ATTR_FILE) file_url = data.get(ATTR_FILE_URL) try: if url: - if tname: - pusher.push_link(title, url, body=message, email=tname) - else: - pusher.push_link(title, url, body=message) + pusher.push_link(title, url, body=message, email=email) elif filepath: if not self.hass.config.is_allowed_path(filepath): _LOGGER.error("Filepath is not valid or allowed") @@ -150,18 +149,20 @@ class PushBulletNotificationService(BaseNotificationService): if filedata.get('file_type') == 'application/x-empty': _LOGGER.error("Can not send an empty file") return - pusher.push_file(title=title, body=message, **filedata) + + pusher.push_file(title=title, body=message, + email=email, **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]) + pusher.push_file(title=title, body=message, email=email, + file_name=file_url, file_url=file_url, + file_type=(mimetypes + .guess_type(file_url)[0])) + elif data_list: + pusher.push_note(title, data_list, email=email) else: - if tname: - pusher.push_note(title, message, email=tname) - else: - pusher.push_note(title, message) + pusher.push_note(title, message, email=email) except PushError as err: _LOGGER.error("Notify failed: %s", err) diff --git a/tests/components/notify/test_pushbullet.py b/tests/components/notify/test_pushbullet.py new file mode 100644 index 00000000000..ba3046e8fd7 --- /dev/null +++ b/tests/components/notify/test_pushbullet.py @@ -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]