Merge pull request #641 from balloob/pushbullet_email

Notify/pushbullet - adjustment in supported targets
This commit is contained in:
Paulus Schoutsen 2015-11-19 22:46:41 -08:00
commit 037bca041e

View file

@ -48,20 +48,22 @@ class PushBulletNotificationService(BaseNotificationService):
def refresh(self): def refresh(self):
''' '''
Refresh devices, contacts, channels, etc Refresh devices, contacts, etc
pbtargets stores all targets available from this pushbullet instance pbtargets stores all targets available from this pushbullet instance
into a dict. These are PB objects!. It sacrifices a bit of memory into a dict. These are PB objects!. It sacrifices a bit of memory
for faster processing at send_message for faster processing at send_message
As of sept 2015, contacts were replaced by chats. This is not
implemented in the module yet
''' '''
self.pushbullet.refresh() self.pushbullet.refresh()
self.pbtargets = { self.pbtargets = {
'device': 'device': {
{tgt.nickname: tgt for tgt in self.pushbullet.devices}, tgt.nickname.lower(): tgt for tgt in self.pushbullet.devices},
'contact': 'channel': {
{tgt.email: tgt for tgt in self.pushbullet.contacts}, tgt.channel_tag.lower(): tgt for
'channel': tgt in self.pushbullet.channels},
{tgt.channel_tag: tgt for tgt in self.pushbullet.channels},
} }
def send_message(self, message=None, **kwargs): def send_message(self, message=None, **kwargs):
@ -69,6 +71,8 @@ class PushBulletNotificationService(BaseNotificationService):
Send a message to a specified target. Send a message to a specified target.
If no target specified, a 'normal' push will be sent to all devices If no target specified, a 'normal' push will be sent to all devices
linked to the PB account. linked to the PB account.
Email is special, these are assumed to always exist. We use a special
call which doesn't require a push object
""" """
targets = kwargs.get(ATTR_TARGET) targets = kwargs.get(ATTR_TARGET)
title = kwargs.get(ATTR_TITLE) title = kwargs.get(ATTR_TITLE)
@ -86,36 +90,36 @@ class PushBulletNotificationService(BaseNotificationService):
# Main loop, Process all targets specified # Main loop, Process all targets specified
for target in targets: for target in targets:
# Allow for untargeted push, combined with other types
if target in ['device', 'device/']:
self.pushbullet.push_note(title, message)
_LOGGER.info('Sent notification to self')
continue
try: try:
ttype, tname = target.split('/', 1) ttype, tname = target.split('/', 1)
except ValueError: except ValueError:
_LOGGER.error('Invalid target syntax: %s', target) _LOGGER.error('Invalid target syntax: %s', target)
continue continue
# 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.pushbullet.push_note(title, message, email=tname)
_LOGGER.info('Sent notification to self')
continue
# Refresh if name not found. While awaiting periodic refresh # Refresh if name not found. While awaiting periodic refresh
# solution in component, poor mans refresh ;) # solution in component, poor mans refresh ;)
if ttype not in self.pbtargets: if ttype not in self.pbtargets:
_LOGGER.error('Invalid target syntax: %s', target) _LOGGER.error('Invalid target syntax: %s', target)
continue continue
if tname not in self.pbtargets[ttype] and not refreshed: if tname.lower() not in self.pbtargets[ttype] and not refreshed:
self.refresh() self.refresh()
refreshed = True refreshed = True
# 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.pbtargets[ttype][tname].push_note(title, message) self.pbtargets[ttype][tname.lower()].push_note(title, message)
except KeyError: except KeyError:
_LOGGER.error('No such target: %s.%s', ttype, tname) _LOGGER.error('No such target: %s/%s', ttype, tname)
continue continue
except self.pushbullet.errors.PushError: except self.pushbullet.errors.PushError:
_LOGGER.error('Notify failed to: %s.%s', ttype, tname) _LOGGER.error('Notify failed to: %s/%s', ttype, tname)
continue continue
_LOGGER.info('Sent notification to %s.%s', ttype, tname) _LOGGER.info('Sent notification to %s/%s', ttype, tname)