Created
December 29, 2018 09:58
-
-
Save davlgd/efdd217e9a27711176968bce084e5b8d to your computer and use it in GitHub Desktop.
Exercice de contrôle des Philips Hue en Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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