Advertisement
kfsone

iPython automation #3

Jun 12th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import requests
  2.  
  3. hub      = "philips.lan"  # or provide the ip address
  4. appname  = "ipytest"      # call it what you like
  5.  
  6. username = None
  7.  
  8.  
  9. def query(query_path, body=None):
  10.     if body is None:
  11.         result = requests.get("http://" + hub + query_path)
  12.     else:
  13.         result = requests.post("http://" + hub + query_path, json=body)
  14.     return result.json()
  15.  
  16. def update(query_path, body=None):
  17.     result = requests.put("http://" + hub + query_path, json=body)
  18.     return result.json()
  19.  
  20. def register():
  21.     global username  # Because we are going to modify it.
  22.     while username is None:
  23.         result = query("/api", {'devicetype': appname})
  24.         status = result[0]
  25.         error = status.get('error')
  26.         if error:
  27.             # Check for code 101, not authorized
  28.             if error['type'] == 101:
  29.                 input("NOT AUTHORIZED! "
  30.                       "Press the button on your hub and hit return.")
  31.                 continue
  32.             raise Exception("Unknown error from hub: " + str(error))
  33.  
  34.         success = status['success']
  35.         username = success['username']
  36.         print("* Registered as", username)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement