Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- hub = "philips.lan" # or provide the ip address
- appname = "ipytest" # call it what you like
- username = None
- def query(query_path, body=None):
- if body is None: # Go the old route
- result = requests.get("http://" + hub + query_path)
- else:
- result = requests.post("http://" + hub + query_path, json=body)
- return result.json()
- def register():
- global username # Because we are going to modify it.
- while username is None:
- result = query("/api", {'devicetype': appname})
- status = result[0]
- error = status.get('error')
- if error:
- # Check for code 101, not authorized
- if error['type'] == 101:
- input("NOT AUTHORIZED! "
- "Press the button on your hub and hit return.")
- continue
- raise Exception("Unknown error from hub: " + str(error))
- success = status['success']
- username = success['username']
- print("* Registered as", username)
- def hue(query_path, body=None):
- register()
- full_path = "/api/" + username + query_path
- return query(full_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement