-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathST-Spark.txt
111 lines (96 loc) · 3.38 KB
/
ST-Spark.txt
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
/**
* Spark Relay
*
* Author: [email protected]
* Date: 2014-01-11
*
*
* Special Thanks to amanfredi from the #Spark IRC Channel for helping me sort
* out the httpPost issues I was having
*/
preferences {
section("Use this switch for the deck led strip..") {
input "Deck", "capability.switch", title: "Deck?"
}
section("Use this switch for the sprinklers..") {
input "Sprinklers", "capability.switch", title: "Sprinklers?"
}
section("Spark Core ID..") {
input "SparkID", "parameters", title: "Spark Core ID?"
}
section("Access Token..") {
input "AccessToken", "parameters", title: "Access Token?"
}
}
def installed() {
log.trace "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
// TODO: subscribe to attributes, devices, locations, etc.
subscribe(Deck, "switch", deckHandler)
subscribe(Sprinklers, "switch", sprinklerHandler)
}
def deckHandler(evt) {
log.trace evt.value
if (evt.value == "on") {
turnRelayOnDeck('Deck')
} else if (evt.value == "off") {
turnRelayOffDeck('Deck')
}
}
def sprinklerHandler(evt) {
log.trace evt.value
if (evt.value == "on") {
turnRelayOnSprinkler('sprinkler')
} else if (evt.value == "off") {
turnRelayOffSprinkler('sprinkler')
}
}
def turnRelayOnDeck(group) {
if (group == "") {
group = "all"
}
def successClosure = { response ->
log.debug "Request was successful, $response"
}
def postBody = "access_token=${AccessToken}¶ms=r1%2CLOW"
httpPost("https://api.spark.io/v1/devices/${SparkID}/relay", postBody, successClosure)
}
def turnRelayOffDeck(group) {
log.trace "turnRelayOff: ${group}"
if (group == "") {
group = "all"
}
def successClosure = { response ->
log.debug "Request was successful, $response"
}
def postBody = "access_token=${AccessToken}¶ms=r1%2CHIGH"
httpPost("https://api.spark.io/v1/devices/${SparkID}/relay", postBody, successClosure)
}
def turnRelayOnSprinkler(group) {
if (group == "") {
group = "all"
}
def successClosure = { response ->
log.debug "Request was successful, $response"
}
def postBody = "access_token=${AccessToken}¶ms=r3%2CLOW"
httpPost("https://api.spark.io/v1/devices/${SparkID}/relay", postBody, successClosure)
}
def turnRelayOffSprinkler(group) {
log.trace "turnRelayOff: ${group}"
if (group == "") {
group = "all"
}
def successClosure = { response ->
log.debug "Request was successful, $response"
}
def postBody = "access_token=${AccessToken}¶ms=r3%2CHIGH"
httpPost("https://api.spark.io/v1/devices/${SparkID}/relay", postBody, successClosure)
}