-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelloworld.groovy
100 lines (89 loc) · 3.29 KB
/
helloworld.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
definition(
name: "Hello World",
namespace: "mvevitsis",
author: "Matvei Vevitsis",
description: "Speak a message on a connected speaker when a switch is turned on",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
)
preferences {
page(name: "mainPage", title: "Speak a message on a connected speaker when a switch is turned on", install: true, uninstall: true)
page(name: "timeNotificationInput", title: "Only send notifications during a certain time:") {
section {
input "notifyStarting", "time", title: "Starting", required: false
input "notifyEnding", "time", title: "Ending", required: false
}
}
}
def mainPage() {
dynamicPage(name: "mainPage") {
section("Input"){
input "mySwitch", "capability.switch", title: "Select a switch", required: false, multiple: true
input "myContact", "capability.contactSensor", title: "Select a contact sensor", required: false, multiple: true
}
section("Output"){
input "audioDevices", "capability.audioNotification", title: "Select a speaker", required: false, multiple: true
input "sendPushMessage", "enum", title: "Send a push notification", options: ["Yes", "No"], defaultValue: "No", required: true
href "timeNotificationInput", title: "Only during a certain time", description: getTimeLabel(notifyStarting, notifyEnding) ?: "Tap to set", state: getTimeLabel(notifyStarting, notifyEnding) ? "complete" : "incomplete"
input "messageText", "text", title: "Message Text", defaultValue: "Hello World", required: false
}
}
}
private hhmm(time, fmt = "h:mm a") {
def t = timeToday(time, location.timeZone)
def f = new java.text.SimpleDateFormat(fmt)
f.setTimeZone(location.timeZone ?: timeZone(time))
return f.format(t)
}
private getTimeLabel(starting, ending) {
return (starting && ending) ? hhmm(starting) + " - " + hhmm(ending, "h:mm a z") : ""
}
def installed(){
subscribeToEvents()
}
def updated(){
unsubscribe()
subscribeToEvents()
}
def subscribeToEvents(){
subscribe(mySwitch, "switch.on", eventHandler)
subscribe(myContact, "contact.open", eventHandler)
}
def eventHandler(evt){
if (isTimeOK(notifyStarting, notifyEnding)) {
sendMessage(evt)
} else {
log.debug "Not sending notifications outside configured time period."
}
}
private sendMessage(evt){
def msg = messageText
if(sendPushMessage == "Yes") {
sendPush(msg)
}
if(audioDevices){
audioDevices?.each { audioDevice ->
if (audioDevice.hasCommand("playText")) { //Check if speaker supports TTS
audioDevice.playText(msg)
} else {
if (audioDevice.hasCommand("speak")) { //Check if speaker supports speech synthesis
audioDevice.speak(msg.toString())
} else {
audioDevice.playTrack(textToSpeech(msg)?.uri)
}
}
}
}
}
private isTimeOK(starting, ending) {
def result = true
if (starting && ending) {
def currTime = now()
def start = timeToday(starting, location?.timeZone).time
def stop = timeToday(ending, location?.timeZone).time
result = start < stop ? currTime >= start && currTime <= stop : currTime <= stop || currTime >= start
}
log.trace "Time period OK to send = $result"
return result
}