Created
February 18, 2014 18:45
-
-
Save cees-elzinga/9077169 to your computer and use it in GitHub Desktop.
Philips Hue register new device
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 socket | |
import random | |
import hashlib | |
import urllib | |
import urllib2 | |
import time | |
import sys | |
port = 1900 | |
ip = "239.255.255.250" | |
address = (ip, port) | |
data = """M-SEARCH * HTTP/1.1 | |
HOST: %s:%s | |
MAN: ssdp:discover | |
MX: 3 | |
ST: upnp:rootdevice""" % (ip, port) | |
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
hue_ip = None | |
num_retransmits = 0 | |
while(num_retransmits < 10) and hue_ip == None: | |
num_retransmits += 1 | |
client_socket.sendto(data, address) | |
recv_data, addr = client_socket.recvfrom(2048) | |
if "IpBridge" in recv_data and "description.xml" in recv_data: | |
hue_ip = recv_data.split("LOCATION: http://")[1].split(":")[0] | |
time.sleep(1) | |
if not hue_ip: | |
print " [!] Could not locate IpBridge, quitting" | |
sys.exit(1) | |
print " [*] Found IpBridge at: %s" % hue_ip | |
username = hashlib.md5(str(random.random())).hexdigest() | |
device = "python-app-%s" % username[:5] | |
data = '{"username": "%s", "devicetype": "%s"}' % (username, device) | |
# use urllib2 as it's included in Python | |
response = urllib2.urlopen('http://%s/api' % hue_ip, data) | |
response = response.read() | |
while "link button not pressed" in response: | |
print " [*] Press button on bridge to register new device" | |
response = urllib2.urlopen('http://%s/api' % hue_ip, data) | |
response = response.read() | |
time.sleep(1) | |
print """ [*] New device is registered | |
Username: %s | |
Device: %s | |
""" % (username, device) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment