· 4 years ago · Jul 24, 2021, 05:34 PM
1#https://github.com/kstep/pushybullet
2#https://pypi.python.org/pypi/websocket-client
3#https://pypi.python.org/pypi/websocket/0.2.1
4
5
6import pushybullet as pb
7import threading
8import wakeonlan
9import sys
10import time
11import requests
12import subprocess
13
14API_KEY = "{KEY HERE}"
15api = pb.PushBullet(API_KEY)
16
17def sendWakePacket(macAddress):
18 wakeonlan.send_magic_packet(macAddress)
19 print("Magic packet sent to: " + macAddress)
20
21def monitorAPIStream():
22 for event in api.stream():
23 for push in event.pushes(skip_empty=True):
24 if (push.dismissed or push.sender_iden != '{ID HERE}'): #if push dismissed or not from me, return
25 return
26 print("Received Push from "+ push.sender_email + ": " + push.body)
27
28 if (push.body.lower().strip() == "wake htpc"):
29 wakeThread = threading.Thread(target=sendWakePacket('{MAC Address}'))
30 wakeThread.start()
31 reply = pb.NotePush('Wake packet sent to HTPC')
32 api.push(reply, push.source_device)
33
34 elif (push.body.lower().strip() == "wake desktop"):
35 wakeThread = threading.Thread(target=sendWakePacket('{MAC Address}'))
36 wakeThread.start()
37 reply = pb.NotePush('Wake packet sent to Desktop')
38 api.push(reply, push.source_device)
39
40 elif (push.body.lower().strip() == "ping"):
41 reply = pb.NotePush('Ping received. Script running.')
42 api.push(reply, push.source_device)
43
44 elif (push.body.lower().strip() == "fan on"):
45 headers = {'Content-type': 'application/json',}
46 data = '{"method":"passthrough", "params": {"deviceId": "{ID}", "requestData": "{\\"system\\":{\\"set_relay_state\\":{\\"state\\":1}}}" }}'
47 response = requests.post('{End Point}', headers=headers, data=data)
48 print (response.text)
49 reply = pb.NotePush('Turning fan on')
50 api.push(reply, push.source_device)
51#
52 elif (push.body.lower().strip() == "fan off"):
53 headers = {'Content-type': 'application/json',}
54 data = '{"method":"passthrough", "params": {"deviceId": "{ID}", "requestData": "{\\"system\\":{\\"set_relay_state\\":{\\"state\\":0}}}" }}'
55 response = requests.post('{EndPoint}', headers=headers, data=data)
56 print (response.text)
57 reply = pb.NotePush('Turning fan off')
58 api.push(reply, push.source_device)
59
60#Ensure Rasppi device exists
61def checkDevices():
62 try:
63 devices = api.devices()
64 device = api['Rasppi']
65 print("Rasppi device exists.")
66 except KeyError:
67 print("Rasppi device does not exist in Pushbullet. Creating it.")
68 device = api.create_device('Rasppi')
69 print("Rasppi device created.")
70
71
72
73
74
75def main_loop():
76 while 1:
77 monitorAPIStream()
78 time.sleep(0.1)
79
80if __name__ == '__main__':
81 try:
82 queryConfigFile()
83 checkDevices()
84 main_loop()
85 except KeyboardInterrupt:
86 print >> sys.stderr, '\nExiting by user request.\n'
87 sys.exit(0)
88