The code is pretty similar so i just paste it here, one could easily copy it and save it on their openhab setup.
That HAL idea is really awesome but almost impossible i think. Twinkly is very hard to control per LED i read so that’s a no for me. Our christmas tree is next to the TV, that’s the reason i want to dim it when we are watching tv.
So here is the code for anyone who want to use it:
import sys
import json
import urllib.request
import codecs
ARG_ON = 'on'
ARG_OFF = 'off'
ARG_STATE = 'state'
ARG_BRIGHT = 'brightness'
ARG_IP = sys.argv[1]
ARG_ACTION = sys.argv[2]
URL = "http://" + ARG_IP + "/xled/v1/"
LOGIN_URL = URL + "login"
VERIFY_URL = URL + "verify"
MODE_URL = URL + "led/mode"
BRIGHT_URL = URL + "led/out/brightness"
AUTH_HEADER = 'X-Auth-Token'
AUTHENTICATION_TOKEN = 'authentication_token'
CHALLENGE_RESPONSE = 'challenge-response'
MODE = 'mode'
MODE_ON = 'movie'
MODE_OFF = 'off'
HEADERS = {'Content-Type': 'application/json'}
LOGIN_DATA = {'challenge': 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8='}
TURN_ON_DATA = {MODE: MODE_ON}
TURN_OFF_DATA = {MODE: MODE_OFF}
if len(sys.argv) > 3: #check if the thirth argument is given, save it in a variable and create the brightness data variable
ARG_ACTION2 = int(sys.argv[3])
BRIGHTNESS_DATA = {'value': ARG_ACTION2, 'type': 'A'}
def formatData(data):
return json.dumps(data).encode('utf8')
def processRequest(request):
return urllib.request.urlopen(request)
def processRequestJSON(request):
loginResponse = processRequest(request)
reader = codecs.getreader("utf-8")
return json.load(reader(loginResponse))
# login to api - get challenge response and auth token
loginRequest = urllib.request.Request(url = LOGIN_URL, headers = HEADERS, data = formatData(LOGIN_DATA))
loginData = processRequestJSON(loginRequest)
challengeResponse = loginData[CHALLENGE_RESPONSE]
authToken = loginData[AUTHENTICATION_TOKEN]
HEADERS[AUTH_HEADER] = authToken
verifyData = {CHALLENGE_RESPONSE: challengeResponse}
# verify token by responding with challenge response
verifyRequest = urllib.request.Request(url = VERIFY_URL, headers = HEADERS, data = formatData(verifyData))
verifyData = processRequestJSON(verifyRequest)
def turnOn():
onRequest = urllib.request.Request(url = MODE_URL, headers = HEADERS, data = formatData(TURN_ON_DATA))
processRequest(onRequest)
print(1)
def turnOff():
offRequest = urllib.request.Request(url = MODE_URL, headers = HEADERS, data = formatData(TURN_OFF_DATA))
processRequest(offRequest)
print(0)
def setBrightness():
brightRequest = urllib.request.Request(url = BRIGHT_URL, headers = HEADERS, data = formatData(BRIGHTNESS_DATA))
processRequest(brightRequest)
def getState():
modeRequest = urllib.request.Request(url = MODE_URL, headers = HEADERS)
modeData = processRequestJSON(modeRequest)
if modeData[MODE] != MODE_OFF:
print(1)
else:
print(0)
if ARG_ACTION == ARG_ON:
turnOn()
elif ARG_ACTION == ARG_OFF:
turnOff()
elif ARG_ACTION == ARG_STATE:
getState()
elif ARG_ACTION == ARG_BRIGHT:
setBrightness()
Use it like this:
python3 twinkly.py 192.168.x.x on
Turn Twinkly on
python3 twinkly.py 192.168.x.x off
Turn Twinkly off
python3 twinkly.py 192.168.x.x brightness 50
Set brightness to 50