-
Notifications
You must be signed in to change notification settings - Fork 11
/
Shelly.lua
357 lines (317 loc) · 11.4 KB
/
Shelly.lua
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
if dofile and not hc3_emulator then
hc3_emulator = {
name = "Shelly", -- Name of QA
poll = 2000, -- Poll HC3 for triggers every 2000ms
type = "com.fibaro.deviceController",
--proxy = true,
deploy=true,
quickVars = {
dev1="L1:0:admin:admin:192.168.1.141",
dev2="L2:0:admin:admin:192.168.1.141",
dev3="D1:0:admin:admin:fakeDim1",
dev4="R1:0:admin:admin:fakeRGB1",
},
UI={
{{button="on", text="On", onReleased="turnOn"},{button="off", text="Off", onReleased="turnOff"}},
{button="remove", text="Regenerate children", onReleased="remove"}
}
}
dofile("fibaroapiHC3.lua")
end
hc3_emulator.FILE("Toolbox/Toolbox_basic.lua","Toolbox")
hc3_emulator.FILE("Toolbox/Toolbox_events.lua","Toolbox_events")
hc3_emulator.FILE("Toolbox/Toolbox_child.lua","Toolbox_child")
local deviceMap = {
['shellyplug'] = {className="SingleSwitch",fibaroType="com.fibaro.binarySwitch"},
['shellyplugs'] = {className="SingleSwitch",fibaroType="com.fibaro.binarySwitch"},
['shelly1:relay'] = {className="SingleSwitch",fibaroType="com.fibaro.binarySwitch"},
['shelly1pm:relay'] = {className="SingleSwitch",fibaroType="com.fibaro.binarySwitch"},
['shellyswitch25:relay'] = {className="SingleSwitch",fibaroType="com.fibaro.binarySwitch"},
['shellyswitch2:relay'] = {className="SingleSwitch",fibaroType="com.fibaro.binarySwitch"},
['shellyrgbw2:color'] = {className="RGB",fibaroType="com.fibaro.colorController"},
['shellydimmer:white'] = {className="Dimmer",fibaroType="com.fibaro.multilevelSwitch"},
['shellydimmersl:white'] = {className="Dimmer",fibaroType="com.fibaro.multilevelSwitch"},
['shellyvintage:white'] = {className="Dimmer",fibaroType="com.fibaro.multilevelSwitch"},
}
debugs = { updates = true }
modules = {"events","childs"}
local format = string.format
_version = "0.9"
INTERVAL = 2000
Shellys = {} -- name:ip -> device object
IPrelays = {} -- ip -> {device1,..}
Watchers = {}
function QuickApp:onInit()
self:post({type='start'},1)
for id,child in pairs(self.childDevices) do
Shellys[child.sid]=child
end
end
class 'ShellyDevice'(QuickAppChild)
function ShellyDevice:__init(device)
QuickAppChild.__init(self,device)
self.className = self:getVariable("className")
self.name = self:getVariable("name")
self.sid = self:getVariable("sid")
self.ip = self:getVariable("ip")
self.creds = self:getVariable("creds")
self.relay = self:getVariable("relay")
self.base = "http://"..self.ip.."/"
self.uri = "status"
self:tracef("%s inited, IP:%s, Relay:%s, DeviceId:%s",self.className,self.ip,self.relay,self.id)
IPrelays[self.ip]=IPrelays[self.ip] or {}
table.insert(IPrelays[self.ip],self)
if not Watchers[self.ip] then
Watchers[self.ip] = self
self:poll()
end
end
function ShellyDevice:poll()
quickApp:shellyRequest{
baseURI=self.base,uri=self.uri,creds=self.creds,
cont={type='update', dev=self},
err=={type='updateAgain', dev=self},
}
end
function ShellyDevice:_update(data)
--quickApp:trace(quickApp:prettyJsonStruct(data))
for _,d in ipairs(IPrelays[self.ip] or {}) do
d:update(data)
end
end
class 'SingleSwitch'(ShellyDevice)
function SingleSwitch:__init(device)
ShellyDevice.__init(self,device)
end
function SingleSwitch:turnOn()
quickApp:shellyRequest{ baseURI = self.base, creds = self.creds, uri = "relay/"..self.relay.."?turn=on" }
end
function SingleSwitch:turnOff()
quickApp:shellyRequest{ baseURI = self.base, creds = self.creds, uri = "relay/"..self.relay.."?turn=off" }
end
function SingleSwitch:toggle()
quickApp:shellyRequest{ baseURI = self.base, creds = self.creds, uri = "relay/"..self.relay.."?turn=toggle" }
end
function SingleSwitch:update(data)
local r = tonumber(self.relay)+1
local relay= data.relays[r]
local meter= data.meters and data.meters[r] or {}
if debugs.updates then self:tracef("ID:%s, On:%s, Power:%s",self.id,relay.ison,meter.power) end
self.total = (meter.total or 0)/60000
if meter.power ~= self.power then
self.power = meter.power
self:updateProperty("power",meter.power)
end
self:updateProperty("value",relay.ison)
end
class 'Dimmer'(ShellyDevice)
function Dimmer:__init(device)
ShellyDevice.__init(self,device)
end
function Dimmer:turnOn()
quickApp:shellyRequest{ baseURI = self.base, creds = self.creds, uri = "light/"..self.relay.."?turn=on" }
end
function Dimmer:turnOff()
quickApp:shellyRequest{ baseURI = self.base, creds = self.creds, uri = "light/"..self.relay.."?turn=off" }
end
function Dimmer:toggle()
quickApp:shellyRequest{ baseURI = self.base, creds = self.creds, uri = "lights/"..self.relay.."?turn=toggle" }
end
function Dimmer:setValue(value)
quickApp:shellyRequest{ baseURI = self.base, creds = self.creds, uri = "lights/"..self.relay.."?brightness="..value }
end
function Dimmer:update(data)
local r = tonumber(self.relay)+1
local light= data.lights[r]
local meter= data.meters and data.meters[r] or {}
if debugs.updates then self:tracef("ID:%s, On:%s, Power:%s",self.id,light.ison,meter.power) end
self.total = (meter.total or 0)/60000
if meter.power and meter.power ~= self.power then
self.power = meter.power
self:updateProperty("power",meter.power)
end
self:updateProperty("value",lights.ison and light.brightness or 0)
end
class 'RGB'(ShellyDevice)
function RGB:__init(device)
ShellyDevice.__init(self,device)
end
function RGB:turnOn()
quickApp:shellyRequest{ baseURI = self.base, creds = self.creds, uri = "light/"..self.relay.."?turn=on" }
end
function RGB:turnOff()
quickApp:shellyRequest{ baseURI = self.base, creds = self.creds, uri = "light/"..self.relay.."?turn=off" }
end
function RGB:toggle()
quickApp:shellyRequest{ baseURI = self.base, creds = self.creds, uri = "lights/"..self.relay.."?turn=toggle" }
end
function RGB:setValue(value)
quickApp:shellyRequest{ baseURI = self.base, creds = self.creds, uri = "lights/"..self.relay.."?gain="..value }
end
function RGB:setColor(r,g,b,w)
local p = format("red=%s&green=%s&blue=%s&white=%s",r,g,b,w)
quickApp:shellyRequest{
baseURI = self.base,
creds = self.creds,
uri = "lights/"..self.relay.."?"..p
}
end
function RGB:update(data)
local r = tonumber(self.relay)+1
local light= data.lights[r]
local meter= data.meters and data.meters[r] or {}
if debugs.updates then self:tracef("ID:%s, On:%s, Power:%s",self.id,light.ison,meter.power) end
self.total = (meter.total or 0)/60000
if meter.power ~= self.power then
self.power = meter.power
self:updateProperty("power",meter.power)
end
local c = format("%s:%s:%s:%s",light.red,light.green,light.blue,light.white)
self:updateProperty("color",c)
self:updateProperty("value",lights.ison and light.gain or 0)
end
----------------------------------------------------------------------------------------------
function QuickApp:turnOn()
self:callChildren("turnOn")
self:updateProperty("value",99)
end
function QuickApp:turnOff()
self:callChildren("turnOff")
self:updateProperty("value",0)
end
function QuickApp:remove() self:removeAllChildren() plugin.restart() end
function QuickApp:shellyRequest(args)
local baseURI,uri,creds,cont,err = args.baseURI,args.uri,args.creds,args.cont,args.err
--self:trace(baseURI..uri)
local f = baseURI:match("http://(.-)/")
if _G[f] then _G[f](uri); return end -- testing
net.HTTPClient():request(baseURI..uri,{
options={
headers={['Authorization'] = creds, ["Accept"] = 'application/json'},
},
success=function(res)
if res.status > 201 then
self:warningf("Error:%s - %s",res.status,res.data)
if err then err.data=res; self:post(err) end
else
if cont then cont.data=json.decode(res.data); self:post(cont) end
end
end,
error=function(res)
if err then err.data=res; self:post(err) else self:post({type='error',data=res}) end
end,
})
end
function QuickApp:main()
self:event({type='start'},
function(env)
setInterval(function()
for _,w in pairs(Watchers) do w:poll() end
end,INTERVAL)
for _,val in pairs(self.config) do
local name,relay,user,pwd,ip = val:match("(.-):(.-):(.-):(.-):(.*)")
if user and pwd and ip then
local creds = self:basicAuthorization(user,pwd)
self:shellyRequest{
baseURI="http://"..ip.."/",
uri="settings",
creds = creds,
cont={type='info',info={ip=ip,relay=relay,creds=creds,name=name}}
}
end
end
end)
self:event({type='info'},
function(env)
local specs = env.event.data
local ip = env.event.info.ip
local creds = env.event.info.creds
local relay = env.event.info.relay
local name = env.event.info.name
--self:debug(self:prettyJsonStruct(specs))
local sid = name..":"..ip
local shellyType=specs.name:match("(.-)-"):lower()
shellyType = shellyType..(specs.mode and (":"..specs.mode) or "")
if Shellys[sid] then
--self:tracef("Shelly %s:%s exists",ip,i)
else
local map = deviceMap[shellyType]
if not map then
self:warning("Type %s for %s is unknown",specs.type,ip)
else
local d = self:createChild{
className=map.className,
type=map.fibaroType,
name=name,
interfaces={"power"},
quickVars={sid=sid,ip=ip,creds=creds,relay=relay,name=name}
}
if d then Shellys[sid] = d end
end
end
end)
self:event({type='update'},
function(env)
local dev = env.event.dev
local data = env.event.data
dev:_update(data)
self:post(function() dev:poll() end,INTERVAL)
end)
self:event({type='updateAgain'},
function(env)
self:warning(env.event.data)
self:post(function() dev:poll() end,2*INTERVAL)
end)
self:event({type='error'},
function(env)
self:warning(env.event.data)
end)
end
-------------------------- Simulated devices ----------------------
function fakeDim1(uri)
if uri=='settings' then
quickApp:post({
type='info',
data= {name="ShellyDimmer-*",type="SDT",mode="white"},
info={ip='fakeDim1',relay="0",name="D1"}
},1)
elseif uri=="status" then
else
local dev = Shellys["D1:fakeDim1"]
local cmd,val=uri:match("%?(.-)=(.*)")
dev._value = dev._value or 0
if cmd=='turn' then
if val=="on" then dev:updateProperty("value",dev._value)
elseif val=="off" then dev:updateProperty("value",0) end
elseif cmd=="brightness" then
dev._value = tonumber(val)
dev:updateProperty("value",dev._value )
end
end
end
function fakeRGB1(uri)
if uri=='settings' then
quickApp:post({
type='info',
data= {name="ShellyRGBW2-*",type="RGB",mode="color"},
info={ip='fakeRGB1',relay="0",name="R1"}
},1)
elseif uri=="status" then
else
local dev = Shellys["R1:fakeRGB1"]
local cmd,val=uri:match("%?(.-)=(.*)")
dev._value = dev._value or 0
if cmd=='turn' then
if val=="on" then dev:updateProperty("value",dev._value)
elseif val=="off" then dev:updateProperty("value",0) end
elseif cmd=="gain" then
dev._value = tonumber(val)
dev:updateProperty("value",dev._value )
elseif cmd=="red" then
local r,g,b,w = uri:match("red=(%d+)&green=(%d+)&blue=(%d+)&white=(%d+)")
local v = format("%s,%s,%s,%s",r,g,b,w)
dev:updateProperty("color",v)
end
print(uri)
end
end