Support for PushBullet events on notifications, #36

This commit is contained in:
Fabio Manganiello 2018-01-07 00:58:03 +01:00
parent 1611fd878c
commit 694d7ca62d
2 changed files with 38 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import websocket
from platypush.config import Config
from platypush.message import Message
from platypush.message.event.pushbullet import PushbulletEvent
from .. import Backend
@ -68,18 +69,25 @@ class PushbulletBackend(Backend):
def on_push(self):
def _f(ws, data):
try:
# Parse the push
try:
data = json.loads(data) if isinstance(data, str) else push
except Exception as e:
logging.exception(e)
return
# If it's a push, get it
if data['type'] == 'tickle' and data['subtype'] == 'push':
push = self._get_latest_push()
elif data['type'] == 'push':
push = data['push']
else: return # Not a push notification
# Post an event, useful to react on mobile notifications if
# you enabled notification mirroring on your PushBullet app
event = PushbulletEvent(**push)
self.on_message(event)
if 'body' not in push: return
logging.debug('Received push: {}'.format(push))

View File

@ -0,0 +1,30 @@
from platypush.message.event import Event
class PushbulletEvent(Event):
"""
PushBullet event object.
If you have configured the PushBullet backend with your account token,
and enabled notification mirroring on the PushBullet app on your mobile
devices, then the backend will trigger a PushbulletEvent whenever
a new notiification hits your mobile, and you can react to that event
through hooks that can, for example, log your notifications on a database,
display them on a dashboard, let the built-in text-to-speech plugin read
them out loud to you if they match the package name of your news app,
display them on your smart watch if they are pictures, and so on.
"""
def __init__(self, *args, **kwargs):
""" Platypush supports by default the PushBullet notification mirror
format, https://docs.pushbullet.com/#mirrored-notifications """
if 'type' in kwargs:
# Prevent name clash with event type attribute
kwargs['push_type'] = kwargs.pop('type')
super().__init__(*args, **kwargs)
# vim:sw=4:ts=4:et: