From f7202fd20509d0f7f5227a29b60e7ff487f7559d Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 12 Dec 2017 20:14:02 +0100 Subject: [PATCH] * PushbulletBackend.send_msg method refactored and removed pushbullet.py dependency * Management of optional dependencies moved to setup.py --- platypush/backend/pushbullet/__init__.py | 43 ++++++++++++++++------- platypush/plugins/light/hue/__init__.py | 5 --- platypush/plugins/switch/wemo/__init__.py | 4 --- setup.py | 8 +++++ 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/platypush/backend/pushbullet/__init__.py b/platypush/backend/pushbullet/__init__.py index 3df899db43..7019fb071f 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 b33e97193c..9b1da8e56e 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 fd810f7412..c691c8408a 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 320cfc5442..357124cd4e 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'], + }, )