/
virtual-sharptools-image-tile.groovy
196 lines (166 loc) · 7.67 KB
/
virtual-sharptools-image-tile.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
/**
* Copyright 2021 Bloodtick
*
* 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.
*
* Virtual SharpTools Image Tile
*
* Update: Bloodtick Jones
* Date: 2021-03-08
*
* 1.0.00 2021-03-09 First release to support Hubitat.
* 1.1.00 2021-04-12 Added random string option
**/
import groovy.json.*
private getVersionNum() { return "1.1.00" }
private getVersionLabel() { return "Virtual SharpTools Image Tile, version ${getVersionNum()}" }
metadata {
definition (name: "Virtual SharpTools Image Tile", namespace: "bloodtick", author: "Hubitat") {
capability "Actuator"
capability "Switch"
capability "PushableButton"
capability "DoubleTapableButton"
capability "Momentary"
capability "Sensor"
capability "Relay Switch"
capability "Music Player"
attribute "metadata1", "string" // no defined use this time
attribute "metadata2", "string" // no defined use this time
command "push"
command "doubleTap"
command "toggle"
command "updateImage", ["string"]
command "updateDescription", ["string"]
}
}
preferences {
input(name:"deviceMomentary", type: "bool", title: "Enable Switch to act like Momentary button", defaultValue: false)
input(name:"deviceTappedToggle", type: "bool", title: "Toggle Switch when Tile is single tapped", defaultValue: true)
input(name:"deviceDoubleTappedToggle", type: "bool", title: "Toggle Switch when Tile is double tapped", defaultValue: true)
input(name:"deviceOnEnable", type: "bool", title: "Enable image and text updates when Switch is on", defaultValue: true)
input(name:"deviceImageOn", type:"string", title:"Image when Switch is On", defaultValue:"", required:false)
input(name:"deviceTextOn", type:"string", title:"Description when Switch is On", defaultValue:"", required:false)
input(name:"deviceOffEnable", type: "bool", title: "Enable image and text updates when Switch is off", defaultValue: true)
input(name:"deviceImageOff", type:"string", title:"Image when Switch is Off", defaultValue:"", required:false)
input(name:"deviceTextOff", type:"string", title:"Description when Switch is Off", defaultValue:"", required:false)
if (!deviceTappedToggle) {
input(name:"deviceTappedEnable", type: "bool", title: "Enable image and text updates when Tile is single tapped", defaultValue: false)
input(name:"deviceImageTapped", type:"string", title:"Image when Tile is single tapped", defaultValue:"", required:false)
input(name:"deviceTextTapped", type:"string", title:"Description when Tile is single tapped", defaultValue:"", required:false)
}
if (!deviceDoubleTappedToggle) {
input(name:"deviceDoubleTappedEnable", type: "bool", title: "Enable image and text updates when Tile is double tapped", defaultValue: false)
input(name:"deviceImageDoubleTapped", type:"string", title:"Image when Tile is double tapped", defaultValue:"", required:false)
input(name:"deviceTextDoubleTapped", type:"string", title:"Description when Tile is double tapped", defaultValue:"", required:false)
}
input(name:"deviceAddRandom", type: "bool", title: "Add random string to albumArtUrl to override cache", defaultValue: false)
input(name:"deviceMetadata1", type:"string", title:"Open metadata1 attribute for external use", defaultValue:"", required:false)
input(name:"deviceMetadata2", type:"string", title:"Open metadata2 attribute for external use", defaultValue:"", required:false)
input(name:"deviceLogEnable", type: "bool", title: "Enable debug logging", defaultValue: false)
}
def installed() {
settings.deviceMomentary = false
settings.deviceTappedToggle = true
settings.deviceDoubleTappedToggle = true
settings.deviceOnEnable = true
settings.deviceOffEnable = true
settings.deviceTappedEnable = false
settings.deviceDoubleTappedEnable = false
settings.deviceLogEnable = false
settings.deviceAddRandom = false
sendEvent(name: "switch", value:"off", displayed: false)
logDebug "Executing 'installed()' with settings: ${settings}"
updated()
}
def updated() {
logDebug "Executing 'updated()' with new preferences: ${settings}"
sendEvent(name: "numberOfButtons", value: 1, displayed: false)
sendEvent(name: "status", value:"stopped", display: false, displayed: false) // this prevents playing bars on image grapic
sendEvent(name: "metadata1", value: settings.deviceMetadata1, displayed: false)
sendEvent(name: "metadata2", value: settings.deviceMetadata2, displayed: false)
logInfo "${device.displayName} preferences saved"
initialize()
}
def initialize() {
}
def parse(String description) {
logDebug description
}
def updateImage(image) {
def trackData = [:]
trackData["albumArtUrl"] = image + (settings.deviceAddRandom ? (image.toString().contains("?") ? "&" : "?") + "random=${new Date().getTime()}" : "")
logDebug "trackData: ${trackData}"
if(image!=null)
sendEvent(name:"trackData", value: JsonOutput.toJson(trackData), display: false, displayed: false)
}
def updateDescription(description) {
def trackDesc = description
logDebug "trackDescription: ${trackDesc}"
if(description!=null)
sendEvent(name:"trackDescription", value: trackDesc, display: false, displayed: false)
}
def on() {
if (settings.deviceOnEnable) {
updateImage(settings.deviceImageOn)
updateDescription(settings.deviceTextOn)
}
sendEvent(name: "switch", value: "on", display: false)
logInfo "${device.displayName} is on"
if(settings.deviceMomentary) runIn(1, off)
}
def off() {
if (settings.deviceOffEnable) {
updateImage(settings.deviceImageOff)
updateDescription(settings.deviceTextOff)
}
sendEvent(name: "switch", value: "off", display: false)
logInfo "${device.displayName} is off"
}
def push() {
play()
}
def play() {
logInfo "${device.displayName} was pushed"
sendEvent(name:"pushed", value: 1, descriptionText: "${device.displayName} was pushed", isStateChange: true )
if (settings.deviceTappedToggle)
toggle()
else if (settings.deviceTappedEnable) {
updateImage(settings.deviceImageTapped)
updateDescription(settings.deviceTextTapped)
}
}
def pause() {
logDebug "pause() was pressed" // this should not happen
}
def doubleTap() {
nextTrack()
}
def nextTrack() {
logInfo "${device.displayName} was doubleTapped"
sendEvent(name:"doubleTapped", value: 1, descriptionText: "${device.displayName} was doubleTapped", isStateChange: true )
if (settings.deviceDoubleTappedToggle)
toggle()
else if (settings.deviceDoubleTappedEnable) {
updateImage(settings.deviceImageDoubleTapped)
updateDescription(settings.deviceTextDoubleTapped)
}
}
def toggle() {
logDebug "toggle() state"
if(device.currentState("switch")?.value == "on")
off()
else
on()
}
private logInfo(msg) { log.info "${msg}" }
private logDebug(msg) { if(settings?.deviceLogEnable == true) { log.debug "${msg}" } }
private logTrace(msg) { if(settings?.deviceTraceEnable == true) { log.trace "${msg}" } }
private logWarn(msg) { log.warn "${msg}" }
private logError(msg) { log.error "${msg}" }