I kinda like the library
Requests, so I would suggest you keep using Python, if you want to control it using AHK, you can parse it parameters when you run it.
The AHK Script:
Code: Select all
#Include StdOutToVar.ahk
MsgBox, % LIFX_SetState("all", "on", "white kelvin:4500", 1.0, 0.1)
LIFX_SetState(selector, power, color, brightness, duration){
return StdOutStream("python LIFX.py " """" selector """ """ power """ """ color """ """ brightness """ """ duration """")
}
The Python Script:
Code: Select all
import requests, sys
def LIFX_SetState(selector, power, color, brightness, duration):
try:
s = requests.Session()
headers = {"Authorization": "Bearer " + "xxxxxxxxxxxxxxxxxxxxxxxxxx"}
payload = {"power": power, "duration": duration, "color": color, "brightness": brightness}
response = s.put("https://api.lifx.com/v1/lights/" + selector + "/state", data = payload, headers = headers)
return JsonToStr(response)
except:
return "Error"
def JsonToStr(response):
res = ""
for key in response:
res += key + ": ["
for light in response[key]:
res += "{"
for key in light:
res += key + ": " + light[key] + ", "
res = res[0:-2] + "},"
res = res[0:-1] + "],\n"
return res[0:-2]
print(LIFX_SetState(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]), end = "")
You will also need this Script by Sean, HotKeyIt and SKAN.
Code: Select all
StdOutStream(sCmd, Callback = "") { ; Modified : SKAN 31-Aug-2013 http://goo.gl/j8XJXY
Static StrGet := "StrGet" ; Thanks to : HotKeyIt http://goo.gl/IsH1zs
; Original : Sean 20-Feb-2007 http://goo.gl/mxCdn
DllCall("CreatePipe", "UIntP", hPipeRead, "UIntP", hPipeWrite, "UInt", 0, "UInt", 0)
DllCall("SetHandleInformation", "UInt", hPipeWrite, "UInt", 1, "UInt", 1)
VarSetCapacity(STARTUPINFO, A_Is64bitOS ? 104 : 68, 0) ; STARTUPINFO ; http://goo.gl/fZf24
NumPut(68, STARTUPINFO, 0) ; cbSize
NumPut(0x100, STARTUPINFO, A_Is64bitOS ? 60 : 44) ; dwFlags => STARTF_USESTDHANDLES = 0x100
NumPut(hPipeWrite, STARTUPINFO, A_Is64bitOS ? 88 : 60) ; hStdOutput
NumPut(hPipeWrite, STARTUPINFO, A_Is64bitOS ? 96 : 64) ; hStdError
VarSetCapacity(PROCESS_INFORMATION, A_Is64bitOS ? 32 : 16) ; PROCESS_INFORMATION ; http://goo.gl/b9BaI
If(!DllCall("CreateProcess", "UInt", 0, "UInt", &sCmd, "UInt", 0, "UInt", 0 ; http://goo.gl/USC5a
, "UInt", 1, "UInt", 0x08000000, "UInt", 0, "UInt", 0
, "UInt", &STARTUPINFO, "UInt", &PROCESS_INFORMATION)){
Return ""
, DllCall("CloseHandle", "UInt", hPipeWrite)
, DllCall("CloseHandle", "UInt", hPipeRead)
, DllCall("SetLastError", "Int", -1)
}
hProcess := NumGet(PROCESS_INFORMATION, 0)
hThread := NumGet(PROCESS_INFORMATION, A_Is64bitOS ? 8 : 4)
DllCall("CloseHandle", "UInt", hPipeWrite)
AIC := (SubStr(A_AhkVersion, 1, 3) = "1.0") ; A_IsClassic
VarSetCapacity(Buffer, 4096, 0), nSz := 0
While DllCall("ReadFile", "UInt", hPipeRead, "UInt", &Buffer, "UInt", 4094, "UIntP", nSz, "Int", 0) {
tOutput := (AIC && NumPut(0, Buffer, nSz, "Char") && VarSetCapacity(Buffer, -1)) ? Buffer : %StrGet%(&Buffer, nSz, "CP850")
Isfunc(Callback) ? %Callback%(tOutput, A_Index) : sOutput .= tOutput
}
DllCall("GetExitCodeProcess", "UInt", hProcess, "UIntP", ExitCode)
DllCall("CloseHandle", "UInt", hProcess)
DllCall("CloseHandle", "UInt", hThread)
DllCall("CloseHandle", "UInt", hPipeRead)
DllCall("SetLastError", "UInt", ExitCode)
Return Isfunc(Callback) ? %Callback%("", 0) : sOutput
}
A little disclaimer, since I neither have a LIFX bulb nor an access token I haven't been able to test this, but I have been using the same setup
https://autohotkey.com/boards/viewtopic ... 609#p93609.
Please excuse my spelling I am dyslexic.