-
Notifications
You must be signed in to change notification settings - Fork 14
/
alexattsparentv0.6.groovy
665 lines (609 loc) · 34.9 KB
/
alexattsparentv0.6.groovy
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
/*
* Alexa TTS Manager
*
* https://raw.githubusercontent.com/ogiewon/Hubitat/master/AlexaTTS/Apps/alexa-tts-manager.src/alexa-tts-manager.groovy
*
*
* Copyright 2018 Daniel Ogorchock - Special thanks to Chuck Schwer for his tips and prodding me
* to not let this idea fall through the cracks!
* Copyright 2018 Gabriele - Automatic cookie refresh with Apollon77/Alexa-Cookie
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
* Change History:
*
* Version Date Who What
* ------- ---- --- ----
* v0.1.0 2018-10-20 Dan Ogorchock Original Creation - with help from Chuck Schwer!
* v0.1.1 2018-10-21 Dan Ogorchock Trapped an error when invalid data returned from Amazon due to cookie issue
* v0.2.0 2018-10-21 Stephan Hackett Modified to include more Alexa Devices for selection
* v0.3.0 2018-10-22 Dan Ogorchock Added support for Canada and United Kingdom, and ability to rename the app
* v0.4.0 2018-11-18 Stephan Hackett Added support for Virtual Container
* v0.4.1 2018-11-18 Dan Ogorchock Optimized multi-country support code and added Notification support for errors
* v0.4.2 2018-11-27 Dan Ogorchock Improved error handling for notifications when cookie expires (via live logging and optoinally, via push notification)
* v0.4.3 2018-12-07 Dan Ogorchock Prevent sending empty string TTS messages to Amazon.
* v0.4.4 2018-12-10 Dan Ogorchock Detect and notify via logging and notification, message rate exceeded errors to avoid confusion with cookie expiration errors.
* v0.4.5 2018-12-14 Stephan Hackett Added ability to paste in the entire Raw Cookie. No manual editing required. Improved setup page flow.
* v0.4.6 2018-12-23 Dan Ogorchock Added support for Italy. Thank you @gabriele!
* v0.5.0 2019-01-02 Gabriele Added support for automatic cookie refresh with external NodeJS webserver
* v0.5.1 2019-02-12 Dan Ogorchock Corrected contentType to prevent errors in response parsing
* v0.5.2 2019-04-04 Thomas Howard Added get/set Volume Control (not working currently - Dan O 4/6/19)
* v0.5.3 2019-04-16 Gabriele Added app events to have some historic logging
* v0.5.4 2019-06-24 Dan Ogorchock Attempt to add Australia
* v0.5.5 2019-07-18 Dan Ogorchock Reduced Debug Logging
* v0.5.6 2020-01-02 Dan Ogorchock Add support for All Echo Device Broadcast
* v0.5.7 2020-01-02 Bob Butler Add an override switch that disables all voice messages when off
* v0.5.8 2020-01-07 Marco Felicio Added support for Brazil
* v0.5.9 2020-01-26 Dan Ogorchock Changed automatic cookie refresh time to 1am to avoid hub maintenance window
* v0.6.0 2020-10-25 lg kahn add mesg if cookie updated sucessfully, also add setvolume command called from each indiv. device.
* v0.6.1 2020-11-04 lg kahn support for ecobee via greg.. thanks
* v0.6.2 2020-10-10 lg kahn custom version with auto exponential backoff retry on 429 too many request failure.
*/
definition(
name: "Alexa TTS Manager",
namespace: "ogiewon",
author: "Dan Ogorchock",
description: "Manages your Alexa TTS Child Devices",
iconUrl: "",
iconX2Url: "")
preferences {
page(name: "pageOne")
page(name: "pageTwo")
}
def pageOne(){
dynamicPage(name: "pageOne", title: "Alexa Cookie and Country selections", nextPage: "pageTwo", uninstall: true) {
section("Please Enter your alexa.amazon.com 'cookie' file string here (end with a semicolon)") {
input("alexaCookie", "text", title: "Raw or edited Cookie", submitOnChange: true, required: true)
}
if(alexaCookie != null && alexaCookie.contains("Cookie: ")){
def finalForm
def preForm = alexaCookie.split("Cookie: ")
if(preForm.size() > 1) finalForm = preForm[1]?.replace("\"", "") + ";"
app.updateSetting("alexaCookie",[type:"text", value: finalForm])
}
section("Please enter settings for automatic cookie refresh with NodeJS") {
input("alexaRefreshURL", "text", title: "NodeJS service URL", required: false)
input("alexaRefreshUsername", "text", title: "NodeJS service Username (not Amazon one)", required: false)
input("alexaRefreshPassword", "password", title: "NodeJS service Password (not Amazon one)", required: false)
input("alexaRefreshOptions", "text", title: "Alexa cookie refresh options", required: false, submitOnChange: true)
input("alexaRefresh", "bool", title: "Force refresh now? (Procedure will require 5 minutes)", submitOnChange: true)
}
if(alexaRefreshOptions == null) {
unschedule()
}
else {
// Schedule automatic update
unschedule()
schedule("0 0 1 1/6 * ? *", refreshCookie) // Check for updates every 6 days at 1:00 AM
//Extract cookie from options if cookie is empty
if(alexaCookie == null){
app.updateSetting("alexaCookie",[type:"text", value: getCookieFromOptions(alexaRefreshOptions)])
}
}
if(alexaRefresh) {
refreshCookie()
app.updateSetting("alexaRefresh",[type:"bool", value: false])
}
section("Please choose your country") {
input "alexaCountry", "enum", multiple: false, required: true, options: getURLs().keySet().collect()
}
section("Notification Device") {
paragraph "Optionally assign a device for error notifications (like when the cookie is invalid or refresh fails)"
input "notificationDevice", "capability.notification", multiple: false, required: false
}
section("Override Switch") {
paragraph "Optionally assign a switch that will disable voice when turned off"
input "overrideSwitch", "capability.switch", multiple: false, required: false
}
section("App Name") {
label title: "Optionally assign a custom name for this app", required: false
}
}
}
def pageTwo(){
dynamicPage(name: "pageTwo", title: "Amazon Alexa Device Selection", install: true, uninstall: true) {
section("Please select devices to create Alexa TTS child devices for") {
input "alexaDevices", "enum", multiple: true, required: false, options: getDevices()
}
section("") {
paragraph "<span style='color:red'>Warning!!\nChanging the option below will delete any previously created child devices!!\n"+
"Virtual Container driver v1.1.20181118 or higher must be installed on your hub!!</span>"+
"<a href='https://github.com/stephack/Hubitat/blob/master/drivers/Virtual%20Container/Virtual%20Container.groovy' target='_blank'> [driver] </a>"+
"<a href='https://community.hubitat.com/t/release-virtual-container-driver/4440' target='_blank'> [notes] </a>"
input "alexaVC", "bool", title: "Add Alexa TTS child devices to a Virtual Container?"
}
}
}
/* lgk new setvolume fx */
def setVolume(Integer newVolume, String device)
{
log.debug "Setting volume level '${newVolume}' to '${device}'"
// find the device
atomicState.alexaJSON.devices.any {it->
if (it.accountName == device) {
//log.debug "${it.accountName}"
// log.debug "${it.deviceType}"
// log.debug "${it.serialNumber}"
// log.debug "${it.deviceOwnerCustomerId}"
try{
def SEQUENCECMD = "Alexa.DeviceControls.Volume"
def DEVICETYPE = "${it.deviceType}"
def DEVICESERIALNUMBER = "${it.serialNumber}"
def MEDIAOWNERCUSTOMERID = "${it.deviceOwnerCustomerId}"
def LANGUAGE = getURLs()."${alexaCountry}".Language
def TTS= ",\\\"value\\\":\\\"${newVolume}\\\"" //volume can be 0-100
//log.debug "TTS = $TTS"
def command = "{\"behaviorId\":\"PREVIEW\",\
\"sequenceJson\":\"{\\\"@type\\\":\\\"com.amazon.alexa.behaviors.model.Sequence\\\",\
\\\"startNode\\\":{\\\"@type\\\":\\\"com.amazon.alexa.behaviors.model.OpaquePayloadOperationNode\\\",\
\\\"type\\\":\\\"${SEQUENCECMD}\\\",\
\\\"operationPayload\\\":{\\\"deviceType\\\":\\\"${DEVICETYPE}\\\",\
\\\"deviceSerialNumber\\\":\\\"${DEVICESERIALNUMBER}\\\",\
\\\"locale\\\":\\\"${LANGUAGE}\\\",\
\\\"customerId\\\":\\\"${MEDIAOWNERCUSTOMERID}\\\"${TTS}}}}\",\
\"status\":\"ENABLED\"}"
// log.debug "Command = $command"
//
// log.debug "urls = $getURLs()"
// log.debug "country = $alexaCountry"
// log.debug "cookie = $alexaCookie"
def csrf = (alexaCookie =~ "csrf=(.*?);")[0][1]
//log.debug "csrf = $csrf"
def params = [uri: "https://" + getURLs()."${alexaCountry}".Alexa + "/api/behaviors/preview",
headers: ["Cookie":"""${alexaCookie}""",
"Referer": "https://" + getURLs()."${alexaCountry}".Amazon + "/spa/index.html",
"Origin": "https://" + getURLs()."${alexaCountry}".Amazon,
"csrf": "${csrf}",
"Connection": "keep-alive",
"DNT":"1"],
//requestContentType: "application/json",
contentType: "text/plain",
body: command
]
//log.debug "parms = ${params}"
httpPost(params) { resp ->
//log.debug resp.contentType
//log.debug resp.status
//log.debug resp.data
if (resp.status != 200) {
log.error "'setVolume()': httpPost() resp.status = ${resp.status}"
notifyIfEnabled("Alexa TTS: Please check your cookie!")
}
}
}
catch (groovyx.net.http.HttpResponseException hre) {
//Noticed an error in parsing the http response. For now, catch it to prevent errors from being logged
if (hre.getResponse().getStatus() != 200) {
log.error "'setVolume()': Error making Call (Data): ${hre.getResponse().getData()}"
log.error "'setVolume()': Error making Call (Status): ${hre.getResponse().getStatus()}"
log.error "'setVolume()': Error making Call (getMessage): ${hre.getMessage()}"
if (hre.getResponse().getStatus() == 400) {
notifyIfEnabled("Alexa TTS: ${hre.getResponse().getData()}")
}
else {
notifyIfEnabled("Alexa TTS: Please check your cookie!")
}
}
}
catch (e) {
log.error "'setVolume()': error = ${e}"
//log.error "'setVolume()': httpPost() resp.contentType = ${e.response.contentType}"
notifyIfEnabled("Alexa TTS: Please check your cookie!")
}
return true
}
}
}
def speakMessage(String message, String device, Integer currentDelay = 0) {
// log.debug "in send message = $message delay = $currentDelay"
if (overrideSwitch != null && overrideSwitch.currentSwitch == 'off') {
log.info "${overrideSwitch} is off, AlexaTTS will not speak message '${message}'"
return
}
log.debug "Sending '${message}' to '${device}'"
sendEvent(name:"speakMessage", value: message, descriptionText: "Sending message to '${device}'")
if (message == '' || message.length() == 0) {
log.warn "Message is empty. Skipping sending request to Amazon"
}
else {
atomicState.alexaJSON.devices.any {it->
if ((it.accountName == device) || (device == "All Echos")) {
//log.debug "${it.accountName}"
//log.debug "${it.deviceType}"
//log.debug "${it.serialNumber}"
//log.debug "${it.deviceOwnerCustomerId}"
try{
def SEQUENCECMD = "Alexa.Speak"
def DEVICETYPE = "${it.deviceType}"
def DEVICESERIALNUMBER = "${it.serialNumber}"
def MEDIAOWNERCUSTOMERID = "${it.deviceOwnerCustomerId}"
def LANGUAGE = getURLs()."${alexaCountry}".Language
def command = ""
if (device == "All Echos") {
//command = "{\"behaviorId\":\"PREVIEW\",\"sequenceJson\":\"{\\\"@type\\\":\\\"com.amazon.alexa.behaviors.model.Sequence\\\",\\\"startNode\\\":{\\\"@type\\\":\\\"com.amazon.alexa.behaviors.model.OpaquePayloadOperationNode\\\",\\\"operationPayload\\\":{\\\"customerId\\\":\\\"${MEDIAOWNERCUSTOMERID}\\\",\\\"expireAfter\\\":\\\"PT5S\\\",\\\"content\\\":[{\\\"locale\\\":\\\"${LANGUAGE}\\\",\\\"display\\\":{\\\"title\\\":\\\"AlexaTTS\\\",\\\"body\\\":\\\"${message}\\\"},\\\"speak\\\":{\\\"type\\\":\\\"text\\\",\\\"value\\\":\\\"${message}\\\"}}],\\\"target\\\":{\\\"customerId\\\":\\\"${MEDIAOWNERCUSTOMERID}\\\"}},\\\"type\\\":\\\"AlexaAnnouncement\\\"}}\",\"status\":\"ENABLED\"}"
command = "{\"behaviorId\":\"PREVIEW\",\
\"sequenceJson\":\"{\\\"@type\\\":\\\"com.amazon.alexa.behaviors.model.Sequence\\\",\
\\\"startNode\\\":{\\\"@type\\\":\\\"com.amazon.alexa.behaviors.model.OpaquePayloadOperationNode\\\",\
\\\"operationPayload\\\":{\\\"customerId\\\":\\\"${MEDIAOWNERCUSTOMERID}\\\",\
\\\"expireAfter\\\":\\\"PT5S\\\",\
\\\"content\\\":[{\\\"locale\\\":\\\"${LANGUAGE}\\\",\
\\\"display\\\":{\\\"title\\\":\\\"AlexaTTS\\\",\
\\\"body\\\":\\\"${message}\\\"},\
\\\"speak\\\":{\\\"type\\\":\\\"text\\\",\
\\\"value\\\":\\\"${message}\\\"}}],\
\\\"target\\\":{\\\"customerId\\\":\\\"${MEDIAOWNERCUSTOMERID}\\\"}},\
\\\"type\\\":\\\"AlexaAnnouncement\\\"}}\",\
\"status\":\"ENABLED\"}"
}
else {
//command = "{\"behaviorId\":\"PREVIEW\",\"sequenceJson\":\"{\\\"@type\\\":\\\"com.amazon.alexa.behaviors.model.Sequence\\\",\\\"startNode\\\":{\\\"@type\\\":\\\"com.amazon.alexa.behaviors.model.OpaquePayloadOperationNode\\\",\\\"type\\\":\\\"${SEQUENCECMD}\\\",\\\"operationPayload\\\":{\\\"deviceType\\\":\\\"${DEVICETYPE}\\\",\\\"deviceSerialNumber\\\":\\\"${DEVICESERIALNUMBER}\\\",\\\"locale\\\":\\\"${LANGUAGE}\\\",\\\"customerId\\\":\\\"${MEDIAOWNERCUSTOMERID}\\\"${TTS}}}}\",\"status\":\"ENABLED\"}"
command = "{\"behaviorId\":\"PREVIEW\",\
\"sequenceJson\":\"{\\\"@type\\\":\\\"com.amazon.alexa.behaviors.model.Sequence\\\",\
\\\"startNode\\\":{\\\"@type\\\":\\\"com.amazon.alexa.behaviors.model.OpaquePayloadOperationNode\\\",\
\\\"type\\\":\\\"${SEQUENCECMD}\\\",\
\\\"operationPayload\\\":{\\\"deviceType\\\":\\\"${DEVICETYPE}\\\",\
\\\"deviceSerialNumber\\\":\\\"${DEVICESERIALNUMBER}\\\",\
\\\"locale\\\":\\\"${LANGUAGE}\\\",\
\\\"customerId\\\":\\\"${MEDIAOWNERCUSTOMERID}\\\",\
\\\"textToSpeak\\\":\\\"${message}\\\"}}}\",\
\"status\":\"ENABLED\"}"
}
def csrf = (alexaCookie =~ "csrf=(.*?);")[0][1]
def params = [uri: "https://" + getURLs()."${alexaCountry}".Alexa + "/api/behaviors/preview",
headers: ["Cookie":"""${alexaCookie}""",
"Referer": "https://" + getURLs()."${alexaCountry}".Amazon + "/spa/index.html",
"Origin": "https://" + getURLs()."${alexaCountry}".Amazon,
"csrf": "${csrf}",
"Connection": "keep-alive",
"DNT":"1"],
//requestContentType: "application/json",
contentType: "text/plain",
body: command
]
//log.debug "Command = ${params}"
httpPost(params) { resp ->
//log.debug resp.contentType
//log.debug resp.status
//log.debug resp.data
if (resp.status != 200) {
log.error "'speakMessage()': httpPost() resp.status = ${resp.status}"
notifyIfEnabled("Alexa TTS: Please check your cookie!")
}
}
}
catch (groovyx.net.http.HttpResponseException hre) {
//Noticed an error in parsing the http response. For now, catch it to prevent errors from being logged
// lgk add retry after delay on error 429 rate limited
def statusCode = hre.getResponse().getStatus()
if (statusCode == 429)
{
def newDelay
if (currentDelay == 0)
newDelay = 5
else newDelay = currentDelay * 2
if (newDelay >= 20)
{
log.debug "Already tried repeated command 3 times ... Giving Up!"
}
else
{
log.debug "Got Alexa rate limiting error (429) delaying for $newDelay and Retrying!"
runIn(newDelay,"speakMessage",[data: [message, device,newDelay]])
}
}
else if (statusCode != 200) {
log.error "'speakMessage()': Error making Call (Data): ${hre.getResponse().getData()}"
log.error "'speakMessage()': Error making Call (Status): ${hre.getResponse().getStatus()}"
log.error "'speakMessage()': Error making Call (getMessage): ${hre.getMessage()}"
if (hre.getResponse().getStatus() == 400) {
notifyIfEnabled("Alexa TTS: ${hre.getResponse().getData()}")
}
else {
notifyIfEnabled("Alexa TTS: Please check your cookie!")
}
}
}
catch (e) {
log.error "'speakMessage()': error = ${e}"
//log.error "'speakMessage()': httpPost() resp.contentType = ${e.response.contentType}"
notifyIfEnabled("Alexa TTS: Please check your cookie!")
}
return true
}
}
}
}
def getDevices() {
if (alexaCookie == null) {log.debug "No cookie yet"
return}
try{
def csrf = (alexaCookie =~ "csrf=(.*?);")[0][1]
def params = [uri: "https://" + getURLs()."${alexaCountry}".Alexa + "/api/devices-v2/device?cached=false",
headers: ["Cookie":"""${alexaCookie}""",
"Referer": "https://" + getURLs()."${alexaCountry}".Amazon + "/spa/index.html",
"Origin": "https://" + getURLs()."${alexaCountry}".Amazon,
"csrf": "${csrf}",
"Connection": "keep-alive",
"DNT":"1"],
requestContentType: "application/json; charset=UTF-8"
]
httpGet(params) { resp ->
//log.debug resp.contentType
//log.debug resp.status
//log.debug resp.data
if ((resp.status == 200) && (resp.contentType == "application/json")) {
def validDevices = ["All Echos"]
atomicState.alexaJSON = resp.data
//log.debug state.alexaJSON.devices.accountName
atomicState.alexaJSON.devices.each {it->
if (it.deviceFamily in ["ECHO", "ROOK", "KNIGHT", "THIRD_PARTY_AVS_SONOS_BOOTLEG", "TABLET"]) {
//log.debug "${it.accountName} is valid"
validDevices << it.accountName
}
else if (it.deviceFamily == "THIRD_PARTY_AVS_MEDIA_DISPLAY" && it.capabilities.contains("AUDIBLE")) {
validDevices << it.accountName
}
else if (it.deviceFamily == "UNKNOWN" && it.capabilities.contains("AUDIO_PLAYER")) {
validDevices << it.accountName
}
}
log.debug "getDevices(): validDevices = ${validDevices}"
return validDevices
}
else {
log.error "Encountered an error. http resp.status = '${resp.status}'. http resp.contentType = '${resp.contentType}'. Should be '200' and 'application/json'. Check your cookie string!"
notifyIfEnabled("Alexa TTS: Please check your cookie!")
return "error"
}
}
}
catch (e) {
log.error "getDevices: error = ${e}"
notifyIfEnabled("Alexa TTS: Please check your cookie!")
}
}
private void createChildDevice(String deviceName) {
log.debug "'createChildDevice()': Creating Child Device '${deviceName}'"
try {
def child = addChildDevice("ogiewon", "Child Alexa TTS", "AlexaTTS${app.id}-${deviceName}", null, [name: "AlexaTTS-${deviceName}", label: "AlexaTTS ${deviceName}", completedSetup: true])
} catch (e) {
log.error "Child device creation failed with error = ${e}"
}
}
def installed() {
log.debug "'Installed()' called with settings: ${settings}"
updated()
}
def uninstalled() {
log.debug "'uninstalled()' called"
childDevices.each { deleteChildDevice(it.deviceNetworkId) }
}
def getURLs() {
def URLs = ["United States": [Alexa: "pitangui.amazon.com", Amazon: "alexa.amazon.com", Language: "en-US"],
"Canada": [Alexa: "alexa.amazon.ca", Amazon: "alexa.amazon.ca", Language: "en-US"],
"United Kingdom": [Alexa: "layla.amazon.co.uk", Amazon: "amazon.co.uk", Language: "en-GB"],
"Italy": [Alexa: "alexa.amazon.it", Amazon: "alexa.amazon.it", Language: "it-IT"],
"Australia": [Alexa: "alexa.amazon.com.au", Amazon: "alexa.amazon.com.au", Language: "en-AU"],
"Brazil": [Alexa: "alexa.amazon.com.br", Amazon: "alexa.amazon.com.br", Language: "pt-BR"]]
return URLs
}
def updated() {
log.debug "'updated()' called"
//log.debug "'Updated' with settings: ${settings}"
//log.debug "AlexaJSON = ${atomicState.alexaJSON}"
//log.debug "Alexa Devices = ${atomicState.alexaJSON.devices.accountName}"
def devicesToRemove
if(alexaVC) {
devicesToRemove = getChildDevices().findAll{it.typeName == "Child Alexa TTS"}
if(devicesToRemove) purgeNow(devicesToRemove)
settings.alexaDevices.each {alexaName->
createContainer(alexaName)
}
}
else {
devicesToRemove = getChildDevices().findAll{it.typeName == "Virtual Container"}
if(devicesToRemove) purgeNow(devicesToRemove)
try {
settings.alexaDevices.each {alexaName->
def childDevice = null
if(childDevices) {
childDevices.each {child->
if (child.deviceNetworkId == "AlexaTTS${app.id}-${alexaName}") {
childDevice = child
//log.debug "Child ${app.label}-${alexaName} already exists"
}
}
}
if (childDevice == null) {
createChildDevice(alexaName)
log.debug "Child ${app.label}-${alexaName} has been created"
}
}
}
catch (e) {
log.error "Error in updated() routine, error = ${e}"
}
}
}
def purgeNow(devices){
log.debug "Purging: ${devices}"
devices.each { deleteChildDevice(it.deviceNetworkId) }
}
def createContainer(alexaName){
def container = getChildDevices().find{it.typeName == "Virtual Container"}
if(!container){
log.info "Creating Alexa TTS Virtual Container"
try {
container = addChildDevice("stephack", "Virtual Container", "AlexaTTS${app.id}", null, [name: "AlexaTTS-Container", label: "AlexaTTS Container", completedSetup: true])
} catch (e) {
log.error "Container device creation failed with error = ${e}"
}
createVchild(container, alexaName)
}
else {createVchild(container, alexaName)}
}
def createVchild(container, alexaName){
def vChildren = container.childList()
if(vChildren.find{it.data.vcId == "${alexaName}"}){
log.info alexaName + " already exists...skipping"
}
else {
log.info "Creating TTS Device: " + alexaName
try{
container.appCreateDevice("AlexaTTS ${alexaName}", "Child Alexa TTS", "ogiewon", "${alexaName}")
}
catch (e) {
log.error "Child device creation failed with error = ${e}"
}
}
}
def initialize() {
log.debug "'initialize()' called"
}
private def getCookieFromOptions(options) {
try
{
def cookie = new groovy.json.JsonSlurper().parseText(options)
if (!cookie || cookie == "") {
log.error("'getCookieFromOptions()': wrong options format!")
notifyIfEnabled("Alexa TTS: Error parsing cookie, see logs for more information!")
return ""
}
cookie = cookie.localCookie.replace('"',"")
if(cookie.endsWith(",")) {
cookie = cookie.reverse().drop(1).reverse()
}
cookie += ";"
log.info("Alexa TTS: new cookie parsed succesfully")
return cookie
}
catch(e)
{
log.error("'getCookieFromOptions()': error = ${e}")
notifyIfEnabled("Alexa TTS: Error parsing cookie, see logs for more information!")
return ""
}
}
def refreshCookie() {
log.info("Alexa TTS: starting cookie refresh procedure")
try {
def authHeaders = ""
if(alexaRefreshUsername != "")
authHeaders = "Basic " + (alexaRefreshUsername + ":" + alexaRefreshPassword).bytes.encodeBase64().toString() + "}"
def params =[
uri: alexaRefreshURL,
headers: [
"Authorization":"${authHeaders}",
"Connection": "keep-alive",
"DNT":"1"
],
requestContentType: "application/json; charset=UTF-8",
body: alexaRefreshOptions
]
httpPost(params) { resp ->
if ((resp.status == 200)) {
//log.debug resp.contentType
//log.debug resp.status
//log.debug resp.data
def respGuid = resp.data.toString()
log.info("Alexa TTS: Request for new cookie sent succesfully, guid: " + respGuid)
runIn(60*5, getCookie, [data: [guid: respGuid]])
}
else {
log.error "Encountered an error. http resp.status = '${resp.status}'. http resp.contentType = '${resp.contentType}'. Should be '200' and 'application/json; charset=utf-8'"
notifyIfEnabled("Alexa TTS: Error sending request for cookie refresh, see logs for more information!")
return "error"
}
}
}
catch (groovyx.net.http.HttpResponseException hre) {
// Noticed an error in parsing the http response
if (hre.getResponse().getStatus() != 200) {
log.error "'refreshCookie()': Error making Call (Data): ${hre.getResponse().getData()}"
log.error "'refreshCookie()': Error making Call (Status): ${hre.getResponse().getStatus()}"
log.error "'refreshCookie()': Error making Call (getMessage): ${hre.getMessage()}"
if (hre.getResponse().getStatus() == 400) {
notifyIfEnabled("Alexa TTS: ${hre.getResponse().getData()}")
}
else {
notifyIfEnabled("Alexa TTS: Error sending request for cookie refresh, see logs for more information!")
}
}
}
catch (e) {
log.error "'refreshCookie()': error = ${e}"
notifyIfEnabled("Alexa TTS: Error sending request for cookie refresh, see logs for more information!")
}
}
def getCookie(data){
log.info("Alexa TTS: starting cookie download procedure")
if(!data.guid || data.guid == "") {
log.error "'getCookie()': error = guid not provided!"
notifyIfEnabled("Alexa TTS: Error downloading cookie, see logs for more information!")
return "error"
}
try {
def authHeaders = ""
if(alexaRefreshUsername != "")
authHeaders = "Basic " + (alexaRefreshUsername + ":" + alexaRefreshPassword).bytes.encodeBase64().toString() + "}"
def params =[
uri: alexaRefreshURL,
headers: [
"Authorization":"${authHeaders}",
"Connection": "keep-alive",
"DNT":"1"
],
requestContentType: "application/json; charset=UTF-8",
query: [guid: data.guid]
]
httpGet(params) { resp ->
//log.debug resp.contentType
//log.debug resp.status
//log.debug resp.data
if ((resp.status == 200) && (resp.contentType == "application/json")) {
//If saved directly as resp.data then double quotes are stripped
def newOptions = new groovy.json.JsonBuilder(resp.data).toString()
app.updateSetting("alexaRefreshOptions",[type:"text", value: newOptions])
log.info("Alexa TTS: cookie downloaded succesfully")
app.updateSetting("alexaCookie",[type:"text", value: getCookieFromOptions(newOptions)])
sendEvent(name:"GetCookie", descriptionText: "New cookie downloaded succesfully")
notifyIfEnabled("Alexa TTS Cookie succesfully refreshed")
}
else {
log.error "Encountered an error. http resp.status = '${resp.status}'. http resp.contentType = '${resp.contentType}'. Should be '200' and 'application/json; charset=utf-8'"
notifyIfEnabled("Alexa TTS: Error downloading cookie, see logs for more information!")
return "error"
}
}
}
catch (groovyx.net.http.HttpResponseException hre) {
// Noticed an error in parsing the http response
if (hre.getResponse().getStatus() != 200) {
log.error "'getCookie()': Error making Call (Data): ${hre.getResponse().getData()}"
log.error "'getCookie()': Error making Call (Status): ${hre.getResponse().getStatus()}"
log.error "'getCookie()': Error making Call (getMessage): ${hre.getMessage()}"
if (hre.getResponse().getStatus() == 400) {
notifyIfEnabled("Alexa TTS: ${hre.getResponse().getData()}")
}
else {
notifyIfEnabled("Alexa TTS: Error downloading cookie, see logs for more information!")
}
}
}
catch (e) {
log.error "'getCookie()': error = ${e}"
notifyIfEnabled("Alexa TTS: Error dowloading cookie, see logs for more information!")
}
}
def notifyIfEnabled(message) {
if (notificationDevice) {
notificationDevice.deviceNotification(message)
}
}