* If we get a push tickle on PushBullet, get that latest push

* Fixed device_id attribute name clash
This commit is contained in:
Fabio Manganiello 2017-12-13 23:55:38 +01:00
parent dfb4620e0a
commit ee5b8e21a0
2 changed files with 37 additions and 13 deletions

View file

@ -47,10 +47,9 @@ class Backend(Thread):
return isinstance(self, LocalBackend) return isinstance(self, LocalBackend)
def on_msg(self, msg): def on_msg(self, msg):
if 'target' not in msg: if 'target' not in msg: return # No target
return # No target
target = msg.pop('target') target = msg.pop('target')
if target != self.device_id and not self.is_local(): if target != self.device_id and not self.is_local():
return # Not for me return # Not for me

View file

@ -1,6 +1,7 @@
import logging import logging
import json import json
import requests import requests
import time
import websocket import websocket
from .. import Backend from .. import Backend
@ -9,7 +10,7 @@ class PushbulletBackend(Backend):
def _init(self, token, device): def _init(self, token, device):
self.token = token self.token = token
self.device_name = device self.device_name = device
self.device_id = self.get_device_id() self.pb_device_id = self.get_device_id()
@staticmethod @staticmethod
def _on_init(ws): def _on_init(ws):
@ -29,6 +30,30 @@ class PushbulletBackend(Backend):
self.ws.close() self.ws.close()
self._init_socket() self._init_socket()
def _get_latest_push(self):
t = int(time.time()) - 2
try:
response = requests.get(
u'https://api.pushbullet.com/v2/pushes',
headers = { 'Access-Token': self.token },
params = {
'modified_after': str(t),
'active' : 'true',
'limit' : 1,
}
)
response = response.json()
except Exception as e:
logging.exception(e)
raise e
if 'pushes' in response and response['pushes']:
return response['pushes'][0]
else:
return {}
def _on_push(self, data): def _on_push(self, data):
try: try:
data = json.loads(data) if isinstance(data, str) else push data = json.loads(data) if isinstance(data, str) else push
@ -36,20 +61,20 @@ class PushbulletBackend(Backend):
logging.exception(e) logging.exception(e)
return return
if data['type'] != 'push': 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 return # Not a push notification
push = data['push']
logging.debug('Received push: {}'.format(push)) logging.debug('Received push: {}'.format(push))
if 'body' not in push: if 'body' not in push: return
return
body = push['body'] body = push['body']
try: try: body = json.loads(body)
body = json.loads(body) except ValueError as e: return
except ValueError as e:
return
self.on_msg(body) self.on_msg(body)
@ -89,7 +114,7 @@ class PushbulletBackend(Backend):
headers = { 'Access-Token': self.token }, headers = { 'Access-Token': self.token },
json = { json = {
'type': 'note', 'type': 'note',
'device_iden': self.device_id, 'device_iden': self.pb_device_id,
'body': msg, 'body': msg,
} }
).json() ).json()