02.02.2022, 13:17
Has anyone already made any code for the Philips Hue api V2?
Why the v2 api? because i want to trigger dynamic scenes and thats not possible in the v1 api.
Please correct my code is you see improvements.
Why the v2 api? because i want to trigger dynamic scenes and thats not possible in the v1 api.
Please correct my code is you see improvements.
Code:
--[[
https://developers.meethue.com/develop/hue-api-v2/api-reference/
https://developers.meethue.com/develop/hue-api-v2/getting-started/
--]]
-- Load modules
https = require('ssl.https')
ltn12 = require('ltn12')
json = require('json')
local bridge = 'ip of the bridge'
local usr = 'same user as api v1'
-- bridge communication base
function hueExec(iMethod, iCommand, iBody)
resp = {}
body = json.encode(iBody)
res, code, headers = https.request({
url = 'https://'..bridge..'/clip/v2/resource'..iCommand,
method = iMethod,
source = ltn12.source.string(body),
sink = ltn12.sink.table(resp),
headers = { ['hue-application-key'] = usr,
['Content-Type'] = 'application/json',
['Accept'] = 'application/json',
['Content-Length'] = #body
}
})
-- handle feedback
if not res or code ~= 200 then
return
else
resp = table.concat(resp)
resp = json.pdecode(resp)
return resp
end
end
-- get all lights
function getLights()
return hueExec('GET', '/light')
end
--log(getLights())
-- get light
function getLight(iLight)
return hueExec('GET', '/light/'..iLight)
end
--log(getLight('e4a834c9-9ca3-4f2d-98be-669c55a90b5d'))
-- activate scene (active or dynamic_palette)
function runScene(iScene, iAction)
vAction = iAction or 'active'
hueExec('PUT', '/scene/'..iScene, {recall = {action = vAction}})
end
--runScene('185d4226-213e-4f01-913b-91c22d85bfac') -- living soho scene
--runScene('185d4226-213e-4f01-913b-91c22d85bfac', 'dynamic_palette') -- living soho dynamic