2015-01-04 01:01:49 -08:00
|
|
|
"""
|
2015-05-13 19:18:30 +02:00
|
|
|
homeassistant.components.notify.pushbullet
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-01-04 01:01:49 -08:00
|
|
|
PushBullet platform for notify component.
|
2015-05-13 19:18:30 +02:00
|
|
|
|
2015-10-13 22:30:21 +02:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 18:33:11 +01:00
|
|
|
https://home-assistant.io/components/notify.pushbullet/
|
2015-01-04 01:01:49 -08:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2015-11-08 22:15:34 -08:00
|
|
|
from homeassistant.components.notify import ATTR_TITLE, BaseNotificationService
|
2015-01-04 01:01:49 -08:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-11-15 17:50:36 +00:00
|
|
|
REQUIREMENTS = ['pushbullet.py==0.9.0']
|
|
|
|
ATTR_TARGET='target'
|
2015-01-04 01:01:49 -08:00
|
|
|
|
|
|
|
|
|
|
|
def get_service(hass, config):
|
2015-09-07 18:26:20 +02:00
|
|
|
""" Get the PushBullet notification service. """
|
2015-11-15 17:50:36 +00:00
|
|
|
from pushbullet import PushBullet
|
2015-11-08 22:15:34 -08:00
|
|
|
from pushbullet import InvalidKeyError
|
2015-01-04 01:01:49 -08:00
|
|
|
|
2015-11-08 22:15:34 -08:00
|
|
|
if CONF_API_KEY not in config:
|
|
|
|
_LOGGER.error("Unable to find config key '%s'", CONF_API_KEY)
|
2015-01-04 01:01:49 -08:00
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
2015-11-15 17:50:36 +00:00
|
|
|
pb = PushBullet(config[CONF_API_KEY])
|
2015-01-11 08:09:25 -08:00
|
|
|
|
|
|
|
except InvalidKeyError:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Wrong API key supplied. "
|
|
|
|
"Get it at https://www.pushbullet.com/account")
|
2015-11-08 22:15:34 -08:00
|
|
|
return None
|
2015-01-04 01:01:49 -08:00
|
|
|
|
2015-11-15 17:50:36 +00:00
|
|
|
return PushBulletNotificationService(pb)
|
|
|
|
|
2015-01-04 01:01:49 -08:00
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class PushBulletNotificationService(BaseNotificationService):
|
|
|
|
""" Implements notification service for Pushbullet. """
|
|
|
|
|
2015-11-15 17:50:36 +00:00
|
|
|
def __init__(self, pb):
|
|
|
|
self.pushbullet = pb
|
|
|
|
self.refresh()
|
2015-01-04 01:01:49 -08:00
|
|
|
|
2015-11-15 17:50:36 +00:00
|
|
|
def refresh(self):
|
|
|
|
''' Refresh devices, contacts, channels, etc '''
|
|
|
|
self.pushbullet.refresh()
|
2015-01-04 01:01:49 -08:00
|
|
|
|
2015-11-15 17:50:36 +00:00
|
|
|
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))
|
2015-01-04 01:01:49 -08:00
|
|
|
|
2015-11-15 17:50:36 +00:00
|
|
|
def send_message(self, message=None, **kwargs):
|
|
|
|
""" Send a message to a user. """
|
|
|
|
targets = kwargs.get(ATTR_TARGET)
|
2015-01-04 01:01:49 -08:00
|
|
|
title = kwargs.get(ATTR_TITLE)
|
|
|
|
|
2015-11-15 17:50:36 +00:00
|
|
|
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)
|
|
|
|
|