05.11.2024, 07:18
(04.11.2024, 14:17)Fcs Wrote: Hi @gjniewenhuijse,
Have you been able to make any progress on using api V2? like you, I need to use dynamic scenes and motion sensors.
thanks you
I made some code to use the api v2
Code:
-- hue v2 api begin --------------------------------------------------------------------------------------------------
--[[
https://developers.meethue.com/develop/hue-api-v2/api-reference/
https://developers.meethue.com/develop/hue-api-v2/getting-started/
--]]
-- bridge communication base
function hueExec(iMethod, iCommand, iBody)
-- load modules
https = require('ssl.https')
ltn12 = require('ltn12')
json = require('json')
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 res,code
else
return json.pdecode(table.concat(resp))
end
end
-- get all lights
function getLights()
return hueExec('GET', '/light')
end
--log(getLights().data)
-- get light
function getLight(iLight)
return hueExec('GET', '/light/'..iLight)
end
--log(getLight('e4a916c9-9ca3-4f3d-98be-779c55a90b5d'))
-- (api v2) activate scene (active or dynamic_palette)
function runScene(iScene, iAction)
vAction = iAction or 'active'
hueExec('PUT', '/scene/'..iScene, {recall = {action = vAction}})
end
--runScene('185d4336-273e-4f09-913c-91c76d85bfac') -- woonkamer soho
--runScene('185d4336-273e-4f09-913c-91c76d85bfac', 'dynamic_palette') -- woonkamer soho dynamic
-- hue v2 api end --------------------------------------------------------------------------------------------------
and tested with the event handler
Code:
--[[
https://developers.meethue.com/develop/hue-api-v2/migration-guide-to-the-new-hue-api/#Event%20Stream
--]]
-- load modules
json = require('json')
if not sock then
host = 'x.x.x.x'
port = 443
proto = 'tlsv12'
path = '/eventstream/clip/v2'
appkey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
require('ssl')
sock = require('socket').tcp()
sock:settimeout(10)
res, err = sock:connect(host, port)
if res then
sock = ssl.wrap(sock, proto)
res, err = sock:dohandshake()
if res then
sock:send(
'GET ' .. path .. ' HTTP/1.1\r\n' ..
'Host: ' .. host .. '\r\n' ..
'Accept: text/event-stream\r\n' ..
'hue-application-key: ' .. appkey .. '\r\n' ..
'\r\n'
)
else
log('handshake failed: ' .. tostring(err))
end
else
log('connect failed: ' .. tostring(err))
sock:close()
end
end
line, err = sock:receive()
if line then
log('line: ' .. line)
if line:find(': hi') then
log('connection ok: ', line)
elseif line:find('data:') then
line = '{"data": '..string.sub(line,7)..'}'
log('textline',line)
line = json.pdecode(line)
log(line.data)
for _l,l_items in pairs(line.data) do
log(l_items)
log(l_items.type, l_items.data[1].id_v1, l_items.data[1].on.on)
end
end
else
log('receive failed: ' .. tostring(err))
sock:close()
sock = nil
end