* PushbulletBackend.send_msg method refactored and removed pushbullet.py dependency
* Management of optional dependencies moved to setup.py
This commit is contained in:
parent
4b0706380e
commit
f7202fd205
4 changed files with 38 additions and 22 deletions
|
@ -1,19 +1,15 @@
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
|
import requests
|
||||||
import websocket
|
import websocket
|
||||||
|
|
||||||
from pushbullet import Pushbullet
|
|
||||||
|
|
||||||
from .. import Backend
|
from .. import Backend
|
||||||
|
|
||||||
class PushbulletBackend(Backend):
|
class PushbulletBackend(Backend):
|
||||||
_requires = [
|
|
||||||
'pushbullet'
|
|
||||||
]
|
|
||||||
|
|
||||||
def _init(self, token, device):
|
def _init(self, token, device):
|
||||||
self.token = token
|
self.token = token
|
||||||
self.device = device
|
self.device_name = device
|
||||||
|
self.device_id = self.get_device_id()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _on_init(ws):
|
def _on_init(ws):
|
||||||
|
@ -67,19 +63,40 @@ class PushbulletBackend(Backend):
|
||||||
|
|
||||||
self.ws.backend = self
|
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):
|
def send_msg(self, msg):
|
||||||
if isinstance(msg, dict):
|
if isinstance(msg, dict):
|
||||||
msg = json.dumps(msg)
|
msg = json.dumps(msg)
|
||||||
if not isinstance(msg, str):
|
if not isinstance(msg, str):
|
||||||
raise RuntimeError('Invalid non-JSON message')
|
raise RuntimeError('Invalid non-JSON message')
|
||||||
|
|
||||||
pb = Pushbullet(self.token)
|
response = requests.post(
|
||||||
devices = [dev for dev in pb.devices if dev.nickname == self.device]
|
u'https://api.pushbullet.com/v2/pushes',
|
||||||
if not devices:
|
headers = { 'Access-Token': self.token },
|
||||||
raise RuntimeError('No such device: {}'.format(self.device))
|
json = {
|
||||||
|
'type': 'note',
|
||||||
|
'device_iden': self.device_id,
|
||||||
|
'body': msg,
|
||||||
|
}
|
||||||
|
).json()
|
||||||
|
|
||||||
device = devices[0]
|
if 'dismissed' not in response or response['dismissed'] is True:
|
||||||
pb.push_note('', msg, device)
|
raise RuntimeError('Error while pushing the message: {}'.
|
||||||
|
format(response))
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self._init_socket()
|
self._init_socket()
|
||||||
|
|
|
@ -6,11 +6,6 @@ from phue import Bridge
|
||||||
from .. import LightPlugin
|
from .. import LightPlugin
|
||||||
|
|
||||||
class LightHuePlugin(LightPlugin):
|
class LightHuePlugin(LightPlugin):
|
||||||
""" Python dependencies """
|
|
||||||
_requires = [
|
|
||||||
'phue'
|
|
||||||
]
|
|
||||||
|
|
||||||
MAX_BRI = 255
|
MAX_BRI = 255
|
||||||
MAX_SAT = 255
|
MAX_SAT = 255
|
||||||
MAX_HUE = 65535
|
MAX_HUE = 65535
|
||||||
|
|
|
@ -5,10 +5,6 @@ from ouimeaux.environment import Environment, UnknownDevice
|
||||||
from .. import SwitchPlugin
|
from .. import SwitchPlugin
|
||||||
|
|
||||||
class SwitchWemoPlugin(SwitchPlugin):
|
class SwitchWemoPlugin(SwitchPlugin):
|
||||||
_requires = [
|
|
||||||
'ouimeaux'
|
|
||||||
]
|
|
||||||
|
|
||||||
def _init(self):
|
def _init(self):
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
|
8
setup.py
8
setup.py
|
@ -58,7 +58,15 @@ setup(
|
||||||
],
|
],
|
||||||
install_requires = [
|
install_requires = [
|
||||||
'pyyaml',
|
'pyyaml',
|
||||||
|
'requires',
|
||||||
'websocket-client',
|
'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'],
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue