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