Skip to content

Instantly share code, notes, and snippets.

@n30m1nd
Last active April 18, 2019 11:39
Show Gist options
  • Save n30m1nd/4c983035bff09114bca4db2c165091d7 to your computer and use it in GitHub Desktop.
Save n30m1nd/4c983035bff09114bca4db2c165091d7 to your computer and use it in GitHub Desktop.
import requests
from sys import argv
from random import randint
from time import sleep
headers = {
'Host': '192.168.1.39',
'Connection': 'close',
'Accept': '*/*',
'User-Agent': 'HueHueHue',
'Accept-Language': 'es-es',
'Accept-Encoding': 'gzip, deflate',
}
hue_hub="192.168.1.39" # The IP of your hub
api_key="YOUR_API_KEY" # See http://rsmck.co.uk/hue
def change_light_state(brightness, hue, saturation, transitiontime, on_off, light_number):
data = '''{
"bri" : %s,
"hue" : %s,
"sat" : %s,
"transitiontime" : %s,
"on" : %s
}
''' % (brightness, hue, saturation, transitiontime, on_off)
print(
"""[+] Enviando comando:
Brillo: %s
Color: %s
Saturacion: %s
Tiempo Transicion: %s
Encender: %s
Luz Numero: %s
""") % (brightness, hue, saturation, transitiontime, on_off, light_number)
response = requests.put('http://%s/api/%s/lights/%s/state' % (hue_hub, api_key, light_number), headers=headers, data=data, verify=False)
def christmas_light_single(light_number):
while True:
change_light_state(randint(1,200), randint(0,50000), randint(0,200), 0, "true", light_number)
sleep(0.5)
def christmas_lights(number_of_lights):
# Fires up the whole house with the same light patterns
while True:
(brightness, hue, saturation) = (randint(1,200), randint(0,50000), randint(0,200))
for i in range(1, number_of_lights+1):
change_light_state(brightness, hue, saturation, 0, "true", i)
sleep(0.5)
def shutdown_single_light(light_number):
change_light_state(0, 0, 0, 2, "false", light_number)
def shutdown_house(number_of_lights):
for i in range(1, number_of_lights+1):
shutdown_single_light(i)
def turn_on_single_light(light_number):
change_light_state(200, 0, 0, 2, "true", light_number)
def lightup_house(number_of_lights):
for i in range(1, number_of_lights+1):
turn_on_single_light(i)
# Examples of use:
# Turn on (encender) or off (apagar)
# philips-hue-control.py luz apagar 2
# philips-hue-control.py luz encender 2
# Turn on the light number 1 in red color:
# philips-hue-control.py luz 200 0 200 5 true 1
# Set christmas lights (it's hardcoded to 4 lights only, my home :) ):
# philips-hue-control.py casa navidad
# Set single christmas light:
# philips-hue-control-py luz navidad 1
if __name__ == "__main__":
if len(argv) < 2:
print("[-] Error en el uso, especifica luz o casa")
exit(1)
if argv[1] == "casa":
if argv[2] == "navidad":
christmas_lights(10)
elif argv[2] == "apagar":
shutdown_house(10)
elif argv[2] == "encender":
lightup_house(10)
elif argv[1] == "luz":
if len(argv) < 3:
print("[-] Error en el uso:\n[i] Ejemplos: %s luz [apagar o numero_brillo] ... \n" % argv[0])
exit(1)
if argv[2] == "apagar":
shutdown_single_light(argv[3])
elif argv[2] == "encender":
turn_on_single_light(argv[3])
elif argv[2] == "navidad":
christmas_light_single(argv[3])
elif len(argv) < 8:
print("[-] Error en el uso:\n[i] Ejemplo: %s luz BRILLO(0,200) COLOR(0,100000) SATURACION(0,200) TIEMPO_TRANSICION(0,5) APAGAR/ENCENDER(true,false) NUMERO_LUZ(1,4)" % argv[0])
else:
change_light_state(argv[2], argv[3], argv[4], argv[5], argv[6], argv[7])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment