I’m using the Twinkly v1 and tried to implement it into OH.
My approach is not via python, but via the OH rules.
For me the use case was mostly turning it OFF and ON.
Started with the dimmer part, but somehow the commands are not working yet.
I also stopped using the twinkly app, because it invalidates the token used in OH
If anyone is interested, here are my files:
twinkly.items:
String TW_Token
Switch TW_TokenValid
Switch TW_Power
twinkly.rules:
var String twinklyAddress = "http://ip.of.your.twinkly/xled/v1"
var Timer renewTokenTimer
var String token
rule "Token expiration"
when
System started or
Item TW_TokenValid changed to OFF
then
if (renewTokenTimer !== null) {
renewTokenTimer.cancel
renewTokenTimer = null
}
logDebug("rules.twinkly", "twinkly address: {}", twinklyAddress)
val loginResponse = sendHttpPostRequest(twinklyAddress + "/login", "application/json", "{\"challenge\": \"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=\"}")
logDebug("rules.twinkly", "login response: {}", loginResponse)
token = transform("JSONPATH", "$.authentication_token", loginResponse)
TW_Token.postUpdate(token)
val String challengeResponse = transform("JSONPATH", "$.challenge_response", loginResponse)
val String tokenExpiration = transform("JSONPATH", "$.authentication_token_expires_in", loginResponse)
val headers = newHashMap("X-Auth-Token" -> token)
logDebug("rules.twinkly", "headers: {}", headers)
val verifyResponse = sendHttpPostRequest(twinklyAddress + "/verify", "application/json", "{\"challenge-response\": \"" + challengeResponse + "\"}", headers, 1000)
logDebug("rules.twinkly", "verify response: {}", verifyResponse)
renewTokenTimer = createTimer(now.plusSeconds(Integer.parseInt(tokenExpiration))) [|
TW_TokenValid.sendCommand(OFF)
]
TW_TokenValid.postUpdate(ON)
end
rule "Turn on christmas lights"
when
Item TW_Power changed to ON
then
logInfo("rules.twinkly", "Turn christmas lights on")
if (token === null) {
TW_TokenValid.sendCommand(OFF)
}
val headers = newHashMap("X-Auth-Token" -> token)
logInfo("rules.twinkly", "headers: {}", headers)
val response = sendHttpPostRequest(twinklyAddress + "/led/mode", "application/json", "{\"mode\": \"movie\"}", headers, 1000)
logInfo("rules.twinkly", "response: {}", response)
logInfo("rules.twinkly", "Christmas lights are on")
val brightnessResponse = sendHttpGetRequest(twinklyAddress + "/led/out/brightness", headers, 1000)
logInfo("rules.twinkly", "brightness response: {}", brightnessResponse)
TW_Brightness.postUpdate(transform("JSONPATH", "$.value", brightnessResponse))
end
rule "Turn off christmas lights"
when
Item TW_Power changed to OFF
then
logInfo("rules.twinkly", "Turn christmas lights off")
if (token === null) {
TW_TokenValid.sendCommand(OFF)
}
val headers = newHashMap("X-Auth-Token" -> token)
val response = sendHttpPostRequest(twinklyAddress + "/led/mode", "application/json", "{\"mode\": \"off\"}", headers, 1000)
logInfo("rules.twinkly", "response: {}", response)
logInfo("rules.twinkly", "Christmas lights are off")
// update brightness value
TW_Brightness.postUpdate(0)
end
Using the switch TW_Power turns your twinkly on / off