diff --git a/platypush/backend/pushbullet/__init__.py b/platypush/backend/pushbullet/__init__.py index 3df899db..7019fb07 100644 --- a/platypush/backend/pushbullet/__init__.py +++ b/platypush/backend/pushbullet/__init__.py @@ -1,19 +1,15 @@ import logging import json +import requests import websocket -from pushbullet import Pushbullet - from .. import Backend class PushbulletBackend(Backend): - _requires = [ - 'pushbullet' - ] - def _init(self, token, device): self.token = token - self.device = device + self.device_name = device + self.device_id = self.get_device_id() @staticmethod def _on_init(ws): @@ -67,19 +63,40 @@ class PushbulletBackend(Backend): self.ws.backend = self + def get_device_id(self): + response = requests.get( + u'https://api.pushbullet.com/v2/devices', + headers = { 'Access-Token': self.token }, + ).json() + + devices = [dev for dev in response['devices'] if 'nickname' in dev + and dev['nickname'] == self.device_name] + + if not devices: + raise RuntimeError('No such Pushbullet device: {}' + .format(self.device_name)) + + return devices[0]['iden'] + def send_msg(self, msg): if isinstance(msg, dict): msg = json.dumps(msg) if not isinstance(msg, str): raise RuntimeError('Invalid non-JSON message') - pb = Pushbullet(self.token) - devices = [dev for dev in pb.devices if dev.nickname == self.device] - if not devices: - raise RuntimeError('No such device: {}'.format(self.device)) + response = requests.post( + u'https://api.pushbullet.com/v2/pushes', + headers = { 'Access-Token': self.token }, + json = { + 'type': 'note', + 'device_iden': self.device_id, + 'body': msg, + } + ).json() - device = devices[0] - pb.push_note('', msg, device) + if 'dismissed' not in response or response['dismissed'] is True: + raise RuntimeError('Error while pushing the message: {}'. + format(response)) def run(self): self._init_socket() diff --git a/platypush/plugins/light/hue/__init__.py b/platypush/plugins/light/hue/__init__.py index b33e9719..9b1da8e5 100644 --- a/platypush/plugins/light/hue/__init__.py +++ b/platypush/plugins/light/hue/__init__.py @@ -6,11 +6,6 @@ from phue import Bridge from .. import LightPlugin class LightHuePlugin(LightPlugin): - """ Python dependencies """ - _requires = [ - 'phue' - ] - MAX_BRI = 255 MAX_SAT = 255 MAX_HUE = 65535 diff --git a/platypush/plugins/switch/wemo/__init__.py b/platypush/plugins/switch/wemo/__init__.py index fd810f74..c691c840 100644 --- a/platypush/plugins/switch/wemo/__init__.py +++ b/platypush/plugins/switch/wemo/__init__.py @@ -5,10 +5,6 @@ from ouimeaux.environment import Environment, UnknownDevice from .. import SwitchPlugin class SwitchWemoPlugin(SwitchPlugin): - _requires = [ - 'ouimeaux' - ] - def _init(self): logging.basicConfig(level=logging.INFO) diff --git a/setup.py b/setup.py index 320cfc54..357124cd 100755 --- a/setup.py +++ b/setup.py @@ -58,7 +58,15 @@ setup( ], install_requires = [ 'pyyaml', + 'requires', 'websocket-client', ], + extras_require = { + 'Support for Apache Kafka backend': ['kafka-python'], + 'Support for Pushbullet backend': ['requests', 'websocket-client'], + 'Support for Philips Hue plugin': ['phue'], + 'Support for MPD/Mopidy music server plugin': ['mpd'], + 'Support for Belkin WeMo Switch plugin': ['ouimeaux'], + }, )