pushbullet; styling and minor fixed before PR

This commit is contained in:
Tom Duijf 2015-11-15 18:57:16 +00:00
parent 0a586bd919
commit 3a85bebbf6

View File

@ -13,9 +13,9 @@ from homeassistant.const import CONF_API_KEY
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pushbullet.py==0.9.0'] REQUIREMENTS = ['pushbullet.py==0.9.0']
ATTR_TARGET='target' ATTR_TARGET = 'target'
# pylint: disable=unused-argument
def get_service(hass, config): def get_service(hass, config):
""" Get the PushBullet notification service. """ """ Get the PushBullet notification service. """
from pushbullet import PushBullet from pushbullet import PushBullet
@ -26,15 +26,14 @@ def get_service(hass, config):
return None return None
try: try:
pb = PushBullet(config[CONF_API_KEY]) pushbullet = PushBullet(config[CONF_API_KEY])
except InvalidKeyError: except InvalidKeyError:
_LOGGER.error( _LOGGER.error(
"Wrong API key supplied. " "Wrong API key supplied. "
"Get it at https://www.pushbullet.com/account") "Get it at https://www.pushbullet.com/account")
return None return None
return PushBulletNotificationService(pb) return PushBulletNotificationService(pushbullet)
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
@ -43,6 +42,7 @@ class PushBulletNotificationService(BaseNotificationService):
def __init__(self, pb): def __init__(self, pb):
self.pushbullet = pb self.pushbullet = pb
self.pbtargets = {}
self.refresh() self.refresh()
def refresh(self): def refresh(self):
@ -50,20 +50,20 @@ class PushBulletNotificationService(BaseNotificationService):
self.pushbullet.refresh() self.pushbullet.refresh()
self.pbtargets = { self.pbtargets = {
'devices' : 'devices':
{target.nickname: target for target in self.pushbullet.devices}, {tgt.nickname: tgt for tgt in self.pushbullet.devices},
'contacts' : 'contacts':
{target.email: target for target in self.pushbullet.contacts}, {tgt.email: tgt for tgt in self.pushbullet.contacts},
'channels' : 'channels':
{target.channel_tag: target for target in self.pushbullet.channels}, {tgt.channel_tag: tgt for tgt in self.pushbullet.channels},
} }
import pprint
_LOGGER.error(pprint.pformat(self.pbtargets))
def send_message(self, message=None, **kwargs): def send_message(self, message=None, **kwargs):
""" Send a message to a user. """ """ Send a message to a user. """
targets = kwargs.get(ATTR_TARGET) targets = kwargs.get(ATTR_TARGET)
title = kwargs.get(ATTR_TITLE) title = kwargs.get(ATTR_TITLE)
# Disabeling title
title = None
if targets: if targets:
# Make list if not so # Make list if not so
@ -71,25 +71,25 @@ class PushBulletNotificationService(BaseNotificationService):
targets = [targets] targets = [targets]
# Main loop, Process all targets specified # Main loop, Process all targets specified
for ttype,tname in [target.split('.') for target in targets]: for ttype, tname in [target.split('.') for target in targets]:
if ttype = 'device' and tname = '': if ttype == 'device' and not tname:
# Allow for 'normal' push, combined with other targets # Allow for 'normal' push, combined with other targets
self.pushbullet.push_note(None, message) self.pushbullet.push_note(title, message)
_LOGGER.info('Sent notification to self')
continue continue
try: try:
self.pbtargets[ttype+'s'][tname].push_note(None, message) self.pbtargets[ttype+'s'][tname].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 as e: except self.pushbullet.errors.PushError:
_LOGGER.error('Sending message failed to: %s.%s, %s'% _LOGGER.error('Notify failed to: %s.%s', ttype, tname)
(ttype, tname, e))
self.refresh() self.refresh()
continue continue
_LOGGER.info('Sent notification to: %s.%s'%(ttype, tname)) _LOGGER.info('Sent notification to %s.%s', ttype, tname)
else: else:
# Backward compatebility, notify all devices in own account # Backward compatebility, notify all devices in own account
self.pushbullet.push_note(None, message) self.pushbullet.push_note(title, message)
_LOGGER.info('Sent notification to self')