Created
January 28, 2018 06:48
-
-
Save kershner/4c48c1d3f7d95b5fbd625f8bfb5ff7e4 to your computer and use it in GitHub Desktop.
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
from socket import socket, AF_INET, SOCK_DGRAM | |
from dtls import do_patch | |
import requests | |
import binascii | |
import sslpsk | |
import json | |
BRIDGE_IP = 'your_bridge_ip' | |
VALID_API_VERSION = 1.22 | |
USERNAME = 'your_hue_username' | |
CLIENT_KEY = 'your_hue_client_key' | |
STREAMING_PORT = 2100 | |
# Check current version of Hue bridge, needs to be > 1.22 for Entertainment stuff | |
def check_bridge_version(ip): | |
url = 'http://%s/api/config' % ip | |
r = requests.get(url).json() | |
api_version_raw = r['apiversion'] | |
final_period = api_version_raw.rfind('.') | |
api_version = float(api_version_raw[0:final_period]) | |
if api_version >= VALID_API_VERSION: | |
return True | |
return False | |
def get_entertainment_groups(ip): | |
url = 'http://%s/api/%s/groups' % (ip, USERNAME) | |
r = requests.get(url) | |
groups = r.json() | |
entertainment_groups = {} | |
for group_id in groups: | |
group = groups[group_id] | |
if group['type'] == 'Entertainment': | |
entertainment_groups[group_id] = group | |
return entertainment_groups | |
def toggle_group_streaming(group_id, state): | |
url = 'http://%s/api/%s/groups/%s' % (BRIDGE_IP, USERNAME, group_id) | |
data = { | |
'stream': { | |
'active': state | |
} | |
} | |
body = json.dumps(data) | |
r = requests.put(url, data=body, timeout=5) | |
print r.json() | |
def dtls_handshake(): | |
psk = bin(int(binascii.hexlify(CLIENT_KEY), 16)) | |
do_patch() | |
s = socket(AF_INET, SOCK_DGRAM) | |
s.connect((BRIDGE_IP, STREAMING_PORT)) | |
sock = sslpsk.wrap_socket(s, | |
psk=psk, | |
hint=USERNAME, | |
ciphers='PSK-AES128-CBC-SHA') | |
message = 'ping' | |
sock.sendto(message, (BRIDGE_IP, STREAMING_PORT)) | |
msg = s.recv(4) | |
print 'Client received: %s' % msg | |
def entertainment_registration(ip): | |
url = 'http://%s/api/' % ip | |
data = { | |
'devicetype': 'ScreenBloom', | |
'generateclientkey': True | |
} | |
body = json.dumps(data) | |
r = requests.post(url, data=body, timeout=5) | |
if __name__ == '__main__': | |
if check_bridge_version(BRIDGE_IP): | |
print 'Bridge API version is OK!' | |
# entertainment_registration(BRIDGE_IP) | |
GROUPS = get_entertainment_groups(BRIDGE_IP) | |
toggle_group_streaming(5, True) | |
dtls_handshake() | |
toggle_group_streaming(5, False) | |
else: | |
print 'Bridge API version is not recent enough for Hue Entertainment.' | |
print 'Make sure you\'re using a 2.0 or greater Bridge (square) and are running the latest firmware!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment