nuclearmage

pushbulletpi

Jul 24th, 2021
1,058
304 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  6. import pushybullet as pb
  7. import threading
  8. import wakeonlan
  9. import sys
  10. import time
  11. import requests
  12. import subprocess
  13.  
  14. API_KEY  = "{KEY HERE}"
  15. api = pb.PushBullet(API_KEY)
  16.  
  17. def sendWakePacket(macAddress):
  18.     wakeonlan.send_magic_packet(macAddress)
  19.     print("Magic packet sent to: " + macAddress)
  20.  
  21. def 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
  61. def 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.  
  75. def main_loop():
  76.     while 1:
  77.         monitorAPIStream()
  78.         time.sleep(0.1)
  79.  
  80. if __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.  
RAW Paste Data