Skip to content

Instantly share code, notes, and snippets.

@davlgd
Created December 29, 2018 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davlgd/efdd217e9a27711176968bce084e5b8d to your computer and use it in GitHub Desktop.
Save davlgd/efdd217e9a27711176968bce084e5b8d to your computer and use it in GitHub Desktop.
Exercice de contrôle des Philips Hue en Python
import requests
import json
bridge_ip = "IP_du_pont"
username = "votre_jeton"
light_number = "ID_de_la_lampe"
lights_url = "http://{}/api/{}/lights".format(bridge_ip, username)
light_state_url = "{}/{}/state".format(lights_url, light_number)
print lights_url
print light_state_url
r = requests.get(lights_url)
for light in r.json():
light_current_status = "{} - {} : {}".format(light,
r.json()[light]["name"].encode("utf-8"),
r.json()[light]["state"]["on"])
print light_current_status
new_state = not r.json()[light_number]["state"]["on"]
message = json.dumps({"on": new_state})
action = requests.put(light_state_url, data=message)
print action.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment