Advertisement
kfsone

iPython automation #2

Jun 11th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 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:  # Go the old route
  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.  
  17. def register():
  18.     global username  # Because we are going to modify it.
  19.     while username is None:
  20.         result = query("/api", {'devicetype': appname})
  21.         status = result[0]
  22.         error = status.get('error')
  23.         if error:
  24.             # Check for code 101, not authorized
  25.             if error['type'] == 101:
  26.                 input("NOT AUTHORIZED! "
  27.                       "Press the button on your hub and hit return.")
  28.                 continue
  29.             raise Exception("Unknown error from hub: " + str(error))
  30.  
  31.         success = status['success']
  32.         username = success['username']
  33.         print("* Registered as", username)
  34.  
  35. def hue(query_path, body=None):
  36.     register()
  37.     full_path = "/api/" + username + query_path
  38.     return query(full_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement