Initial commit of pushbullet enhancement

This commit is contained in:
Tom Duijf 2015-11-15 17:50:36 +00:00
parent d993f4014e
commit 0a586bd919
2 changed files with 58 additions and 9 deletions

View File

@ -23,6 +23,9 @@ DEPENDENCIES = []
ATTR_TITLE = "title"
ATTR_TITLE_DEFAULT = "Home Assistant"
# Target of the notification (user, device, etc)
ATTR_TARGET = 'target'
# Text to notify user of
ATTR_MESSAGE = "message"
@ -69,8 +72,9 @@ def setup(hass, config):
return
title = call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
target = call.data.get(ATTR_TARGET)
notify_service.send_message(message, title=title)
notify_service.send_message(message, title=title, target=target)
# register service
service_call_handler = partial(notify_message, notify_service)

View File

@ -12,11 +12,13 @@ from homeassistant.components.notify import ATTR_TITLE, BaseNotificationService
from homeassistant.const import CONF_API_KEY
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pushbullet.py==0.8.1']
REQUIREMENTS = ['pushbullet.py==0.9.0']
ATTR_TARGET='target'
def get_service(hass, config):
""" Get the PushBullet notification service. """
from pushbullet import PushBullet
from pushbullet import InvalidKeyError
if CONF_API_KEY not in config:
@ -24,7 +26,7 @@ def get_service(hass, config):
return None
try:
return PushBulletNotificationService(config[CONF_API_KEY])
pb = PushBullet(config[CONF_API_KEY])
except InvalidKeyError:
_LOGGER.error(
@ -32,19 +34,62 @@ def get_service(hass, config):
"Get it at https://www.pushbullet.com/account")
return None
return PushBulletNotificationService(pb)
# pylint: disable=too-few-public-methods
class PushBulletNotificationService(BaseNotificationService):
""" Implements notification service for Pushbullet. """
def __init__(self, api_key):
from pushbullet import Pushbullet
def __init__(self, pb):
self.pushbullet = pb
self.refresh()
self.pushbullet = Pushbullet(api_key)
def refresh(self):
''' Refresh devices, contacts, channels, etc '''
self.pushbullet.refresh()
def send_message(self, message="", **kwargs):
self.pbtargets = {
'devices' :
{target.nickname: target for target in self.pushbullet.devices},
'contacts' :
{target.email: target for target in self.pushbullet.contacts},
'channels' :
{target.channel_tag: target for target in self.pushbullet.channels},
}
import pprint
_LOGGER.error(pprint.pformat(self.pbtargets))
def send_message(self, message=None, **kwargs):
""" Send a message to a user. """
targets = kwargs.get(ATTR_TARGET)
title = kwargs.get(ATTR_TITLE)
self.pushbullet.push_note(title, message)
if targets:
# Make list if not so
if not isinstance(targets, list):
targets = [targets]
# Main loop, Process all targets specified
for ttype,tname in [target.split('.') for target in targets]:
if ttype = 'device' and tname = '':
# Allow for 'normal' push, combined with other targets
self.pushbullet.push_note(None, message)
continue
try:
self.pbtargets[ttype+'s'][tname].push_note(None, message)
except KeyError:
_LOGGER.error('No such target: %s.%s'%(ttype, tname))
continue
except self.pushbullet.errors.PushError as e:
_LOGGER.error('Sending message failed to: %s.%s, %s'%
(ttype, tname, e))
self.refresh()
continue
_LOGGER.info('Sent notification to: %s.%s'%(ttype, tname))
else:
# Backward compatebility, notify all devices in own account
self.pushbullet.push_note(None, message)