-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdtg_domoticz.lua
More file actions
485 lines (465 loc) · 16 KB
/
dtg_domoticz.lua
File metadata and controls
485 lines (465 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
dtg_domoticz_version = '0.9 202402091028'
--[[
A set of support functions currently aimed at dtgbot,
but probably more general
]]
function Check_Json_Result(jresponse)
-- check if json call returned status ok
local jsonok = false
if jresponse ~= nil then
local decoded_response = JSON.decode(jresponse)
if decoded_response.status ~= nil and decoded_response.status:lower() == "ok" then
jsonok = true
end
end
return jsonok
end
function Form_Device_name(parsed_cli)
-- joins together parameters after the command name to form the full "device name"
Print_to_Log(0, parsed_cli[2])
DeviceName = parsed_cli[3]
Print_to_Log(0, parsed_cli[3])
local len_parsed_cli = #parsed_cli
if len_parsed_cli > 3 then
for i = 4, len_parsed_cli do
DeviceName = DeviceName .. " " .. parsed_cli[i]
Print_to_Log(0, parsed_cli[i])
end
end
Print_to_Log(0, DeviceName)
return DeviceName
end
function Form_Device_names(parsed_cli)
-- joins together parameters after the command name to form the full "device name"
--command = parsed_cli[2]
DeviceNames = {}
DeviceNames[1] = ""
local j = 1
local len_parsed_cli = #parsed_cli
local bit
for i = 3, len_parsed_cli do
bit = parsed_cli[i]
if not (string.match(bit, ",")) then
bit = string.gsub(bit, " ", "")
if (DeviceNames[j] == "") then
DeviceNames[j] = bit
else
DeviceNames[j] = DeviceNames[j] .. " " .. bit
end
else
-- Needed to deal with , ,word, word,word etc..
bit = string.gsub(bit, ",", " , ")
for w in string.gmatch(bit, "([^ ]+)") do
w = string.gsub(w, " ", "")
if (w == ",") then
j = j + 1
DeviceNames[j] = ""
else
if (DeviceNames[j] == "") then
DeviceNames[j] = w
else
DeviceNames[j] = DeviceNames[j] .. " " .. w
end
end
end
end
end
return DeviceNames
end
-- returns list of all user variables - called early by dtgbot
-- in case Domoticz is not running will retry
-- allowing Domoticz time to start
function Domo_Variable_List()
local t, jresponse, status, decoded_response
t = Domoticz_Url .. "/json.htm?type=command¶m=getuservariables"
jresponse = nil
local domoticz_tries = 1
-- Domoticz seems to take a while to respond to getuservariables after start-up
-- So just keep trying after 1 second sleep
while (jresponse == nil) do
Print_to_Log(1, "JSON request <" .. t .. ">")
jresponse, status = HTTP.request(t)
if (jresponse == nil) then
SOCKET.sleep(1)
domoticz_tries = domoticz_tries + 1
if domoticz_tries > 20 then
Print_to_Log(0, "Domoticz not sending back user variable list")
break
end
end
end
if jresponse ~= nil then
Print_to_Log(0, "Domoticz returned getuservariables after " .. domoticz_tries .. " attempts")
decoded_response = JSON.decode(jresponse)
else
decoded_response = {}
decoded_response["result"] = "{}"
end
return decoded_response
end
-- returns idx of a user variable from name
function Domo_Variable_List_Names_IDXs()
local record, decoded_response, result
decoded_response = Domo_Variable_List()
result = decoded_response["result"]
local variables = {}
if result then
for i = 1, #result do
record = result[i]
if type(record) == "table" then
variables[record["Name"]] = record["idx"]
end
end
end
return variables
end
function Domo_Idx_From_Variable_Name(DeviceName)
return Variablelist[DeviceName]
end
-- returns the value of the variable from the idx
function Domo_Get_Variable_Value(idx)
local t, decoded_response
if idx == nil then
return ""
end
t = Domoticz_Url .. "/json.htm?type=command¶m=getuservariable&idx=" .. tostring(idx)
Print_to_Log(1, "JSON request <" .. t .. ">")
local jresponse, status = HTTP.request(t)
decoded_response = JSON.decode(jresponse)
Print_to_Log(1, Sprintf("Idx:%s Value:%s ", idx, decoded_response["result"][1]["Value"]))
return decoded_response["result"][1]["Value"]
end
function Domo_Set_Variable_Value(idx, name, Type, value)
-- store the value of a user variable
local t, jresponse, decoded_response
t = Domoticz_Url .. "/json.htm?type=command¶m=updateuservariable&idx=" .. idx .. "&vname=" .. name .. "&vtype=" .. Type .. "&vvalue=" .. tostring(value)
Print_to_Log(1, "JSON request <" .. t .. ">")
jresponse, status = HTTP.request(t)
return
end
-- ### Not Used currently
function Domo_Create_Variable(name, Type, value)
-- creates user variable
local t, jresponse, decoded_response
t = Domoticz_Url .. "/json.htm?type=command¶m=saveuservariable&vname=" .. name .. "&vtype=" .. Type .. "&vvalue=" .. tostring(value)
Print_to_Log(1, "JSON request <" .. t .. ">")
jresponse, status = HTTP.request(t)
return
end
function Domo_Get_Names_From_Variable(DividedString)
local Names = {}
for Name in string.gmatch(DividedString, "[^|]+") do
Names[#Names + 1] = Name
Print_to_Log(1, "Name :" .. Name)
end
if Names == {} then
Names = nil
end
return Names
end
-- returns a device table of Domoticz items based on type i.e. devices or scenes
function Domo_Device_List(DeviceType, idx)
local t, jresponse, status, decoded_response
-- Use new API format as of Revison 15326.
if DeviceType == "plandevices" then
if idx then
t = Domoticz_Url .. "/json.htm?type=command¶m=get" .. DeviceType .. "&idx=" .. idx
else
Print_to_Log(0, " idx parameter missing for Devicelist update :" .. DeviceType)
end
else
if (DomoticzBuildDate or 0) > 20230601 then
t = Domoticz_Url .. "/json.htm?type=command¶m=get" .. DeviceType .. "&order=name&used=true"
else
t = Domoticz_Url .. "/json.htm?type=" .. DeviceType .. "&order=name&used=true"
end
end
Print_to_Log(1, "JSON request <" .. t .. ">")
jresponse, status = HTTP.request(t)
if jresponse ~= nil then
decoded_response = JSON.decode(jresponse)
else
decoded_response = {}
decoded_response["result"] = "{}"
end
return decoded_response
end
-- returns a list of Domoticz items based on type i.e. devices or scenes
function Domo_Device_List_Names_IDXs(DeviceType)
--returns a device idx based on its name
local record, decoded_response
decoded_response = Domo_Device_List(DeviceType)
local result = decoded_response["result"]
local devices = {}
local dcount = 0;
local devicesproperties = {}
if result ~= nil then
for i = 1, #result do
record = result[i]
if type(record) == "table" then
if DeviceType == "plans" then
devices[record["Name"]] = record["idx"]
dcount = dcount + 1
else
devices[string.lower(record["Name"])] = record["idx"]
devices[record["idx"]] = record["Name"]
if DeviceType == "scenes" then
devicesproperties[record["idx"]] = {Type = record["Type"], SwitchType = record["Type"]}
end
dcount = dcount + 1
end
end
end
Print_to_Log(1,"DeviceType:"..DeviceType.." count:"..dcount.." DomoticzBuildDate:"..DomoticzBuildDate)
else
Print_to_Log(0, " !!!! Domo_Device_List_Names_IDXs(): nothing found for ", DeviceType)
end
return devices, devicesproperties
end
function Domo_Idx_From_Name(DeviceName, DeviceType)
--returns a device idx based on its name
if DeviceType == "devices" then
return Devicelist[string.lower(DeviceName)]
elseif DeviceType == "scenes" then
return Scenelist[string.lower(DeviceName)]
else
return Roomlist[DeviceName]
end
end
function Domo_Retrieve_Status(idx, DeviceType)
local t, jresponse, status, decoded_response
if (DomoticzBuildDate or 0) > 20230601 then
t = Domoticz_Url .. "/json.htm?type=command¶m=get" .. DeviceType .. "&rid=" .. tostring(idx)
else
t = Domoticz_Url .. "/json.htm?type=" .. DeviceType .. "&rid=" .. tostring(idx)
end
Print_to_Log(2, "JSON request <" .. t .. ">")
jresponse, status = HTTP.request(t)
if jresponse ~= nil then
decoded_response = JSON.decode(jresponse)
else
decoded_response = {}
decoded_response["result"] = ""
end
return decoded_response
end
-- support function to scan through the Devices and Scenes idx tables and retrieve the required information for it
function Domo_Devinfo_From_Name(idx, DeviceName, DeviceScene)
local k, record, Type, DeviceType, SwitchType
local found = 0
local rDeviceName = ""
local status = ""
local LevelNames = ""
local LevelInt = 0
local MaxDimLevel = 100
local ridx = 0
local tvar
if DeviceScene ~= "scenes" then
-- Check for Devices
-- Have the device name
if DeviceName ~= "" then
idx = Domo_Idx_From_Name(DeviceName, "devices")
end
Print_to_Log(2, "==> start Domo_Devinfo_From_Name", idx, DeviceName)
if idx ~= nil then
tvar = Domo_Retrieve_Status(idx, "devices")["result"]
if tvar == nil then
found = 9
else
record = tvar[1]
if record ~= nil and record.Name ~= nil and record.idx ~= nil then
Print_to_Log(2, "device ", DeviceName, record.Name, idx, record.idx)
end
if type(record) == "table" then
ridx = record.idx
rDeviceName = record.Name
DeviceType = "devices"
Type = record.Type
LevelInt = record.LevelInt
if LevelInt == nil then
LevelInt = 0
end
LevelNames = record.LevelNames
if LevelNames == nil then
LevelNames = ""
end
-- as default simply use the status field
-- use the dtgbot_type_status to retrieve the status from the "other devices" field as defined in the table.
Print_to_Log(2, "Type ", Type)
if dtgbot_type_status[Type] ~= nil then
Print_to_Log(2, "dtgbot_type_status[Type] ", JSON.encode(dtgbot_type_status[Type]))
if dtgbot_type_status[Type].Status ~= nil then
status = ""
CurrentStatus = dtgbot_type_status[Type].Status
Print_to_Log(2, "CurrentStatus ", JSON.encode(CurrentStatus))
for i = 1, #CurrentStatus do
if status ~= "" then
status = status .. " - "
end
cindex, csuffix = next(CurrentStatus[i])
status = status .. tostring(record[cindex]) .. tostring(csuffix)
Print_to_Log(2, "cindex:", cindex, " csuffix:", csuffix," status:", status)
end
end
else
SwitchType = record.SwitchType
-- Check for encoded selector LevelNames
if SwitchType == "Selector" then
if string.find(LevelNames, "[|,]+") then
Print_to_Log(2, "-- < 4.9700 selector switch levelnames: ", LevelNames)
else
LevelNames = MIME.unb64(LevelNames)
Print_to_Log(2, "-- >= 4.9700 decoded selector switch levelnames: ", LevelNames)
end
end
MaxDimLevel = record.MaxDimLevel
status = tostring(record.Status)
end
found = 1
--~ Print_to_Log(2," !!!! found device",record.Name,rDeviceName,record.idx,ridx)
end
end
end
--~ Print_to_Log(2," !!!! found device",rDeviceName,ridx)
end
-- Check for Scenes
if found == 0 then
if DeviceName ~= "" then
idx = Domo_Idx_From_Name(DeviceName, "scenes")
else
DeviceName = Domo_Idx_From_Name(idx, "scenes")
end
if idx ~= nil then
DeviceName = Scenelist[idx]
DeviceType = "scenes"
ridx = idx
rDeviceName = DeviceName
SwitchType = Sceneproperties[tostring(idx)]["SwitchType"]
Type = Sceneproperties[tostring(idx)]["Type"]
found = 1
end
end
-- Check for Scenes
if found == 0 or found == 9 then
ridx = 9999
DeviceType = "command"
Type = "command"
SwitchType = "command"
end
Print_to_Log(2, " --< Domo_Devinfo_From_Name:", found, ridx, rDeviceName, DeviceType, Type, SwitchType, status, LevelNames, LevelInt)
return ridx, rDeviceName, DeviceType, Type, SwitchType, MaxDimLevel, status, LevelNames, LevelInt
end
-- Switch functions
function Domo_SwitchID(DeviceName, idx, DeviceType, state, SendTo)
if string.lower(state) == "on" then
state = "On"
elseif string.lower(state) == "off" then
state = "Off"
else
return "state must be on or off!"
end
t = Domoticz_Url .. "/json.htm?type=command¶m=switch" .. DeviceType .. "&idx=" .. idx .. "&switchcmd=" .. state
Print_to_Log(1, "JSON request <" .. t .. ">")
local jresponse, status = HTTP.request(t)
local response
Print_to_Log(1, "raw jason", jresponse)
if Check_Json_Result(jresponse) then
response = "Switched " .. DeviceName .. " " .. state
else
response = "Failed to switch " .. DeviceName .. " to " .. state
end
return response
end
function Domo_sSwitchName(DeviceName, DeviceType, SwitchType, idx, state)
local status
local response
if idx == nil then
response = "Device " .. DeviceName .. " not found."
else
local subgroup = "light"
if DeviceType == "scenes" then
subgroup = "scene"
end
if string.lower(state) == "on" then
state = "On"
t = Domoticz_Url .. "/json.htm?type=command¶m=switch" .. subgroup .. "&idx=" .. idx .. "&switchcmd=" .. state
elseif string.lower(state) == "off" then
state = "Off"
t = Domoticz_Url .. "/json.htm?type=command¶m=switch" .. subgroup .. "&idx=" .. idx .. "&switchcmd=" .. state
elseif string.lower(string.sub(state, 1, 9)) == "set level" then
t = Domoticz_Url .. "/json.htm?type=command¶m=switch" .. subgroup .. "&idx=" .. idx .. "&switchcmd=Set%20Level&level=" .. string.sub(state, 11)
else
return "state must be on, off or set level!"
end
Print_to_Log(3, "JSON request <" .. t .. ">")
local jresponse, status = HTTP.request(t)
Print_to_Log(3, "JSON feedback: ", jresponse)
if Check_Json_Result(jresponse) then
response = DTGMenu_translate_desc(Language, "Switched") .. " " .. (DeviceName or "?") .. " => " .. (state or "?")
else
response = "Failed to switch " .. (DeviceName or "?") .. " to " .. (state or "?")
end
end
Print_to_Log(0, " -< Domo_sSwitchName:", DeviceName, idx, status, response)
return response, status
end
-- other functions
function FileExists(name)
local f = io.open(name, "r")
if f ~= nil then
io.close(f)
return true
else
return false
end
end
function Domoticz_Version()
local t, jresponse, status, decoded_response
local domoticz_tries = 1
t = Domoticz_Url .. "/json.htm?type=command¶m=getversion"
jresponse = nil
-- Domoticz seems to take a while to respond API calls after start-up
-- So just keep trying after 1 second sleep
while (jresponse == nil) do
Print_to_Log(1, "JSON request <" .. t .. ">")
jresponse, status = HTTP.request(t)
if (jresponse == nil) then
SOCKET.sleep(1)
domoticz_tries = domoticz_tries + 1
if domoticz_tries > 20 then
Print_to_Log(0, "Domoticz not replying to the getversion")
break
end
end
end
Print_to_Log(9, jresponse)
if jresponse ~= nil then
decoded_response = JSON.decode(jresponse)
-- Set the Global variables for Domoticz version and revision
DomoticzRevision = (decoded_response["Revision"] or 0)
DomoticzVersion = (decoded_response["version"] or 0)
-- build_time: "2023-06-18 14:39:26" convert to number 20230618 to allow for comparing
DomoticzBuildDate = (decoded_response['build_time'] or 0)
DomoticzBuildDate = DomoticzBuildDate:gsub("(%d+)%-(%d+)%-(%d+).*","%1%2%3")
DomoticzBuildDate = tonumber(DomoticzBuildDate or 0)
end
end
function Domoticz_Language()
local t, jresponse, status, decoded_response
t = Domoticz_Url .. "/json.htm?type=command¶m=getlanguage"
jresponse = nil
Print_to_Log(1, "JSON request <" .. t .. ">")
jresponse, status = HTTP.request(t)
if jresponse ~= nil then
decoded_response = JSON.decode(jresponse)
else
decoded_response = {}
decoded_response["result"] = "{}"
end
local Language = decoded_response["Language"]
if Language ~= nil then
return Language
else
return "en"
end
end