! LET OP ! LEES EERST: !
Maak in je Fibaro HC2 een nieuwe Scene aan in LUA. Zet in de “General” tab het maximum aantal simultane processen voor deze scene op “1”. In de Advanced tab voeg je volgende code in:
--[[
%% properties
%% events
%% globals
--]]
local NestButton=0 --id of the virtual device
local setpincode=fibaro:getValue(NestButton,"ui.pincode.value")
local acccode=fibaro:getValue(NestButton,"ui.acccode.value")
local thermoid=fibaro:getValue(NestButton,"ui.thermoid.value")
local uri=fibaro:getValue(NestButton,"ui.url.value")
function capture307(status)
if (tonumber(status.status)==307) then
fibaro:call(NestButton, "setProperty","ui.url.value","https://"..status.headers.Location:match('^%w+://([^/]+)').."/")
fibaro:debug("307 Status - URL cached ".."https://"..status.headers.Location:match('^%w+://([^/]+)').."/")
else
result = json.decode(status.data)
if (result~=nil) then
--fibaro:debug("200 Result - returning")
else
fibaro:debug("200 Result - nil response")
end
return result
end
end
function doRequest(url,options)
local selfhttp = net.HTTPClient({timeout=2000})
selfhttp:request(url,options)
end
--have pincode, not acccode; get acccode
if (acccode=="") then
if (setpincode~="") then
fibaro:call(NestButton,"setProperty","ui.log.value",'access code request')
fibaro:debug('running access code request')
local body="code="..setpincode.."&client_id=85678ff8-99b5-47e5-b3fb-320dce57b9e6&client_secret=obH3q6WhTpzkdI5mL3zqRq3VT&grant_type=authorization_code"
--fibaro:debug(body)
doRequest("https://api.home.nest.com/oauth2/access_token?"..body,{
options={
method = 'POST',
timeout = 5000
},
success=function(status)
fibaro:debug("Received Access Code")
result=capture307(status)
fibaro:call(NestButton,"setProperty","ui.acccode.value","Bearer "..result['access_token'])
end,
error = function(status)
fibaro:debug("error "..status)
end
})
end
fibaro:sleep(1100)
end
-- have acccode not thermoid; get thermostat id
if (acccode~="") and (thermoid=="") then
fibaro:call(NestButton,"setProperty","ui.log.value",'thermostat id request')
doRequest(uri.."devices/thermostats",{
options={
headers = {
['Content-Type'] = "application/json",
['Authorization'] = acccode
},
method = 'GET',
timeout = 5000
},
success=function(status)
local result=capture307(status)
if (result~=nil) then
local tid=""
for k in pairs(result) do
tid=k
end
fibaro:call(NestButton,"setProperty","ui.thermoid.value",tid)
fibaro:call(NestButton,"setProperty","ui.setpoint.value","-1")
fibaro:debug("Received Thermostat ID")
end
end,
error = function(status)
fibaro:debug("error "..status)
end
})
fibaro:sleep(1100)
end
-- have accode and thermoid
if (acccode~="") and (thermoid~="") then
fibaro:call(NestButton,"setProperty","ui.log.value",'data update request')
doRequest(uri.."devices/thermostats/"..thermoid,{
options={
headers = {
['Content-Type'] = "application/json",
['Authorization'] = acccode
},
method = 'GET',
timeout = 5000
},
success=function(status)
local result=capture307(status)
if (result~=nil) then
fibaro:debug("Received Thermostat Data")
if (result["temperature_scale"]=="C") then
fibaro:call(NestButton,"setProperty","ui.scale.value","°C")
fibaro:call(NestButton,"setProperty","ui.curtemp.value",result["ambient_temperature_c"])
fibaro:call(NestButton,"setProperty","ui.settemp.value",result["target_temperature_c"])
local sp=fibaro:getValue(NestButton,"ui.setpoint.value")
if (tonumber(sp)==-1) then
fibaro:call(NestButton,"setProperty","ui.setpoint.value",result["target_temperature_c"])
end
else
fibaro:call(NestButton,"setProperty","ui.scale.Label","°F")
fibaro:call(NestButton,"setProperty","ui.curtemp.value",result["ambient_temperature_f"])
fibaro:call(NestButton,"setProperty","ui.settemp.value",result["target_temperature_f"])
local sp=fibaro:getValue(NestButton,"ui.setpoint.value")
if (tonumber(sp)==-1) then
fibaro:call(NestButton,"setProperty","ui.setpoint.value",result["target_temperature_c"])
end
end
fibaro:call(NestButton,"setProperty","ui.curhum.value",result["humidity"])
fibaro:call(NestButton,"setProperty","ui.online.value",result["is_online"])
end
end,
error = function(status)
fibaro:debug("error "..status)
end
})
fibaro:sleep(1100)
end
--have all, setpoint different from requested
if (acccode~="") and (thermoid~="") and (tonumber(fibaro:getValue(NestButton,"ui.setpoint.value"))~=-1) then
fibaro:call(NestButton,"setProperty","ui.log.value",'checking setpoint')
local csp=fibaro:getValue(NestButton,"ui.settemp.value")
local tsp=fibaro:getValue(NestButton,"ui.setpoint.value")
if (tonumber(csp)~=tonumber(tsp)) then
local pmsg=""
fibaro:debug("SetTempFrom : "..csp.." To: "..tsp)
if (fibaro:getValue(NestButton,"ui.scale.value")=="°C") then
pmsg='{"target_temperature_c": '..tsp..'}'
else
pmsg='{"target_temperature_f": '..tsp..'}'
end
fibaro:call(NestButton,"setProperty","ui.log.value",'changing setpoint from '..csp.." to "..tsp)
doRequest(uri.."devices/thermostats/"..thermoid,{
options={
headers = {
['Content-Type'] = "application/json",
['Authorization'] = acccode
},
method = 'PUT',
data = pmsg,
timeout = 5000
},
success=function(status)
local result=capture307(status)
if (result~=nil) then
fibaro:debug("Temperature Set OK")
if (result["target_temperature_c"]~=nil) then
fibaro:call(NestButton,"setProperty","ui.settemp.value",result["target_temperature_c"])
end
if (result["target_temperature_f"]~=nil) then
fibaro:call(NestButton,"setProperty","ui.settemp.value",result["target_temperature_f"])
end
end
end
})
end
fibaro:sleep(1100)
end
Sla deze scene op en onthou het ID dat toegekend wordt aan de scene. Dit kan je aflezen bovenaan in de balk van je internet browser (…/fibaro/en/scenes/edit.html?id=59) => 59
Ga vervolgens verder met het aanmaken van het virtual device.