Stop polluting Pushbullet messages with StopEvent JSON whenever the backend stops.

This commit is contained in:
Fabio Manganiello 2020-01-11 12:31:32 +01:00
parent b0a5f1e272
commit 3daf39e3e0

View file

@ -41,6 +41,7 @@ class PushbulletBackend(Backend):
:type proxy_port: int :type proxy_port: int
""" """
# noinspection PyPackageRequirements
from pushbullet import Pushbullet from pushbullet import Pushbullet
super().__init__(**kwargs) super().__init__(**kwargs)
@ -51,6 +52,7 @@ class PushbulletBackend(Backend):
self.pb = Pushbullet(token) self.pb = Pushbullet(token)
self.listener = None self.listener = None
# noinspection PyBroadException
try: try:
self.device = self.pb.get_device(self.device_name) self.device = self.pb.get_device(self.device_name)
except: except:
@ -79,7 +81,8 @@ class PushbulletBackend(Backend):
push = self._get_latest_push() push = self._get_latest_push()
elif data['type'] == 'push': elif data['type'] == 'push':
push = data['push'] push = data['push']
else: return # Not a push notification else:
return # Not a push notification
if not push: if not push:
return return
@ -89,7 +92,8 @@ class PushbulletBackend(Backend):
event = PushbulletEvent(**push) event = PushbulletEvent(**push)
self.on_message(event) self.on_message(event)
if 'body' not in push: return if 'body' not in push:
return
self.logger.debug('Received push: {}'.format(push)) self.logger.debug('Received push: {}'.format(push))
body = push['body'] body = push['body']
@ -98,8 +102,8 @@ class PushbulletBackend(Backend):
self.on_message(body) self.on_message(body)
except Exception as e: except Exception as e:
self.logger.debug(('Unexpected message received on the ' + self.logger.debug(('Unexpected message received on the ' +
'Pushbullet backend: {}. Message: {}') 'Pushbullet backend: {}. Message: {}')
.format(str(e), body)) .format(str(e), body))
except Exception as e: except Exception as e:
self.logger.exception(e) self.logger.exception(e)
@ -108,9 +112,10 @@ class PushbulletBackend(Backend):
return callback return callback
def get_device_id(self): def get_device_id(self):
# noinspection PyBroadException
try: try:
return self.pb.get_device(self.device_name).device_iden return self.pb.get_device(self.device_name).device_iden
except Exception as e: except Exception:
device = self.pb.new_device(self.device_name, model='Platypush virtual device', device = self.pb.new_device(self.device_name, model='Platypush virtual device',
manufacturer='platypush', icon='system') manufacturer='platypush', icon='system')
@ -119,11 +124,6 @@ class PushbulletBackend(Backend):
return device.device_iden return device.device_iden
def send_message(self, msg):
if isinstance(msg, dict):
msg = json.dumps(msg)
self.device.push_note(title=None, body=str(msg))
def close(self): def close(self):
if self.listener: if self.listener:
self.listener.close() self.listener.close()
@ -132,11 +132,12 @@ class PushbulletBackend(Backend):
return self.close() return self.close()
def run(self): def run(self):
# noinspection PyPackageRequirements
from pushbullet import Listener from pushbullet import Listener
super().run() super().run()
self.logger.info('Initialized Pushbullet backend - device_id: {}' self.logger.info('Initialized Pushbullet backend - device_id: {}'
.format(self.device_name)) .format(self.device_name))
self.listener = Listener(account=self.pb, on_push=self.on_push(), self.listener = Listener(account=self.pb, on_push=self.on_push(),
http_proxy_host=self.proxy_host, http_proxy_host=self.proxy_host,
@ -145,4 +146,3 @@ class PushbulletBackend(Backend):
self.listener.run_forever() self.listener.run_forever()
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: