-
Notifications
You must be signed in to change notification settings - Fork 97
/
httpGetSwitch.groovy
68 lines (60 loc) · 1.86 KB
/
httpGetSwitch.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
/*
* Http GET Switch
*
* Calls URIs with HTTP GET for switch on or off
*
*/
metadata {
definition(name: "Http GET Switch", namespace: "community", author: "Community", importUrl: "https://raw.githubusercontent.com/hubitat/HubitatPublic/master/examples/drivers/httpGetSwitch.groovy") {
capability "Actuator"
capability "Switch"
capability "Sensor"
}
}
preferences {
section("URIs") {
input "onURI", "text", title: "On URI", required: false
input "offURI", "text", title: "Off URI", required: false
input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
}
}
def logsOff() {
log.warn "debug logging disabled..."
device.updateSetting("logEnable", [value: "false", type: "bool"])
}
def updated() {
log.info "updated..."
log.warn "debug logging is: ${logEnable == true}"
if (logEnable) runIn(1800, logsOff)
}
def parse(String description) {
if (logEnable) log.debug(description)
}
def on() {
if (logEnable) log.debug "Sending on GET request to [${settings.onURI}]"
try {
httpGet(settings.onURI) { resp ->
if (resp.success) {
sendEvent(name: "switch", value: "on", isStateChange: true)
}
if (logEnable)
if (resp.data) log.debug "${resp.data}"
}
} catch (Exception e) {
log.warn "Call to on failed: ${e.message}"
}
}
def off() {
if (logEnable) log.debug "Sending off GET request to [${settings.offURI}]"
try {
httpGet(settings.offURI) { resp ->
if (resp.success) {
sendEvent(name: "switch", value: "off", isStateChange: true)
}
if (logEnable)
if (resp.data) log.debug "${resp.data}"
}
} catch (Exception e) {
log.warn "Call to off failed: ${e.message}"
}
}