From 694d7ca62d837418028cbfd9de9dcfde3e177063 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 7 Jan 2018 00:58:03 +0100 Subject: [PATCH] Support for PushBullet events on notifications, #36 --- platypush/backend/pushbullet/__init__.py | 8 +++++++ platypush/message/event/pushbullet.py | 30 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 platypush/message/event/pushbullet.py diff --git a/platypush/backend/pushbullet/__init__.py b/platypush/backend/pushbullet/__init__.py index ccba3958f0..d58fedcbf1 100644 --- a/platypush/backend/pushbullet/__init__.py +++ b/platypush/backend/pushbullet/__init__.py @@ -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)) diff --git a/platypush/message/event/pushbullet.py b/platypush/message/event/pushbullet.py new file mode 100644 index 0000000000..28416fa71e --- /dev/null +++ b/platypush/message/event/pushbullet.py @@ -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: +