-
Notifications
You must be signed in to change notification settings - Fork 1
/
L_MyQGateway.lua
1070 lines (873 loc) · 70.2 KB
/
L_MyQGateway.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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local https = require("ssl.https")
local json = require("dkjson")
--------load a local mock module when needed so we can easily run outside of Vera---------
luup = luup ~= nil and luup or require "luup"
------------------------------------------------------------------------------------------
--Initialize randomizer for PKCE.
math.randomseed(math.floor(os.time()))
local MYQGATEWAY_SID = "urn:macrho-com:serviceId:MyQGateway1"
local GDO_SID = "urn:upnp-org:serviceId:SwitchPower1"
local GDO_DEVICE_TYPE = "urn:schemas-upnp-org:device:SwitchPower:1"
local LAMP_SID = "urn:upnp-org:serviceId:SwitchPower1"
local LAMP_DEVICE_TYPE = "urn:schemas-upnp-org:device:BinaryLight:1"
local VERSION = "v3.0 03-28-2021"
local PLUGIN_VARS = {
REFRESH_INTERVAL = "RefreshInterval",
REFRESH_AFTER_CHANGE_INTERVAL = "RefreshAfterChangeInterval",
USERNAME = "Username",
PASSWORD = "Password",
DEBUG_MODE = "DebugMode",
DEBUG_MODE_TEXT = "DebugModeTxt",
LAST_CHECK = "LastCheck",
LAST_TRIP = "LastTrip",
STATUS = "Status",
NUM_DOORS = "NumDoors",
NUM_LIGHTS = "NumLights",
TOKEN_TIMESTAMP = "Timestamp"
}
local LOGLEVELS = {
CRITICAL = 1, -- Critical error. Something is wrong that shouldn't happen.
WARN = 2, -- Warning. This is something to make note of, though it's not always a problem.
VARIABLE_CHANGE = 6, -- Variable. A UPnP Variable has changed.
GENERAL = 10, -- General Status. There are lots of these messages to indicate something happening in the system.
OUTBOUND_DATA = 41, -- Outgoing data going to the external devices, such as the Z-Wave dongle and Luup plugins talking to the serial/network devices.
INCOMING_DATA = 42 -- Incoming data. (as above)
}
local PLUGIN_CONFIG = {
FORCE_DEBUG = false, -- overrides user configured debug_mode - normally false
DEBUG_MODE = true,
USERNAME = "",
PASSWORD = "",
ACCOUNTID = "",
SECURITY_TOKEN = "",
APPID = "",
NAME = "MyQGateway",
LAST_TOKEN_REFRESH = os.time()-60*60*24, --yesterday by default
MAX_TOKEN_AGE = 3600, -- in seconds
REFRESH_AFTER_CHANGE = 10, -- in seconds
REFRESH_INTERVAL = 30, -- in seconds
CLEAR_TASK_AFTER = 30 -- in seconds
}
-- Found: http://forum.micasaverde.com/index.php/topic,7458.msg47733.html#msg47733
-- Probably NOT the way I want to do this but works for now
local child_id_lookup_table = {}
local child_deviceid_lookup_table = {} -- Reverses for looking up by Vera DeviceId
local TASK_ERROR = 2
local TASK_ERROR_PERM = -2
local TASK_SUCCESS = 4
local MSG_CLASS = "MyQ Gateway"
local taskHandle = -1
local MyQGatewayID = nil -- Device ID
local COOKIEJAR = {}
local UTIL = {
base64encode = function (self, data)
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' -- You will need this for encoding/decoding
return ((data:gsub('.', function(x)
local r,b='',x:byte()
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
return r;
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if (#x < 6) then return '' end
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end,
initVar = function(self, serviceId, variable, default, deviceId)
if (variable == nil or serviceId == nil or default == nil) then return end
if (type(default) == "boolean") then
default = default and "1" or "0"
end
local val = luup.variable_get(serviceId, variable, deviceId)
if (val == nil or val == "") then
luup.variable_set(serviceId, variable, default, deviceId)
end
end,
getFriendlyTimestamp = function(self, timestamp)
return os.date("%a %b %d %Y, %X", timestamp)
end,
sanitizeText = function(self, text)
if (PLUGIN_CONFIG.USERNAME and (PLUGIN_CONFIG.USERNAME ~= "")) then
text = text:gsub(PLUGIN_CONFIG.USERNAME,"********")
end
if (PLUGIN_CONFIG.PASSWORD and (PLUGIN_CONFIG.PASSWORD ~= "")) then
text = text:gsub(PLUGIN_CONFIG.PASSWORD,"********")
end
return text
end,
base64urlencode = function(self,str)
str = str:gsub("(%+)", "-")
str = str:gsub("(/)", "_")
str = str:gsub("(%=)", "")
return str
end,
shellExecute = function(self, cmd, Output)
if (Output == nil) then Output = true end
local file = assert(io.popen(cmd, 'r'))
if (Output == true) then
local cOutput = file:read('*all')
file:close()
return cOutput
else
file:close()
return
end
end,
buildKeyValueString = function(self, values, delimiter)
delimiter = delimiter ~= nil and delimiter or "&"
local str = ""
for k,v in pairs(values) do
str = str..delimiter..k.."="..v
end
return self:trim(str, delimiter)
end,
generateCodeVerifier = function(self, length)
local charset = {} do -- [0-9a-zA-Z]
for c = 48, 57 do table.insert(charset, string.char(c)) end --numbers
for c = 65, 90 do table.insert(charset, string.char(c)) end --A-Z
for c = 97, 122 do table.insert(charset, string.char(c)) end --a-z
end
local specialChars = "-._~"
for i = 1, #specialChars do
local s= string.sub(specialChars, i, i)
table.insert(charset, s)
end
local res = ""
for _ = 1, length do
res = res .. charset[math.random(1, #charset)]
end
return res
end,
startsWith = function(self, str, pattern)
return string.find(str, '^' .. pattern) ~= nil
end,
urldecode = function(self, url)
local hex_to_char = function(x)
return string.char(tonumber(x, 16))
end
if url == nil then
return
end
url = url:gsub("+", " ")
url = url:gsub("%%(%x%x)", hex_to_char)
return url
end,
trim = function(self, s, trimChar)
trimChar = trimChar ~=nil and trimChar or ' '
if (s == nil) then return s end
local l = 1
while string.sub(s,l,l) == trimChar do
l = l+1
end
local r = string.len(s)
while string.sub(s,r,r) == trimChar do
r = r-1
end
return string.sub(s,l,r)
end,
cutString = function(self, srcString, startPattern,endPattern)
local idx = string.find(srcString, startPattern)+#startPattern
local subStr = string.sub(srcString, idx, #srcString)
idx = endPattern ~= nil and string.find(subStr,endPattern) or #subStr+1
return string.sub(subStr, 1, idx-1)
end,
split = function(self, str, sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
string.gsub(str, pattern, function(c) fields[#fields+1] = c end)
return fields
end,
parseCookies = function(self,cookieHeaders)
for _,v in pairs(self:split(cookieHeaders, ",")) do
local cookieVal = self:split(v, ";")[1]
local kv = self:split(cookieVal, "=")
COOKIEJAR[self:trim(kv[1])] = self:trim(kv[2])
end
return self:buildKeyValueString(COOKIEJAR, ";")
end,
request = function(self, url, method, headers, body, logRequest, logResponse)
headers = headers ~= nil and headers or {}
headers["Host"] = self:split(url,"//")[2]
headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
headers["Accept-Language"] = "en-US,en;q=0.9"
headers["User-Agent"] = headers["User-Agent"] ~= nil and headers["User-Agent"] or "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"
if (logRequest == true) then
debug(method.." "..url)
if (headers ~= nil) then
for k,v in pairs(headers) do
debug(k..": "..v)
end
end
if (body ~= nil) then
debug(body)
end
debug("--------------------------------------------------------------------------------------------------------------------------")
end
local responseChunks = {}
local success, statusCode, responseHeaders = https.request{
url = url,
method = method,
headers = headers,
source = body ~=nil and ltn12.source.string(body) or nil,
protocol = "tlsv1_2",
redirect = false,
sink = ltn12.sink.table(responseChunks)
}
local responseContent = (responseChunks ~= nil and table.maxn(responseChunks) > 0 and table.concat(responseChunks)) or nil
if (logResponse == true) then
for k,v in pairs(responseHeaders) do
debug(k..": "..v)
end
if (responseContent ~= nil) then
debug(responseContent)
end
debug("--------------------------------------------------------------------------------------------------------------------------")
end
return success, statusCode, responseHeaders, responseContent
end,
debug = function(self, text, level, forced)
if (forced == nil) then forced = false end
if ((PLUGIN_CONFIG.DEBUG_MODE == true) or (forced == true)) then
-- sanitize the debug text
text = self:sanitizeText(text)
if (#text < 7000) then
if (level == nil) then
luup.log((text or "NIL"), LOGLEVELS.WARN)
else
luup.log((text or "NIL"), level)
end
else
-- split the output into multiple debug lines
local prefix_string = ""
local _,debug_prefix,_ = text:find("): ")
if (debug_prefix) then
prefix_string = text:sub(1,debug_prefix)
text = text:sub(debug_prefix + 1)
end
while (#text > 0) do
local debug_text = text:sub(1,7000)
text = text:sub(7001)
if (level == nil) then
luup.log((prefix_string..(debug_text or "NIL")), LOGLEVELS.WARN)
else
luup.log((prefix_string..(debug_text or "NIL")),level)
end
end
end
end
end
}
UPnP = {
NAME = "UPnP",
changeDeviceState = function(self, lul_device, lul_settings)
local logPrefix = PLUGIN_CONFIG.NAME.."::"..self.NAME.."::changeDeviceState "
local targetValue = lul_settings.newTargetValue
if (targetValue ~= "1" and targetValue ~= "0") then
log(logPrefix.."Unknown state value "..targetValue.." for device "..lul_device, LOGLEVELS.ERROR)
return 2, 0
end
local deviceFamily = lul_settings.deviceFamily
if (deviceFamily == nil) then
log(logPrefix.."Unspecified device family when requesting "..targetValue.." for device "..lul_device, LOGLEVELS.ERROR)
return 2, 0
end
local serviceId
if (deviceFamily == "garagedoor") then
serviceId = GDO_SID
elseif (deviceFamily == "lamp") then
serviceId = LAMP_SID
else
log(logPrefix.."Unsupported device family ".. deviceFamily.." when requesting "..targetValue.." for device "..lul_device, LOGLEVELS.ERROR)
return 2, 0
end
local myQSerialNumber = child_deviceid_lookup_table[lul_device]
debug(logPrefix.."gateway/device/family/serial/newStatus to "..MyQGatewayID.."/"..(lul_device or "NIL").. "/".. (deviceFamily or "NIL") .."/" ..(myQSerialNumber or "NIL").. "/" .. (targetValue or "NIL"))
local result = MYQ_API:changeDeviceStatus(PLUGIN_CONFIG.SECURITY_TOKEN, PLUGIN_CONFIG.ACCOUNTID, myQSerialNumber, lul_device, MyQGatewayID, deviceFamily, targetValue)
if (result == false) then
log(logPrefix.."Failed calling myQ API to change status on device "..lul_device, LOGLEVELS.ERROR)
return 2, 0
end
luup.variable_set(serviceId, PLUGIN_VARS.STATUS, tonumber(targetValue), lul_device)
return 4, 0
end,
setCredentials = function(self, lul_device, lul_settings)
local Username = lul_settings.newUsername or ""
local Password = lul_settings.newPassword or ""
if Username ~= "" then
luup.variable_set(MYQGATEWAY_SID,PLUGIN_VARS.USERNAME,Username,lul_device)
end
if Password ~= "" then
luup.variable_set(MYQGATEWAY_SID,PLUGIN_VARS.PASSWORD,Password,lul_device)
end
task("Please wait....Re-initializing myQ after password update.", TASK_ERROR_PERM)
init(lul_device)
return 4, nil
end,
toggleDebugMode = function(self, lul_device)
PLUGIN_CONFIG.DEBUG_MODE = not PLUGIN_CONFIG.DEBUG_MODE
local varValue = (PLUGIN_CONFIG.DEBUG_MODE and "1" or "0")
local varTextValue = (PLUGIN_CONFIG.DEBUG_MODE and "ENABLED" or "DISABLED")
task("DEBUG MODE "..varTextValue.."!",TASK_SUCCESS)
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.DEBUG_MODE, varValue, lul_device)
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.DEBUG_MODE_TEXT, varTextValue, lul_device)
debug("("..PLUGIN_CONFIG.NAME.."::"..self.NAME.."::ToggleDebugMode): Debug mode now ["..(PLUGIN_CONFIG.DEBUG_MODE and "ENABLED" or "DISABLED").."].")
end
}
function debug(text, level, forced)
UTIL:debug(text, level, forced)
end
--Utility function to prefix MSG_CLASS to all log calls
function log(message,msgLevel)
if (msgLevel ~= nil) then debug(message,msgLevel,true) else debug(message,nil,true) end
end
local DEVICE_ICONS = {
DATA = {
["MyQ_gateway.png"] = "89504E470D0A1A0A0000000D494844520000003C0000003C08060000003AFCD972000000206348524D0000870F00008C0F0000FD520000814000007D790000E98B00003CE5000019CC733C857700000103694343504943432050726F66696C65000028CF6360609260000226010686DCBC92A22077278588C82805062490985C5CC0801B3032307CBB062219182EEB32900E3853528B9381F407202E29025A0E343205C8164987B02B40EC2408BB07C42E0A097206B21700D91AE948EC24247679494109907D02A43EB9A008C4BE0364DBE4E6942623DCCDC0939A171A0CA423805886A1982188C19DC18981CA00119EF98B18182CBE3230304F408825CD6460D8DECAC020710B21A602F4037F0B03C3B6F3058945896021165024A5A531307C5ACEC0C01BC9C0207C8181812B1AD30E445CE0F0AB02D8AFEE0CF94098CE90C3900A14F164C8634866D003B28C180C180C19CC004CA540911C77C180000000097048597300000EC300000EC301C76FA86400000726494441546843ED9A096C145518C7FF6F667676B7F4E26A28D0165AEE2A1E5514410EA1A2A262D4483431E205E29968E2114314D188468304904412624C0C46A590A8201020E550C122726929B4624D0F5BDA5228EDCEEC1C7E6F7696D975173A9010B7EBFE9276DFBC999D79FFF77DEFFBBE372D52A4489122458AC481D99F7159765C630B2B35D6AADA1D09CE88748685A34563769E64F7C41223787B938EB9FBB59266C59CA79B98D4A523D3B0CF253A22A9F189689218B60CF4B115474ABD27EC53E788123C668BE2276B7E785235E7192604BBBB47E21110E827634186842595B7FA74BBDB11B5BC5A13EBBBCC554D8A39BFA78BE5040DF81A0378AF2DC85EB3BB2CCE59386F43604E5D00ABA979C175DDD31018B4C23436A16A8677AF75CC7F95EE52E596205EA1665289E590B74A8D8AF9DAC6C690575B826B3BCD31AA8161564F12A218B8F9A3E37A6FDEB604B7A8663E9F09DE4E4628DB641C6E377278DB124C9139E95C391232261A9490C61E1F8D2F9694E06427253851106062085584A5FD0CDCD95FC735993AFCEC5C8578C98422575960167DACE7EDFF9A6CC9C093050C8F0E913032333A539EA17A71438386F78FE938709A4F896BF87EEF2AE35E5F654259785C9681DD93652C1EEB8B11CBC9A01DC1EC7C193B27FBF0FA70DA20B08B906C933082C7671BF8768217A33245BB2704CFA15438449146FBBF378ABDF8F00A815CF4E2442784E001B281CFC7C9E8EB0D0D870BDCD81044E98E2E64AD3F8BB4759D28DED489454714B4510DCCA14D019E1926E3F13CEBD035DD0AE61764088615440A298894641AD63AE3F000904B83BD9E5CB180CE85CB35895C2D9DBE130F31CEB997870B28E815B2AC4662171C5670CF1E1DDB5A047452C5AB3111959D22DEAC02A6962BA83DEB04AF378B65E478DCBFA2E85670AED7C0FE6932D694306C982063758984FDB7C8B88B22E7DBA3811FA7C85879B5841FA678F14171486C9ECFC49EA91EE4FB620732BF0074BDF3D85E247ECE50D93E02D6FCA9E283E326090F4F5F34873A043C52A142E5BE4EE4FA05DC3F28FEB5F1E8DEC274AF7C9AFD03ED26AEDC16C4D5DB75BC7A48C517E3FD189B25A0649B82EBCA0DDCB85DC1CC5C0933734C9CE812B0B7D5C00B45D1B74F170D3C5DE4C167B5CE444CEC0364794203E61A16556A707C253EBB5A19BE27970F73C700D1F55AEE5630A795F6574B6B4C044DEED80C650D0C7F759A587A4C438B1672C5BF14116BEB744CCF11AD472F39AEE3C17C4F94BBDD3D80E174D044798B23684CA63384EA331A6A68B2BAC3A0316C6D760416F562905D1AD995607EEB48E7D4E8A79D161B658928DA68D2336D6B1D3A236067B386670B4317710B3C5F246145B50635C25D7B45649FBF038635A1F1E0D33884964838159D541DC1323D82C70637B8121C9738F78FECE2EDC547353C3ED46345E1697D4DF426339435447F915B3CCCE0B4F86986FB55D90D02AA6FF763F34D12AD7B93628B3331018A61E75BF3FFE6D205BBB8FF7EB272459B86B954393D3F4CC22735419C35A21FF9EB29C7770AD2258CCD880D745CE06DB9A1C0362947C2922B19AD5BE73E951D7CB95D6EC1AE6078BF4AC70B2364DCD047C0EADA58EBED39C52C57E6F0212F1AE389A9A03A6892BEAC55EC23E0B1422FA6E478EC23E0BB063D9EC3C5A55BC14D2AC3FD7B54288633837C9D3DF36B107B69B0917C5567E03D12180917547546C7A77F6A68D3621FA750205C5AE5FC69E38E811E2C230B668A8E04DE7AEEA0815FC85B38FCA93C7B706A3A74ACAD772BD785602E7437A5011E1923A920212DC1E8BEDA2E86DF3AA2FB06534EE66B73D51FB1AE1A66C509E0A7938E98278BBCD83549C2BC021363D37514FA0DF022ECD18A209A296344B2B8328876DDBDA3BABFF21279A240B0FE7C534D93713E3A7486872B341C254F08539C2DE1E36B7DD83BCD8F83A53E1C29F562643AC3FC7D4A546D7DDF20115E97119A735905F32AEAA13C11CBAAF91A3BBF604E0D4DC8E47215DFD405A30479C877FD22A3082FE081C102D6FD2DE0DDDF03E7D6EC8C5C0F168E62AE855C56C11A899CBE2B887DED17161BA6394865E2CF3A66EDEEC2FA3A154D946FB8782EAE83F27E597DC803DE3D4681AADEA9B49E2A92D19F529F1BAC9124D20B80307C6059B449E94FD9C847166EA542A35E7136FD7CD3B2ED661923683B79A45DC7F8724A79B434CE4362BE0088840B3B4551FD58A740551B435D84584E832A60F20E15B37F0A60FA4EF54262A34858C16E68A225F035D5F5FCD32D3D5AF0A590129CECFC3F05E778A3B6BB4987F5D6C61FD268094E97580D75F6907F4EBA78288DB78FCC101A79DB123C3A1D47696FFE1B6F2723B4F1D8BA69A27C9AB72DC1DF4CF06A037CEC1DB27C646E4F0A240625CFCFDEB10F9DA0B570B4B83643C2F264124D5A82591EBC483BADC3769723F8E102C9CC4B632FF5F7E22DF279E7F5420F853657A7C86BE7667BD84ABBCB226E013A6AB332E4A46ACE550D4CECD4916DBFF34E78C87DF9BF1E36FA68CD92D85507A77B5BED532952A4489122458AFF1CE01FDE3B8B1BA4AF49CB0000000049454E44AE426082",
["MyQ_garage_closed.png"] = "89504E470D0A1A0A0000000D494844520000003C0000003C0802000000B59E4E25000000206348524D0000870F00008C0F0000FD520000814000007D790000E98B00003CE5000019CC733C857700000103694343504943432050726F66696C65000028CF6360609260000226010686DCBC92A22077278588C82805062490985C5CC0801B3032307CBB062219182EEB32900E3853528B9381F407202E29025A0E343205C8164987B02B40EC2408BB07C42E0A097206B21700D91AE948EC24247679494109907D02A43EB9A008C4BE0364DBE4E6942623DCCDC0939A171A0CA423805886A1982188C19DC18981CA00119EF98B18182CBE3230304F408825CD6460D8DECAC020710B21A602F4037F0B03C3B6F3058945896021165024A5A531307C5ACEC0C01BC9C0207C8181812B1AD30E445CE0F0AB02D8AFEE0CF94098CE90C3900A14F164C8634866D003B28C180C180C19CC004CA540911C77C180000000097048597300000EC300000EC301C76FA864000008D1494441546843ED987B5054F715C77FF77DEFBEB8B0800245449028E2AB8960F015C5B4B668747C66B49969679A69664CA7D34E3B7D4CA69D699B3A9DC9D4B6A67F74A6CDA4ADD38A8D6D34D5341A48A368048CF1850A2A08082AAF5D76F7BE1FBF1ED8AB71896C88B01B9DE13B67D8FBD8BDF7B3E79EF3FD1D96C018A3474DA4F3FA4869023A599A804E9626A093A509E864693C5744B8507F583A507BFA64E3B59B7D03695EF7BCE9B9EB963E9137C94F386F191F8D0FB46A9AEF9FBEB4A7BABEE6D48599F95F5855569293912A297AFDA596C327CF9514E66E5951F695F279A28B773E30368D09DAC2F8E2F5CEBDD5F56F1E3D45D3F4FA658F6F5A51F658EEE47BF31A94D583273EAA7AB7BEA9AD7365E9EC2D15650B6715B014E59C7E208D121ADEF33109ECDC0A84F61FFBF09F35F5AD5DDD4F97CED95251BA687611478F88021FB9D6D5FDC67B0DFFFA5F036C6F786A0144614EE68395CD67CBB4AC1B35A71AA10C6ACF5E2E29187CE8958BE6A5795CCEE95148B72CA8F83DD527ABEBCE17E5E5C0B7AD5C343FD52D38A747A75141DB089DBDD25E555377E0D8873CC76D5ABE60E38AD282AC4C720CFD1594948327CE5455D735B6743C5D36E7D98AB245731E634677C54F81EEEA1FD8F75EFDDE9A86AE9EFE554FCE85C4949714D163818D15DCBBF5666F55CDC97D350D966D6F58BE607345D9F4EC4C82B8F716F0AE983BDE1F3AA2EA471ACE5755D79F38D7347F463EA4E1ABE5F35284F1E9FDFBCAB0F107E79BF754D781DB14E5654376562F9AEFF7BA9DD3B11A0E7DB33FF49B3D6F83D7A67ADDD176C9CF4A1FB7C48E42014939345436175B3B2B9E28FECEE655C57959CEB9BB02E8BBEAEA0BCEFDFA4B5FFBC51F8F9FBFA29B60689F9B6C8C9B6FDCFEE96BFF9EBEE5FB0D4DADCED13B8A817EE94FFBD6FEF877A60D1F7928041C3B76FF07908601C5CC1EE7AE753C357F0615D3049FA780A3B27CEE4797AE5ACE014731D0968D69F2E11AA1589AB42CB0DC183DB4535E3C238E718FD53FFAEDAAD29217D7AF8CEE06C2F2E1538DD1ED640A4A7492E88D6E3775DCACD8FEF2F503AFD2D1FD217D027A41C98B1B1CE865EBBF4171BC989669604B8A28BCC743F2AC44D96145362D43A06986A5B0A11B86866CEC72B91445B10992E3044CD1B68D2C8280552812EC49F1BAB14D50146513C8B22C4452E17098E55D3CCFEB86651806C771360821BF2088A6D9D7D1515DF55A94E1BED0F76E0F17CDB1DF7AE185A5CBE65E6B8FDCEAEE55548D623931D52F490AC6968B65E02F7C3E3B6752562AEAB86D3637379B88F0FBD315C3044A9AE335594AF5095E816F6B6B5BBC7826A4A7B6B62977EA545996C3218924493056B81149D39AA6310C979B95E9A2B4DFEF7C250A7047C34B251E744F6FAFCBEBA9ADBBF2F28E1D614926289A26585DB36892730B0C643718E84D4B11376E58BB6D5BE5EE3FFFEDE8D15A55D3D233B383A1011B93BCDB25054372A0C7E776312CBBF4ADD7E19AAFFCECD79AA1472291CCC9D9905CC3B41886216806BE063CABA2C2BC5D3BBFA7EB7A146048403CDCCDE235A22F55CCCAC9D64DB3A3F386A428244DD96EEE3AA9B4B88C1697DDCC68ADA47CCD6505727C0101B579518B605D66D40B2804711E0D5CA2E42646368B73DA53D04552BACAA366015DA0C2ED3ED29A99DB48842F92914652BA442B702988AB9CD14E1B6184BABBBB1D8211142FD390B0DEBEBE2953A6AC59B7166ABA3710504D3C63D66CD5C00432916DEBDA34D1973289E434194D1733B4A2E2B024092E9FA2A998A05D1E37D64D5D0DBBB3A6B8DD6E9F3998B467CA9787654955D582F42CA87258C8A048385680EC323C979F9509436A6161A1433082E241C3454F9F3D33BDA8B86CE1C28C8C4936465ECEADF5CBB60CD9D76804A398C59294C72BB0ADC10D25A55F9E5AA21A3A45B2163C3F4C2386A2498AB054CB00243D74A6CB46C4B6F2E50CC74349289A210882669820E8C8C146C4C8E3E5DEDA7FB4BDBDDD211814D4C6F09A8EE71E73566F9932759A65930352C4EFCF18902455373C622A49D004699136869A44E023A6620F0D2A2EC16340FE2D7010C63490661A307C333456E588C7E7EBEBEB836AF4FBFD03A13094AFAC2A5EAF17D0C173585E8004402F72C8CE1198B6A6CB7507F746199A3ABA2AB6FF6A987BC4CC1E953FDCB9EB8D23CE0EC6AB2B9FAB3B7E018355681887305687C204CF485440B6E13ECF7DFB270E01C697DB3B73D66C379C3D47F11A51107D9E3411B1834564463B181C6A70032C352101D76660E966E196F1140FFA46EFEDD69E9B3D0A0E1A48026E0E596E0A9126226181484840834BFAA7BB47DC4CA7A68899E9AC1B6A14113C8AC0BF08918884680531090A19219E45E0570EC1088AD788B32AD66CDDB679DAB469D19F06BC6E4E333014881DD773C62212A3487FF0AFAFFFA57ADFE04A04BA6F23C6832E787265D9C2C729440482BDA66E88296E0C69C6344663FAA9258E281B65FAC5C387FE7BFDCCB1E891FBCE1EF1DC63F1335BDFA93F17C118022C04226C620927366E69F8F91FFCDC217800F770D1ACC0B0F0C8E04D9A64437A050209302A612341C1C3FAC5A25028E4108CA078D0942DD3A6C2012BCC21ACCE60832234021B085B090A120D2EA6B0463A0423281EB489290D13E0D083D760784C3011D9D6485E4D58280405A32AAC9A51809114AF11E7AD58F7ECE64D05F9F92C4B9204E1E2B888AA9B94601109730F647777B655FDE3EFEFEFDF1D3D32D488BFBC7EE00FA3758FDC2F562C59B204869B50B01FE65D8E6534DDA038971DF7F98C45005D9897F3CEDB879AEADE8D1EF9CCEEB16CE3F36F1EBFD826E33E8CFB311E181A40C29FE8F7710CB8785B40DFF8CDEF3A040FE01E0C8933448F5F40D08B14583442968279E84B8C131470F174911145D121184171DDC3502C750070019A336CF010518072B609022728C055FB826A301874081C7D5CC0510D878691E5AE486CD0B6C9118339E6187BC8EC607046062613141A467E91F7F97C0EC1A0EE01BAA3E18D286B6671DEE4E8EE078D2DADB760724FB6D23CEE5565C5D1ED0149ADA93BDB7660D788EE71E4D48573576F383B434F65E86BDE794DAEEEDE75729A6FEB97CAEF2588817E549428C74DA826A093A509E86469023A599A804E8E10FA3F0EAB9A49F9A3171C0000000049454E44AE426082",
["MyQ_garage_offline.png"] = "89504E470D0A1A0A0000000D494844520000003C0000003C08030000000D222940000000206348524D0000870F00008C0F0000FD520000814000007D790000E98B00003CE5000019CC733C857700000103694343504943432050726F66696C65000028CF6360609260000226010686DCBC92A22077278588C82805062490985C5CC0801B3032307CBB062219182EEB32900E3853528B9381F407202E29025A0E343205C8164987B02B40EC2408BB07C42E0A097206B21700D91AE948EC24247679494109907D02A43EB9A008C4BE0364DBE4E6942623DCCDC0939A171A0CA423805886A1982188C19DC18981CA00119EF98B18182CBE3230304F408825CD6460D8DECAC020710B21A602F4037F0B03C3B6F3058945896021165024A5A531307C5ACEC0C01BC9C0207C8181812B1AD30E445CE0F0AB02D8AFEE0CF94098CE90C3900A14F164C8634866D003B28C180C180C19CC004CA540911C77C1800000024F504C5445FFFFFFFEFFFFECF2F49EB6C2668B9F4D78905F859B8FAAB8DFE8ECF7FBFBB8C9D260869C1E527047738CAABFC99FB6C3698DA12355734F7990A6BBC7EFF5F7FCFEFED2DEE37C9BAD2C5D7930617D82A0B0D7E2E7A4BBC64D77902658756A8EA2C1D1D9F9FCFCFFFFFEE6EEF197B1BE426F88215572668BA0BDCED6F8FBFCDBE5E987A4B435647F34647FDAE4E9F5F9FAB3C5CF5B82991F53714B768EA2B8C5EDF3F5698EA12657744C768FA3B9C5EEF4F6FDFFFFCEDAE17798AA295B7731627E86A3B3DAE5E9FDFEFFEFF4F6A4BAC64D778F225572678CA0BFCFD7FAFDFEE5EDF092ACBA3C6A85225573C1D1D8FBFDFEDDE6EB86A4B333627E30607C83A1B1D9E4E8FEFEFEF3F7F9AEC2CC577F961D516F4E7990A6BCC7F0F6F7C1D0D849748DA0B7C3E6EDF17495A82659763766818AA6B6235674668A9ED5E0E6E6EDF02F607C5C8399C5D3DBFAFCFD7E9EAF255774CBD9DF7C9CAD4F79918FAAB91E506DFBFFFF4B778F93ACBBD8E2E51B4D6B93ADBBF0F5F7F7FAFBD8E2E6809EAF1B5373567F956E92A6114C6E45738C1B4D6A154F70537D968DA9B9A5BBC8BCCDD6BBCCD69AB3C17899AD124D6FE9EEF17D9DAFD8E2E7B1C4CF8AA7B78EAABAB7C8D34B7891A7BDC947748E5C849CC0CFD8C1D0D9F3F6F89EB6C41E5575D6E0E6DBE4E95781999DB5C3497690F4F7F8D7E1E7134E6F809FB1DEE6EB44728D6F92A65D859CD5DFE5BECED7275C7B6F92A57A9BAEB2C5D0DAE3E8B9CAD4E3E9EDBFCFD8F0F3F5E3EAEEC7D5DD92ADBB1D557541708B6B8FA3356683255A7A5C849B1A4D6CBACBD38CA7B78BA7B78CA7B6A4B9C5698B8029000000097048597300000EC300000EC301C76FA8640000022349444154484BEDD6E95F0C711C07F0EF24965C45E52A9523C756EE2B49682392DC29B96F72849C09398B945B6E96721F15D6BA36FE30BF99F98CFDB5B3EC77EA8127FB7E30FBF9FE663EAFDFCCEC3E580A0AFA4F94900EA11D3BD93A2B982DE812D6B55B775B8F9EE111BD7A47628D272ABA4FDF7EFD07687B46C6C40E8C8B4FD0D6FF69907A50060F199A386CF8086D45A58CB42725DB5318B73F6AF498B1E3C64FC06448889F3869F294544CFE4D4D9B963E3D63C64C8CADCCCA7464CD9E938DC964EEBC9CF971B90B309929790B172D5EB274999AB507FC6379FE8A82C2954598FE267B5578C4EA35219860EDBAF51B36E6B1BED1D44CC7A6CD5B30A8B66EDBBEA31839302565E7AEDDC8C29E9228241EC55EE2BDCBC2BD085CD1FBF62311951E40E03A78A8AD65F135B52A1F1687E2233C47C5B565BE3B1F2BE7392EAE35954F9CE4A910D7969D0A503E7DE6ECB9F39555172E6236A865D30BF32DEBA788AA2F610198E59ADACB57AE8A33D7B0A26395AFDFD03E6E12DDD28281F5CC863ABA8DA463ED6CB84374175163A97C8FEE23E9AC941F103D44D459283F7A4CCE27C83A7EF929517D0332F82D3FC349C9F317442F5F6130A865D36FFB354E4A6A89DE207A71777E4BEF9024DCF27B6A449270CB4DCD1F9024DCB25FDA0BF3FD6D5B2A3376FEE8742249B8B75D438424697F597EE64FE2E0A7EC72B99024A617F6D9ED767FC1C940BE8A6BBF7DF7967F788416CF4FF583A1C5E3F9D5863F584141D610FD06ADF08807204183EF0000000049454E44AE426082",
["MyQ_garage_open.png"] = "89504E470D0A1A0A0000000D494844520000003C0000003C0802000000B59E4E25000000206348524D0000870F00008C0F0000FD520000814000007D790000E98B00003CE5000019CC733C857700000103694343504943432050726F66696C65000028CF6360609260000226010686DCBC92A22077278588C82805062490985C5CC0801B3032307CBB062219182EEB32900E3853528B9381F407202E29025A0E343205C8164987B02B40EC2408BB07C42E0A097206B21700D91AE948EC24247679494109907D02A43EB9A008C4BE0364DBE4E6942623DCCDC0939A171A0CA423805886A1982188C19DC18981CA00119EF98B18182CBE3230304F408825CD6460D8DECAC020710B21A602F4037F0B03C3B6F3058945896021165024A5A531307C5ACEC0C01BC9C0207C8181812B1AD30E445CE0F0AB02D8AFEE0CF94098CE90C3900A14F164C8634866D003B28C180C180C19CC004CA540911C77C180000000097048597300000EC300000EC301C76FA86400000634494441546843ED990D4C13571CC0FB716DA10845045ABE56F103118B8008551AA75034F8350DA8DD1C5B66748B9BCBCCA2D19939E7A2C832CD32A62ECE194D74CB0A7E339999D2AAA00805658208288A8814442DDFB4B4BD768FF6C9DAE328EDB55648FACB85DE7BFF7777BF7BF77FEF3E20EBF57AD26883027F47152E6967E19276162E6967E19276168EBC23821D29BA7A72AFDF2EAE7AD8FCB2C3C7D3237A72C8F2B76772D9E3C8B08963708CB44AABBD76BB5A2C9149CBEE4E0D0D4EE1F382FCC6F628D5B2EA47978A2B7893424449FC8509D1DE4C37B8817DD8258DEAF5F71E37E54864E70ACA1004499D1BBB32893F258463DAAFEDBDAABCA2F2EC7C596D4353727CA448C89F356D229D4A85614258290DDAFC6F020A2D6D9DE70B6F9D94CAEAE5ADF3E3A78B84F182C8300632A40AD8E4A1BCF5D495D233574BC17ADABC38B04C0AF2279636B6F574AF5A232DAB026970FD4E0D6F62FF455F2C88F619C384612B50A328C878B1A458525219C60D0267BB581033D6C31D86ADC32A691D8974E7C1936C69496EE12D3706636562DC8AA4F88901FE143BC6577B8F32AFE8DF6C4949D5A3C6F9FCE9EF0AF982E95368D6ED711869B9A2E3F415598EB454FE5C91323B0A744C022F0CB147D61C70ECFAE617D9D2E2D3D25254A74B4B8C5B25E44F0EF427934D0F015A991D115FBA5BA5BE5C5A992D911555D4C68487826E589410CD7277CCD8C745A3D3DFACBC2F969480D9268C1B087A678920669CA7070C9B83956E5674FE28BE08E6DAB19E1EC6E1121AE0EBB08EB582B61EE5DF86B4B957DF249C19B171554A043700C60600D203C85FB6477DB43D7DD7AF372A1FA8B560427B63E8F4FAFB4F9FED387A76B26873696D3DAC7D8599F4F623A7976DCBD2EAC0262302E091F9FB05A08411327BF6A878D8382F269C6A3608DE24C06371425479751D0A2B2066D2A84E8F5046D623141DA1A0289872CD18B14F799626E241D226A9A141751DBD2AE72F2A8DD6C4032757CDA6BC255FFD9412C7FB3C2DD958FCEED8B9DFF2AE1BD79D898037217BE767C6F5DAC666E1868CC7B9071063D980A5F450742B7BFB34CE5FDABA95D000824D95119BD30300636C868C7C691C5CD2AF05901BD89C361D94046130E889B1D3C607FA07F8B2383E2C260D61D0A81432B94F8B829786D6B6AE1645D79396D682F29AB68E2EB88D0DBC869CE606B1AF1DFC3A73FD8AF405B385332278E38343833881FEBE1C3F5F6E007B2A37686E74B82829EEDB35CB8B0F7F93103D156E661FF6F6F4AE75A9540A89BF76A75AA3815578D0E93449D6D6AC2FDE9BF36986AAAF0FD612C5AE9E4E888958181B7EE8AF42CBC600B55A73E842E1043FD6FB8BE6C02A3B2022EDEEC610C4F2D6BC9398B96E59BBB22FE752110C58E48CA4B851D1B9292D697D6A72D2AC281F6F160CD80E91F410CD9FB5EF9354303AC0F2ACB367EE8C08EDA007B1C120548A1645DFF2F1DAB36629687D34BF6C4BD60918B3114BD2D841FB8AF305B7552869DFDAA54C3A8DEDE5717CCB07D83969088C3B04C67B7224D9F9370DA561C1B1B09C1EF83260E68A9FC205C6B06CD8B1358B1170C845F1D39A9A9FC3F230E03858961E38901908424D9E110E0B8488E4B283397EB0603B4406228346F3F36282AB6C655660001B8237BA60FF71B06C3B44A4C13B244221EF165F5A95716CFB898B7F1694D7B576C0D810C8DBBB7365D51939F9ABBF3FFEE10FFDE38F41237E8B20220D0079932E8C678D614A4AAB36EF17CFDBB0A7BCA105C606F1B2B72F71E3DE8F338F9CBD7A8B4CA17CB952889F76F880B6D82B4A501A30C1CFFBC84651D1CF9B1F8833A507B6057A7BC2C020DC11CAA9DD1BEA4EEE95FDB2F58F2DE9B1A18130602DD873242E6D04EC8F49A584B1BDD92CFC4F580030CF4486F87B127CD1B779F6B00A30B050C35FDC71092A8D0D30DF2EEC81E06830F5DB71224F5A56E34EA7B931680CA4FF521ABE79825766600B5EE9F52AB556A9D64C0AE61CDDB47AA09370CF7008B06DB1D27A6CFEE0A0526B0EE6DD60B9D3C1BA4E4FBA78B3B2BEB1D918B24063CB8BC3FF14BBD3FAFF5BA04675350D4DC6FAE1C011C27E42E8EDD3467039C6A2ACFA719DDCCAFB9623F1F51AB3200E3E7977F4A8A425771A72F79BF6AE99F4E5B2BB15754F61C170550CA7F9EAD7B90C1C95E3E3B57A4182A901F6FBF4A8C001B387F371493B0B97B4B370493B0B97B4732091FE031F2C57844B95DC0B0000000049454E44AE426082",
["MyQ_garage_opening.png"] = "89504E470D0A1A0A0000000D494844520000003C0000003C0802000000B59E4E25000000206348524D0000870F00008C0F0000FD520000814000007D790000E98B00003CE5000019CC733C857700000103694343504943432050726F66696C65000028CF6360609260000226010686DCBC92A22077278588C82805062490985C5CC0801B3032307CBB062219182EEB32900E3853528B9381F407202E29025A0E343205C8164987B02B40EC2408BB07C42E0A097206B21700D91AE948EC24247679494109907D02A43EB9A008C4BE0364DBE4E6942623DCCDC0939A171A0CA423805886A1982188C19DC18981CA00119EF98B18182CBE3230304F408825CD6460D8DECAC020710B21A602F4037F0B03C3B6F3058945896021165024A5A531307C5ACEC0C01BC9C0207C8181812B1AD30E445CE0F0AB02D8AFEE0CF94098CE90C3900A14F164C8634866D003B28C180C180C19CC004CA540911C77C180000000097048597300000EC300000EC301C76FA86400000634494441546843ED990D4C13571CC0FB716DA10845045ABE56F103118B8008551AA75034F8350DA8DD1C5B66748B9BCBCCA2D19939E7A2C832CD32A62ECE194D74CB0A7E339999D2AAA00805658208288A8814442DDFB4B4BD768FF6C9DAE328EDB55648FACB85DE7BFF7777BF7BF77FEF3E20EBF57AD26883027F47152E6967E19276162E6967E19276168EBC23821D29BA7A72AFDF2EAE7AD8FCB2C3C7D3237A72C8F2B76772D9E3C8B08963708CB44AABBD76BB5A2C9149CBEE4E0D0D4EE1F382FCC6F628D5B2EA47978A2B7893424449FC8509D1DE4C37B8817DD8258DEAF5F71E37E54864E70ACA1004499D1BBB32893F258463DAAFEDBDAABCA2F2EC7C596D4353727CA448C89F356D229D4A85614258290DDAFC6F020A2D6D9DE70B6F9D94CAEAE5ADF3E3A78B84F182C8300632A40AD8E4A1BCF5D495D233574BC17ADABC38B04C0AF2279636B6F574AF5A232DAB026970FD4E0D6F62FF455F2C88F619C384612B50A328C878B1A458525219C60D0267BB581033D6C31D86ADC32A691D8974E7C1936C69496EE12D3706636562DC8AA4F88901FE143BC6577B8F32AFE8DF6C4949D5A3C6F9FCE9EF0AF982E95368D6ED711869B9A2E3F415598EB454FE5C91323B0A744C022F0CB147D61C70ECFAE617D9D2E2D3D25254A74B4B8C5B25E44F0EF427934D0F015A991D115FBA5BA5BE5C5A992D911555D4C68487826E589410CD7277CCD8C745A3D3DFACBC2F969480D9268C1B087A678920669CA7070C9B83956E5674FE28BE08E6DAB19E1EC6E1121AE0EBB08EB582B61EE5DF86B4B957DF249C19B171554A043700C60600D203C85FB6477DB43D7DD7AF372A1FA8B560427B63E8F4FAFB4F9FED387A76B26873696D3DAC7D8599F4F623A7976DCBD2EAC0262302E091F9FB05A08411327BF6A878D8382F269C6A3608DE24C06371425479751D0A2B2066D2A84E8F5046D623141DA1A0289872CD18B14F799626E241D226A9A141751DBD2AE72F2A8DD6C4032757CDA6BC255FFD9412C7FB3C2DD958FCEED8B9DFF2AE1BD79D898037217BE767C6F5DAC666E1868CC7B9071063D980A5F450742B7BFB34CE5FDABA95D000824D95119BD30300636C868C7C691C5CD2AF05901BD89C361D94046130E889B1D3C607FA07F8B2383E2C260D61D0A81432B94F8B829786D6B6AE1645D79396D682F29AB68E2EB88D0DBC869CE606B1AF1DFC3A73FD8AF405B385332278E38343833881FEBE1C3F5F6E007B2A37686E74B82829EEDB35CB8B0F7F93103D156E661FF6F6F4AE75A9540A89BF76A75AA3815578D0E93449D6D6AC2FDE9BF36986AAAF0FD612C5AE9E4E888958181B7EE8AF42CBC600B55A73E842E1043FD6FB8BE6C02A3B2022EDEEC610C4F2D6BC9398B96E59BBB22FE752110C58E48CA4B851D1B9292D697D6A72D2AC281F6F160CD80E91F410CD9FB5EF9354303AC0F2ACB367EE8C08EDA007B1C120548A1645DFF2F1DAB36629687D34BF6C4BD60918B3114BD2D841FB8AF305B7552869DFDAA54C3A8DEDE5717CCB07D83969088C3B04C67B7224D9F9370DA561C1B1B09C1EF83260E68A9FC205C6B06CD8B1358B1170C845F1D39A9A9FC3F230E03858961E38901908424D9E110E0B8488E4B283397EB0603B4406228346F3F36282AB6C655660001B8237BA60FF71B06C3B44A4C13B244221EF165F5A95716CFB898B7F1694D7B576C0D810C8DBBB7365D51939F9ABBF3FFEE10FFDE38F41237E8B20220D0079932E8C678D614A4AAB36EF17CFDBB0A7BCA105C606F1B2B72F71E3DE8F338F9CBD7A8B4CA17CB952889F76F880B6D82B4A501A30C1CFFBC84651D1CF9B1F8833A507B6057A7BC2C020DC11CAA9DD1BEA4EEE95FDB2F58F2DE9B1A18130602DD873242E6D04EC8F49A584B1BDD92CFC4F580030CF4486F87B127CD1B779F6B00A30B050C35FDC71092A8D0D30DF2EEC81E06830F5DB71224F5A56E34EA7B931680CA4FF521ABE79825766600B5EE9F52AB556A9D64C0AE61CDDB47AA09370CF7008B06DB1D27A6CFEE0A0526B0EE6DD60B9D3C1BA4E4FBA78B3B2BEB1D918B24063CB8BC3FF14BBD3FAFF5BA04675350D4DC6FAE1C011C27E42E8EDD3467039C6A2ACFA719DDCCAFB9623F1F51AB3200E3E7977F4A8A425771A72F79BF6AE99F4E5B2BB15754F61C170550CA7F9EAD7B90C1C95E3E3B57A4182A901F6FBF4A8C001B387F371493B0B97B4B370493B0B97B4732091FE031F2C57844B95DC0B0000000049454E44AE426082",
["MyQ_garage_closing.png"] = "89504E470D0A1A0A0000000D494844520000003C0000003C08030000000D222940000000206348524D0000870F00008C0F0000FD520000814000007D790000E98B00003CE5000019CC733C857700000103694343504943432050726F66696C65000028CF6360609260000226010686DCBC92A22077278588C82805062490985C5CC0801B3032307CBB062219182EEB32900E3853528B9381F407202E29025A0E343205C8164987B02B40EC2408BB07C42E0A097206B21700D91AE948EC24247679494109907D02A43EB9A008C4BE0364DBE4E6942623DCCDC0939A171A0CA423805886A1982188C19DC18981CA00119EF98B18182CBE3230304F408825CD6460D8DECAC020710B21A602F4037F0B03C3B6F3058945896021165024A5A531307C5ACEC0C01BC9C0207C8181812B1AD30E445CE0F0AB02D8AFEE0CF94098CE90C3900A14F164C8634866D003B28C180C180C19CC004CA540911C77C180000002A3504C5445FFFFFFFEFFFFECF2F49EB6C2668B9F4D78905F859B8FAAB8DFE8ECF7FBFBB8C9D260869C1E527047738CAABFC99FB6C3698DA12355734F7990A6BBC7EFF5F7FCFEFED2DEE37C9BAD2C5D7930617D82A0B0D7E2E7A4BBC64D77902658756A8EA2C1D1D9F9FCFCFFFFFEE6EEF197B1BE426F88215572668BA0BDCED6F8FBFCDBE5E987A4B435647F34647FDAE4E9F5F9FAB3C5CF5B82991F53714B768EA2B8C5EDF3F5698EA12657744C768FA3B9C5EEF4F6FDFFFFCEDAE17798AA295B7731627E86A3B3DAE5E9FDFEFFEFF4F6A4BAC64D778F225572678CA0BFCFD7FAFDFEE5EDF092ACBA3C6A85225573C1D1D8FBFDFEDDE6EB86A4B333627E30607C83A1B1D9E4E8FEFEFEF3F7F9AEC2CC577F961D516F4E7990A6BCC7F0F6F7C1D0D849748DA0B7C3E6EDF17495A82659763766818AA6B6235674668A9ED5E0E6E6EDF02F607C5C8399C5D3DBFAFCFD7E9EAF255774CBD9DF7C9CAD4F79918FAAB91E506DFBFFFF4B778F93ACBBD8E2E51B4D6B93ADBBB8CAD4A8BECAF7FAFBD8E2E6809EAF42708A88A5B689A5B5648AA0557E946E92A66F92A3B5C8D244728B1B4D6AB5C8D1265B7A47738D366783114C6E6D91A6AFC3CEB3C6D0B5C7D2B6C8D2BACBD5517C95114B6D9CB4C26A8FA4124B6C9AB3C196B0BF7194A86F92A693AEBD7396AA45738C6F92A5134E6F144E7092ADBD7B9CAE1952731A53731B53741851725D859CD9E2E8E2E9EDE8EEF1F4F7F8FAFBFCF6F8F948758F376885C8D6DEFCFCFDA1B8C61F567616507182A1B3F7F9FAE9EEF15C849C3D6D89D3DEE4B8C9D3285D7C1750718EAABAF9FAFBF2F5F77295A9124D6F47748ED8E2E7CAD7DF3668841C547498B1C0FBFCFCF8FAFB89A6B7507B94E0E7ECDAE3E892ADBB1F57777698AB1E557541708B6B8FA3356683255A7A5C849B1A4D6CBACBD38CA7B78BA7B78CA7B6A4B9C558822F63000000097048597300000EC300000EC301C76FA8640000026649444154484B631805A36080002313330B2B1B3B0723944F02E0E4E2E6E165E7E31710141216818A110744C5C42524A5A4C1768AC8C8CAC92B2882C5F1022510C1A8ACA2AAA6AEA109160101462D6D1D5D6D3D229CAF6F6068646C620AE5C180A28299B985A51594871D58DBD8DAD93B383A41B928C0D9C5D5CDDDC313CAC3005EDE3EBEF27EFE501E26600C080C0A0E090D03B1C11E8483F088C8A8E89858280F17F08C13108C4F6082F2A020312939253580A818B572714D4BCF80724020332B3B2717CA260C18F5F2F20BA06C20282C1285B288038CDA45085746174319C402B19252288B81A1AC1CCA2016545492AB19184D289AAB80446E750D51A016A8B60EDDE6FA86C62682A0B1A9B905A81643736B1B844300B4770089BA4E0A34630418919ABB7068662406E0B0B9BB8718D08BDDCF7DC8A07F02104C9C3469D2E42953A642C5C0601A769BA1B210301D22030633A06260309308CDB3203260D00F1503036234CF86C880C11CA818180C9C66EC0136172A0B01F3203260301F2A06010B802218697B215472D1A2458B172F5E02910183A5CB962F5FBE6225541EA419A7CDAB56AF59BB76ED3A880C18ACDFB07EFDFAD51BA1F27835F76DDA0C2E9A51C096ADDBA0B20434F76DDFB113220E07BB7643A5808080E6BEBE3D7B912D0FDBB71F2A0E02E000434FDB28A17DE0E0218814101C3E72142A0A0660CDF86C068263C7A1969F380915810282CE068153A7CF0045CF9EDB0EE5C300519AFBFACE5F08BB088B2004006B46F6F3252081A9B9EFF295AB501612C008B06BD7AF5FBF019524046E02D5DEBA8DD07CE72E10DCBB7B1F441101EEDDBDFB808C06D6281805A401060600DF672B0ECE90EB850000000049454E44AE426082",
["MyQ_garage_stopped.png"] = "89504E470D0A1A0A0000000D494844520000003C0000003C08030000000D222940000000206348524D0000870F00008C0F0000FD520000814000007D790000E98B00003CE5000019CC733C857700000103694343504943432050726F66696C65000028CF6360609260000226010686DCBC92A22077278588C82805062490985C5CC0801B3032307CBB062219182EEB32900E3853528B9381F407202E29025A0E343205C8164987B02B40EC2408BB07C42E0A097206B21700D91AE948EC24247679494109907D02A43EB9A008C4BE0364DBE4E6942623DCCDC0939A171A0CA423805886A1982188C19DC18981CA00119EF98B18182CBE3230304F408825CD6460D8DECAC020710B21A602F4037F0B03C3B6F3058945896021165024A5A531307C5ACEC0C01BC9C0207C8181812B1AD30E445CE0F0AB02D8AFEE0CF94098CE90C3900A14F164C8634866D003B28C180C180C19CC004CA540911C77C18000000207504C5445FFFFFFFEFFFFECF2F49EB6C2668B9F4D78905F859B8FAAB8DFE8ECF7FBFBB8C9D260869C1E527047738CAABFC99FB6C3698DA12355734F7990A6BBC7EFF5F7FCFEFED2DEE37C9BAD2C5D7930617D82A0B0D7E2E7A4BBC64D77902658756A8EA2C1D1D9F9FCFCFFFFFEE6EEF197B1BE426F88215572668BA0BDCED6F8FBFCDBE5E987A4B435647F34647FDAE4E9F5F9FAB3C5CF5B82991F53714B768EA2B8C5EDF3F5698EA12657744C768FA3B9C5EEF4F6FDFFFFCEDAE17798AA295B7731627E86A3B3DAE5E9FDFEFFEFF4F6A4BAC64D778F225572678CA0BFCFD7FAFDFEE5EDF092ACBA3C6A85225573C1D1D8FBFDFEDDE6EB86A4B333627E30607C83A1B1D9E4E8FEFEFEF3F7F9AEC2CC577F961D516F4E7990A6BCC7F0F6F7C1D0D849748DA0B7C3E6EDF17495A82659763766818AA6B6235674668A9ED5E0E6E6EDF02F607C5C8399C5D3DBFAFCFD7E9EAF255774CBD9DF7C9CAD4F79918FAAB91E506DFBFFFF4B778F93ACBBD8E2E51B4D6B93ADBBF0F5F7F7FAFBD8E2E6809EAF1B5373567F956E92A6114C6E45738C1B4D6A2259787D9DAF46748ED5DFE53E6E89CDD9E0376885C6D4DC306381BFCFD8295E7CB8C9D3B0C4CF1B5374A9BECA134E6FA2B9C69BB3C26F92A694AEBE6F92A58DA9B986A4B57F9EB1C7D4DC7899AD326582E6ECF092ADBB41708B6B8FA3356683255A7A5C849B1A4D6CBACBD38CA7B78BA7B78CA7B6A4B9C598027B9E000000097048597300000EC300000EC301C76FA864000001F349444154484BEDD6F95F0C711807F0679222A4E840E870444B5222458E0EE5ACC8518AEE508E9CA1245A92A384D0A16CAA2D7F64DF99D787676B46FBCCFEE2977DFF30FB7966E6F3FACEF5C3929FDF7FA2052C0A5C1C14BC44C36CC3D29065CB570487AE0C0B5FB53A02FB6422A3A2D7AC5D1763AC19B17EC3C6D8B87863FF8212F48DB669F396AD89DBB61B7B745A9263C74E47B2E0F277A5EC4E4DDB938EE98FF8B8BDFB32F66762B29675E060F6A1C3478E629C2327372FFF5841212693E3274E9E8A3D7D06939956545C72F65CE9793D1B37F8D7858B97CACA2F5760FA97C2CAB0F02B5703304155754D6D5D91E88D66E6E6D5373462D05DBB7EA3A919D93B2DF9E6ADDBC8CA9D96482419CDD1C257597E17412AEADE7D24A2070F11A41EB5FA5A56AF694EF9B1DA343F9179AACE6D9BBF72FB33990E75AEA9FCBC53E6853AB7EDA5F77297D3E944647AD9F4C02CCAAF885E233269B99BE80D2293967B88DE2232E93DBF237A8FC8A42B7F20EA4564D27217511F2293963F12F5233269F913D16744262D77127D416296E5011CF444F41589E965D3B7FD0D073D117D476236561E446236CA43484C5C1E1E1E4162E2B215E381CDFFB66D95052BFF181D157E615665F1E769ABEC79CF636AE3F303FBE972B9C671D09B5FEADC89492E4FB99569F78CFE2330ED76FFF6E10F969F9F3D44B392A97F5DDCB84B640000000049454E44AE426082",
["MyQ_garage_faulted.png"] = "89504E470D0A1A0A0000000D494844520000003C0000003C08030000000D222940000000206348524D0000870F00008C0F0000FD520000814000007D790000E98B00003CE5000019CC733C857700000103694343504943432050726F66696C65000028CF6360609260000226010686DCBC92A22077278588C82805062490985C5CC0801B3032307CBB062219182EEB32900E3853528B9381F407202E29025A0E343205C8164987B02B40EC2408BB07C42E0A097206B21700D91AE948EC24247679494109907D02A43EB9A008C4BE0364DBE4E6942623DCCDC0939A171A0CA423805886A1982188C19DC18981CA00119EF98B18182CBE3230304F408825CD6460D8DECAC020710B21A602F4037F0B03C3B6F3058945896021165024A5A531307C5ACEC0C01BC9C0207C8181812B1AD30E445CE0F0AB02D8AFEE0CF94098CE90C3900A14F164C8634866D003B28C180C180C19CC004CA540911C77C18000000207504C5445FFFFFFFEFFFFECF2F49EB6C2668B9F4D78905F859B8FAAB8DFE8ECF7FBFBB8C9D260869C1E527047738CAABFC99FB6C3698DA12355734F7990A6BBC7EFF5F7FCFEFED2DEE37C9BAD2C5D7930617D82A0B0D7E2E7A4BBC64D77902658756A8EA2C1D1D9F9FCFCFFFFFEE6EEF197B1BE426F88215572668BA0BDCED6F8FBFCDBE5E987A4B435647F34647FDAE4E9F5F9FAB3C5CF5B82991F53714B768EA2B8C5EDF3F5698EA12657744C768FA3B9C5EEF4F6FDFFFFCEDAE17798AA295B7731627E86A3B3DAE5E9FDFEFFEFF4F6A4BAC64D778F225572678CA0BFCFD7FAFDFEE5EDF092ACBA3C6A85225573C1D1D8FBFDFEDDE6EB86A4B333627E30607C83A1B1D9E4E8FEFEFEF3F7F9AEC2CC577F961D516F4E7990A6BCC7F0F6F7C1D0D849748DA0B7C3E6EDF17495A82659763766818AA6B6235674668A9ED5E0E6E6EDF02F607C5C8399C5D3DBFAFCFD7E9EAF255774CBD9DF7C9CAD4F79918FAAB91E506DFBFFFF4B778F93ACBBD8E2E51B4D6B93ADBBF0F5F7F7FAFBD8E2E6809EAF1B5373567F956E92A6114C6E45738C1B4D6A2259787D9DAF46748ED5DFE53E6E89CDD9E0376885C6D4DC306381BFCFD8295E7CB8C9D3B0C4CF1B5374A9BECA134E6FA2B9C69BB3C26F92A694AEBE6F92A58DA9B986A4B57F9EB1C7D4DC7899AD326582E6ECF092ADBB41708B6B8FA3356683255A7A5C849B1A4D6CBACBD38CA7B78BA7B78CA7B6A4B9C598027B9E000000097048597300000EC300000EC301C76FA864000001F349444154484BEDD6F95F0C711807F0679222A4E840E870444B5222458E0EE5ACC8518AEE508E9CA1245A92A384D0A16CAA2D7F64DF99D787676B46FBCCFEE2977DFF30FB7966E6F3FACEF5C3929FDF7FA2052C0A5C1C14BC44C36CC3D29065CB570487AE0C0B5FB53A02FB6422A3A2D7AC5D1763AC19B17EC3C6D8B87863FF8212F48DB669F396AD89DBB61B7B745A9263C74E47B2E0F277A5EC4E4DDB938EE98FF8B8BDFB32F66762B29675E060F6A1C3478E629C2327372FFF5841212693E3274E9E8A3D7D06939956545C72F65CE9793D1B37F8D7858B97CACA2F5760FA97C2CAB0F02B5703304155754D6D5D91E88D66E6E6D5373462D05DBB7EA3A919D93B2DF9E6ADDBC8CA9D96482419CDD1C257597E17412AEADE7D24A2070F11A41EB5FA5A56AF694EF9B1DA343F9179AACE6D9BBF72FB33990E75AEA9FCBC53E6853AB7EDA5F77297D3E944647AD9F4C02CCAAF885E233269B99BE80D2293967B88DE2232E93DBF237A8FC8A42B7F20EA4564D27217511F2293963F12F5233269F913D16744262D77127D416296E5011CF444F41589E965D3B7FD0D073D117D476236561E446236CA43484C5C1E1E1E4162E2B215E381CDFFB66D95052BFF181D157E615665F1E769ABEC79CF636AE3F303FBE972B9C671D09B5FEADC89492E4FB99569F78CFE2330ED76FFF6E10F969F9F3D44B392A97F5DDCB84B640000000049454E44AE426082"
},
file_exists = function(self,filename)
local file = io.open(filename)
if (file) then
io.close(file)
return true
else
return false
end
end,
decode_hex_string = function(self,hexStr)
local logPrefix = "("..PLUGIN_CONFIG.NAME.."::DEVICE_ICONS::decode_hex_string): "
if (not hexStr) then
log(logPrefix.."No hex data supplied.",LOGLEVELS.WARN)
return nil
end
if (math.floor(#hexStr/2) ~= (#hexStr/2)) then
log(logPrefix.."Invalid hex data supplied.",LOGLEVELS.WARN)
return nil
end
debug(logPrefix.."input size ["..(#hexStr or "NIL").."].")
local i = 1
local hexStr_len = hexStr:len()
local VALUE = ""
while i <= hexStr_len do
local c = hexStr:sub(i,i+1)
VALUE = VALUE .. string.char(tonumber(c,16))
i = i + 2
end
debug(logPrefix.."output size ["..(#VALUE or "NIL").."].")
return VALUE
end,
create_png = function(self,data,filename)
-- data = hex encoded png file contents
local logPrefix = "("..PLUGIN_CONFIG.NAME.."::DEVICE_ICONS::create_png): "
if (self:file_exists(filename)) then return false end
local png_data = self:decode_hex_string(data)
if (png_data and (#png_data == (#data/2))) then
debug(logPrefix.."writing PNG Data for file ["..(filename or "NIL").."]")
local file = io.open(filename,"wb")
if (file) then
file:write(png_data)
file:close()
return true
else
return false
end
else
log(logPrefix.."PNG Data DECODE ERROR",LOGLEVELS.WARN)
return false
end
end,
createIcons = function(self)
local fPath = "/www/cmh/skins/default/img/devices/device_states/"
for fName, fData in pairs(self.DATA) do
if (not self:file_exists(fPath..fName)) then
self:create_png(fData,fPath..fName)
end
end
end
}
function task(text, mode)
debug("("..PLUGIN_CONFIG.NAME.."::task): task [" .. text.."]")
if (mode == TASK_ERROR_PERM) then
taskHandle = luup.task(text, TASK_ERROR, MSG_CLASS, taskHandle)
else
taskHandle = luup.task(text, mode, MSG_CLASS, taskHandle)
-- Clear the previous error, since they're all transient
if (mode ~= TASK_SUCCESS) then
luup.call_delay("clearTask", PLUGIN_CONFIG.CLEAR_TASK_AFTER, "")
end
end
end
local function clearTask()
task("Clearing...", TASK_SUCCESS)
end
do
local BASE_URL = "https://partner-identity.myq-cloud.com"
MYQ_API = {
API_CONFIG = {
NAME = "MyQApi",
CLIENTID = "IOS_CGI_MYQ",
REDIRECT_URL = "com.myqops://ios",
CLIENT_SECRET = "VUQ0RFhuS3lQV3EyNUJTdw==",
AUTHORIZE_URL = BASE_URL.."/connect/authorize?response_type=code&state=&client_id=%s&scope=MyQ_Residential%%20offline_access&redirect_uri=%s&code_challenge=%s&code_challenge_method=S256",
TOKEN_URL = "https://partner-identity.myq-cloud.com/connect/token",
ACCOUNT_URL = "https://accounts.myq-cloud.com/api/v6.0/accounts",
DEVICES_URL = "https://devices.myq-cloud.com/api/v5.2/Accounts/%s/Devices",
GDO_ACTION_URL = "https://account-devices-gdo.myq-cloud.com/api/v5.2/Accounts/%s/door_openers/%s/%s",
LAMP_ACTION_URL = "https://account-devices-lamp.myq-cloud.com/api/v5.2/Accounts/%s/lamps/%s/%s"
},
login = function(self, userName, password)
local logPrefix = "("..self.API_CONFIG.NAME.."::login): "
local codeVerifier = UTIL:generateCodeVerifier(128)
--Apparently SHA256 is hard in Lua so we'll have to call out to the terminal.
local osCmd = "printf "..codeVerifier.." | openssl dgst -binary -sha256 | openssl base64 | tr '+/' '-_' | tr -d '=' | tr -d '\n' ' '"
local codeChallenge = UTIL:trim(UTIL:shellExecute(osCmd), " ")
--If the shell command fails, we're probably testing/debugging locally on Windows so use a pure Lua SHA module.
--This likely never runs on Vera.
if (codeChallenge == "") then
local sha = require "sha2"
debug("Using Lua to generate challenge.")
codeChallenge = UTIL:base64urlencode(UTIL:base64encode(sha.hex2bin(sha.sha256(codeVerifier))))
end
local challengeUrl = string.format(self.API_CONFIG.AUTHORIZE_URL, self.API_CONFIG.CLIENTID, self.API_CONFIG.REDIRECT_URL, codeChallenge)
local response, status, header, contentStr
debug(logPrefix.."Code verifier: "..codeVerifier)
debug(logPrefix.."Beginning login with PKCE challenge: "..codeChallenge)
--We'll need to carry along all of the cookies we get as we trickle through the OAuth flow.
local accumulatedCookies = ""
--Follow redirects to get login URL.
while (status == nil or status == 302) do
response, status, header, contentStr = UTIL:request(challengeUrl, "GET", string.len(accumulatedCookies) > 0 and {["Cookie"] = accumulatedCookies} or nil, nil)
accumulatedCookies = UTIL:parseCookies(header["set-cookie"] or "")
if (status == 302) then
local redirectTo = header["location"]
if (string.find(redirectTo, "?error") ~= nil) then
local msg = "Bad login url."
log(logPrefix..msg, LOGLEVELS.ERROR)
return false, msg
end
challengeUrl = UTIL:startsWith(redirectTo, "/") and BASE_URL..redirectTo or redirectTo
end
end
if (response ~= 1 or status ~= 200) then
return false, "Could not find login page."
end
debug(logPrefix.."Login page found, submitting credentials.")
--Hack up the HTML and find the form action, verification token and return url.
local action = BASE_URL..UTIL:cutString(contentStr,"<form action=\"","\"")
local loginParams = {
["ReturnUrl"] = UTIL:cutString(action, "returnUrl=", nil),
["Email"] = userName,
["Password"] = password,
["__RequestVerificationToken"] = UTIL:cutString(contentStr, "__RequestVerificationToken\" type=\"hidden\" value=\"", "\"")
}
local loginBody = UTIL:buildKeyValueString(loginParams)
local HEADERS = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Cookie"] = accumulatedCookies,
["Content-Length"] = #loginBody
}
response, status, header, contentStr = UTIL:request(challengeUrl, "POST", HEADERS, loginBody)
accumulatedCookies = UTIL:parseCookies(header["set-cookie"])
if (response ~= 1 or status ~= 302) then
log(logPrefix.."Login failed. Response status is: "..status, LOGLEVELS.ERROR)
return false, "Login failed"
end
debug(logPrefix.."Login successful, following callback url.")
local loginRedirect = BASE_URL..header["location"]
HEADERS = {
["Cookie"] = accumulatedCookies
}
--Follow the redirect to the callback url.
response, status, header, contentStr = UTIL:request(loginRedirect, "GET", HEADERS, nil)
if (response ~= 1 or status ~= 302) then
log(logPrefix.."Failure navigating to callback url.", LOGLEVELS.ERROR)
return false, "Could not execute login callback."
end
loginRedirect = header["location"]
local code = UTIL:cutString(loginRedirect,"code=","&")
local scope = UTIL:urldecode(UTIL:cutString(loginRedirect,"scope=","&"))
debug(logPrefix.."Got login code, beginning token retrieval. Last step....whew!")
local qry = {
["client_id"] = self.API_CONFIG.CLIENTID,
["client_secret"] = UTIL:base64encode(self.API_CONFIG.CLIENT_SECRET),
["code"] = code,
["code_verifier"] = codeVerifier,
["grant_type"] = "authorization_code",
["redirect_uri"] = UTIL:split(loginRedirect,"?")[1],
["scope"] = scope
}
local tokenPayload = UTIL:buildKeyValueString(qry)
HEADERS = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Content-Length"] = #tokenPayload
}
response, status, header, contentStr = UTIL:request(self.API_CONFIG.TOKEN_URL, "POST", HEADERS, tokenPayload)
if (response ~= 1 or status ~= 200) then
local msg = "Failed to retrieve access token."
log(logPrefix, msg, LOGLEVELS.ERROR)
return false, msg
end
local token = json.decode(contentStr).access_token
debug(logPrefix.."Successfully cut access token.")
HEADERS = {
["Authorization"] = "Bearer "..token
}
response, status, header, contentStr = UTIL:request(self.API_CONFIG.ACCOUNT_URL, "GET", HEADERS)
if (response ~= 1 or status ~= 200) then
local msg = "Failed getting account info"
log(logPrefix..msg, LOGLEVELS.ERROR)
return false, msg
end
--Let's not make it difficult just yet. Assume only 1 account.
local accountId = json.decode(contentStr).accounts[1].id
debug(logPrefix.."Found account id "..accountId)
return true, token, accountId
end,
changeDeviceStatus = function(self, token, accountId, serialNumber, deviceId, gatewayDeviceId, deviceFamily, action)
local logPrefix = "("..self.API_CONFIG.NAME.."::changeDeviceStatus): "
debug(logPrefix.."Requesting action "..(action or "NIL").." on device "..(serialNumber or "NIL").." of type "..deviceFamily)
local resultText -- Summary of the result
local response, status, header, contentStr
if (action ~= "0" and action ~= "1") then
log(logPrefix.."Unknown device state requested: "..(action or "NIL").." for device "..(serialNumber or "NIL").."... Aborting state change.", LOGLEVELS.WARN)
return false, "Unknown device state request"
end
local HEADERS = {
["Authorization"] = "Bearer "..token,
["Content-Length"] = 0
}
local actionText, messageText, url
if (deviceFamily == "lamp") then
actionText = action == "1" and "on" or "off"
messageText = "Turning "..actionText
url = self.API_CONFIG.LAMP_ACTION_URL
elseif (deviceFamily == "garagedoor") then
actionText = action == "1" and "open" or "close"
messageText = action == "1" and "Opening" or "Closing"
url = self.API_CONFIG.GDO_ACTION_URL
else
return false, "Unknown device family."
end
url = string.format(url, accountId, serialNumber, actionText)
log(logPrefix..url, LOGLEVELS.ERROR)
response, status, header, contentStr = UTIL:request(url, "PUT", HEADERS)
local result = response == 1
if (result == true) then
result = status == 202
if (result == true) then
resultText = "Successfully changed status to " .. action
local refreshAfterChangeInterval = (luup.variable_get(MYQGATEWAY_SID, PLUGIN_VARS.REFRESH_AFTER_CHANGE_INTERVAL, gatewayDeviceId) or PLUGIN_CONFIG.REFRESH_AFTER_CHANGE)
debug(logPrefix.."State change command issued. Will refresh status in "..refreshAfterChangeInterval.." seconds.")
luup.call_delay('refreshDevices', refreshAfterChangeInterval, "")
luup.device_message(deviceId, 4, messageText, PLUGIN_CONFIG.REFRESH_AFTER_CHANGE_INTERVAL, PLUGIN_CONFIG.NAME)
else
resultText = "State change error. Response status from myQ is: "..status
log(logPrefix..resultText,LOGLEVELS.ERROR)
luup.device_message(deviceId, 2, "Failed "..messageText, PLUGIN_CONFIG.REFRESH_AFTER_CHANGE_INTERVAL, PLUGIN_CONFIG.NAME)
end
else
resultText = "Unsuccessful at communicating with the myQ service"
log(logPrefix..resultText,LOGLEVELS.ERROR)
end
return result, resultText
end,
inspectDevices = function(self, securityToken, accountId, gatewayDeviceId)
local logPrefix = "("..self.API_CONFIG.NAME.."::inspectDevices): "
-- Statuses for doors
local doorStatuses = {
["closed"] = "0",
["open"] = "1",
["stopped"] = "3",
["opening"] = "4",
["closing"] = "5",
["faulted"] = "7", -- faulted
["autoreverse"] = "7", -- faulted
["unknown"] = "10" -- offline
}
local connectionResult -- True if successful, false if not
local connectionText -- Holds issue with connection
local openerInfo = {} -- Table to hold info about openers
local ParentName = nil
local ParentId = nil
local numGDO = 0
local numLights= 0
local response, status, header, contentStr
local HEADERS = {
["Authorization"] = "Bearer "..securityToken
}
response, status, header, contentStr = UTIL:request(string.format(self.API_CONFIG.DEVICES_URL, accountId), "GET", HEADERS)
-- Check out our response
if (response == 1 and status == 200) then
--json parser is dumb and can't handle empty arrays.
local responseStr = contentStr:gsub('%[%]',"null")
local deviceContent = json.decode(responseStr)
debug(logPrefix.."Parsing devices.")
connectionResult = true
local numOpeners = 0
-- Time to loop over our device collection
for _, dx in ipairs(deviceContent.items) do
local d = dx
local deviceId = d.serial_number
-- This is only going to return a single "Place" / gateway. fix at some point to handle multiple gateways.
if (d.device_family == "gateway") then
ParentName = d.name
ParentId = d.serial_number
elseif (d.device_family == "garagedoor") or (d.device_family == "lamp") then
numOpeners = numOpeners + 1
local deviceStateValue = d.state.door_state or d.state.light_state
local openerName = d.name
local deviceState = doorStatuses[deviceStateValue]
-- Keep track of all the openers along with their state
table.insert(openerInfo, numOpeners, {
DeviceId = deviceId,
DeviceName = d.name,
DeviceState = deviceState,
DeviceStateTransitioning = (deviceState == doorStatuses["opening"] or deviceState == doorStatuses["closing"]),
OpenerName = openerName,
DeviceFamily = d.device_family
})
numGDO = numGDO + (d.device_family == "garagedoor" and 1 or 0)
numLights = numLights + (d.device_family == "lamp" and 1 or 0)
debug(logPrefix.."Discovered "..d.device_family.." ["..d.name.."] in state "..deviceStateValue)
else
-- don't recognize this device.
log(logPrefix.."Unknown Device Type Encountered. Device Family: " .. (d.device_family or "NIL"),LOGLEVELS.WARN)
end
end
else
connectionResult = false
connectionText = "Unsuccessful at connecting with device URL!"
log(logPrefix.."Unsuccessful at connecting with device URL!",LOGLEVELS.ERROR)
end
if connectionResult == true then
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.NUM_DOORS, tostring(numGDO), gatewayDeviceId)
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.NUM_LIGHTS, tostring(numLights), gatewayDeviceId)
debug("("..self.API_CONFIG.NAME.."::inspectDevices): Retrieved device data")
else
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.NUM_DOORS, "ERROR: Could not initialize connection.", gatewayDeviceId)
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.NUM_LIGHTS, " Check username and password.", gatewayDeviceId)
log("("..self.API_CONFIG.NAME.."::inspectDevices): FAILED to Retreived device data",LOGLEVELS.ERROR)
end
return connectionResult, openerInfo, ParentId, ParentName
end
} --end myQ API
end
local function refreshToken(logPrefix)
if (os.difftime(os.time(),PLUGIN_CONFIG.LAST_TOKEN_REFRESH) > PLUGIN_CONFIG.MAX_TOKEN_AGE) then
local result, tokenOrMsg, accountId = MYQ_API:login(PLUGIN_CONFIG.USERNAME, PLUGIN_CONFIG.PASSWORD)
if (result == false) then
local msg1 = "myQ Authorization Failed."
local msg2 = "Check settings in myQ setup."
luup.variable_set (MYQGATEWAY_SID,PLUGIN_VARS.NUM_DOORS,msg1,MyQGatewayID)
luup.variable_set (MYQGATEWAY_SID,PLUGIN_VARS.NUM_LIGHTS,msg2,MyQGatewayID)
task(msg1.." "..msg2, TASK_ERROR_PERM)
log(logPrefix..msg1..": "..(tokenOrMsg or "No Text"), LOGLEVELS.ERROR)
return false
end
PLUGIN_CONFIG.SECURITY_TOKEN = tokenOrMsg
PLUGIN_CONFIG.ACCOUNTID = accountId
PLUGIN_CONFIG.LAST_TOKEN_REFRESH = os.time()
debug(logPrefix.."TOKEN RETRIEVAL SUCCESSFUL!!")
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.TOKEN_TIMESTAMP, UTIL:getFriendlyTimestamp(PLUGIN_CONFIG.LAST_TOKEN_REFRESH), MyQGatewayID)
clearTask()
else
debug(logPrefix.."Token assumed valid: "..(os.difftime(os.time(), PLUGIN_CONFIG.LAST_TOKEN_REFRESH) / 60).." minutes old.")
end
debug(logPrefix.."Bearer "..PLUGIN_CONFIG.SECURITY_TOKEN)
end
function refreshDevices()
local logPrefix = "("..PLUGIN_CONFIG.NAME.."::refreshDevices): "
debug(logPrefix.."Starting status poll process.")
local tokenResult = refreshToken(logPrefix)
if (tokenResult == false) then
return
end
local devicesStillTransitioning = false
local connectionResult, openerInfo, _, _ = MYQ_API:inspectDevices(PLUGIN_CONFIG.SECURITY_TOKEN, PLUGIN_CONFIG.ACCOUNTID)
if (connectionResult == false) then
log(logPrefix.."Error on refresh doors, Exiting.",LOGLEVELS.ERROR)
return
end
for i = 1, #openerInfo do
local deviceId = openerInfo[i].DeviceId
local childId = child_id_lookup_table[deviceId]
debug(logPrefix.."Processing myQ device ["..(deviceId or "NIL").."] vera id ["..(childId or "NIL").."].")
if openerInfo[i].DeviceFamily == "garagedoor" then
luup.set_failure(0,childId)
local deviceStatusValue = openerInfo[i].DeviceState
local lastStatusText = luup.variable_get(GDO_SID, PLUGIN_VARS.STATUS, childId)
if (deviceStatusValue ~= lastStatusText) then
luup.variable_set("urn:micasaverde-com:SecuritySensor1", PLUGIN_VARS.LAST_TRIP, os.time(), childId)
end
if (not openerInfo[i].DeviceStateTransitioning) then
--remove any messages on the device.
luup.device_message(deviceId, 22, "Completed", 0, PLUGIN_CONFIG.NAME)
end
devicesStillTransitioning = devicesStillTransitioning or openerInfo[i].DeviceStateTransitioning
luup.variable_set(GDO_SID, PLUGIN_VARS.STATUS, deviceStatusValue, childId)
else
log(logPrefix..openerInfo[i].DeviceFamily.." devices not currently supported.",LOGLEVELS.WARN)
end
end
--Use the LastCheck variable to store the last time the doors were checked
--The timestamp is set for when the last re-auth occurred
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.LAST_CHECK, UTIL:getFriendlyTimestamp(os.time()), MyQGatewayID)
if (devicesStillTransitioning) then
-- one or more devices are transitioning - queue a recheck
luup.call_delay('refreshDevices', 5, "")
end
debug(logPrefix.."Poll process completed.")
end
function init(lul_device)
if (MyQGatewayID == nil) then MyQGatewayID = lul_device end -- save the global device ID
local isDisabled = luup.attr_get("disabled", lul_device)
local logPrefix = "("..PLUGIN_CONFIG.NAME.."::init): "
if ((isDisabled == 1) or (isDisabled == "1")) then
log(logPrefix.."MyQ Gateway Plugin "..VERSION.." - ************** DISABLED **************", LOGLEVELS.WARN)
PLUGIN_CONFIG.PLUGIN_DISABLED = true
-- mark device as disabled
return true, "Plugin Disabled.", "MyQGateway"
end
debug(logPrefix.."MyQ Gateway Plugin "..VERSION.." DeviceId " ..lul_device.." - ************** STARTING **************")
-- Not sure why, but the task (status) seems to only work as expected when the function is called via timer.
-- This allows a status message to appear for the device to allow the user to change settings, while at the same time
-- does not "fail" the initiation. this allows the user to make use of the UI to set the initial parameters
-- instead of going to advanced tab and manually enter values which could be problematic for some.
luup.variable_set (MYQGATEWAY_SID,"PLUGIN_VERSION",VERSION,MyQGatewayID)
UTIL:initVar(MYQGATEWAY_SID, PLUGIN_VARS.USERNAME, "", MyQGatewayID)
UTIL:initVar(MYQGATEWAY_SID, PLUGIN_VARS.PASSWORD, "", MyQGatewayID)
UTIL:initVar(MYQGATEWAY_SID, PLUGIN_VARS.REFRESH_INTERVAL, PLUGIN_CONFIG.REFRESH_INTERVAL, MyQGatewayID)
UTIL:initVar(MYQGATEWAY_SID, PLUGIN_VARS.REFRESH_AFTER_CHANGE_INTERVAL, PLUGIN_CONFIG.REFRESH_AFTER_CHANGE, MyQGatewayID)
UTIL:initVar(MYQGATEWAY_SID, PLUGIN_VARS.DEBUG_MODE, (PLUGIN_CONFIG.DEBUG_MODE and "1" or "0"), MyQGatewayID)
UTIL:initVar(MYQGATEWAY_SID, PLUGIN_VARS.DEBUG_MODE_TEXT, "DISABLED", MyQGatewayID)
luup.set_failure(0)
if (DEVICE_ICONS) then
DEVICE_ICONS:createIcons()
DEVICE_ICONS = nil
end
-- load the PLUGIN_CONFIG variables before the deferred startup code runs
PLUGIN_CONFIG.DEBUG_MODE = (tonumber(luup.variable_get(MYQGATEWAY_SID, "DEBUG_MODE", MyQGatewayID),10) == 1) and true or (PLUGIN_CONFIG.FORCE_DEBUG or false)
debug(logPrefix.."MyQ Gateway Plugin Options - DEBUG_MODE ["..(PLUGIN_CONFIG.DEBUG_MODE and "ENABLED" or "DISABLED").."]")
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.DEBUG_MODE_TEXT, (PLUGIN_CONFIG.DEBUG_MODE and "ENABLED" or "DISABLED"), lul_device)
luup.call_timer("startupDeferred", 1, "1", "")
return true, "Plugin starting.", "MyQGateway"
end
function startupDeferred()
local foundIssue = false --Any problem with username/password?
local issueMessage = "ERROR: " --Our fancy error message
local logPrefix = "("..PLUGIN_CONFIG.NAME.."::startupDeferred): "
-- Grab our username and password from advanced
-- Throw an error if any or both are not set below
PLUGIN_CONFIG.USERNAME = luup.variable_get(MYQGATEWAY_SID, PLUGIN_VARS.USERNAME, MyQGatewayID) or PLUGIN_CONFIG.USERNAME
if (PLUGIN_CONFIG.USERNAME == nil or PLUGIN_CONFIG.USERNAME == "") then
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.USERNAME, "", MyQGatewayID)
foundIssue = true
issueMessage = "Username not configured. "
debug(logPrefix.."MyQ Username not specified.")
end
PLUGIN_CONFIG.PASSWORD = luup.variable_get(MYQGATEWAY_SID, PLUGIN_VARS.PASSWORD, MyQGatewayID) or PLUGIN_CONFIG.PASSWORD
if (PLUGIN_CONFIG.PASSWORD == nil or PLUGIN_CONFIG.PASSWORD == "") then
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.PASSWORD, "", MyQGatewayID)
foundIssue = true
issueMessage = issueMessage .. "Password not configured."
debug(logPrefix.."MyQ Password not specified.")
end
local refreshInterval = luup.variable_get(MYQGATEWAY_SID, PLUGIN_VARS.REFRESH_INTERVAL, MyQGatewayID)
if (refreshInterval ~= nil and tonumber(refreshInterval) > 0) then
debug(logPrefix.."Status refresh on your opener(s) will be called every:" .. refreshInterval)
else
debug(logPrefix.."Setting refresh interval to 120 sec by default")
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.REFRESH_INTERVAL, 120, MyQGatewayID)
end
local refreshAfterChangeInterval = luup.variable_get(MYQGATEWAY_SID, PLUGIN_VARS.REFRESH_AFTER_CHANGE_INTERVAL, MyQGatewayID)
if (refreshAfterChangeInterval == nil) then
luup.variable_set(MYQGATEWAY_SID, PLUGIN_VARS.REFRESH_AFTER_CHANGE_INTERVAL, PLUGIN_CONFIG.REFRESH_AFTER_CHANGE, MyQGatewayID)
end
if (foundIssue == true) then
local msg = issueMessage .. " Check settings in myQ setup."
task(msg, TASK_ERROR_PERM)
luup.variable_set (MYQGATEWAY_SID,PLUGIN_VARS.NUM_DOORS,issueMessage,MyQGatewayID)
luup.variable_set (MYQGATEWAY_SID,PLUGIN_VARS.NUM_LIGHTS,"",MyQGatewayID)
debug(logPrefix.."MyQ Gateway Plugin "..VERSION.." - ********** STARTUP COMPLETE **********")
return
end
debug(logPrefix.."ATTEMPTING INITIAL API LOGIN")
local tokenResult = refreshToken(logPrefix)
if (tokenResult == false) then
return
end
local connectionResult, openerInfo, _, _ = MYQ_API:inspectDevices(PLUGIN_CONFIG.SECURITY_TOKEN, PLUGIN_CONFIG.ACCOUNTID, MyQGatewayID)
if (connectionResult == false) then
log(logPrefix.."No data retrieved.",LOGLEVELS.ERROR)
log(logPrefix.."Unable to continue. Exiting.",LOGLEVELS.ERROR)
debug(logPrefix.."MyQ Gateway Plugin "..VERSION.." - ********** STARTUP COMPLETE **********")
return
end