* PushbulletBackend.send_msg method refactored and removed pushbullet.py dependency

* Management of optional dependencies moved to setup.py
This commit is contained in:
Fabio Manganiello 2017-12-12 20:14:02 +01:00
parent 4b0706380e
commit f7202fd205
4 changed files with 38 additions and 22 deletions

View file

@ -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()

View file

@ -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

View file

@ -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)

View file

@ -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'],
},
)