-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsmart-blinds.groovy
121 lines (97 loc) · 3.07 KB
/
smart-blinds.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
/**
* NodeMCU Blinds Switch Device Handler
*
* Copyright 2018 Viet Nguyen
*/
import java.security.MessageDigest
preferences {
input("serverIP", "text", title: "NodeMCU Server IP Address")
}
metadata {
definition (name: "NodeMCU Blinds", namespace: "smartthings-users", author: "Viet Nguyen") {
capability "Switch"
capability "Refresh"
capability "Polling"
command "subscribe"
command "setOffline"
}
simulator {}
tiles(scale: 2) {
multiAttributeTile(name:"switch", type: "lighting", width: 6, height: 4, canChangeIcon: true){
tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
attributeState "on", label:'${name}', action:"switch.off", icon:"st.switches.switch.on", backgroundColor:"#79b821", nextState:"off"
attributeState "off", label:'${name}', action:"switch.on", icon:"st.switches.switch.off", backgroundColor:"#ffffff", nextState:"On"
attributeState "offline", label:'${name}', icon:"st.switches.switch.off", backgroundColor:"#cccccc"
}
}
standardTile("refresh", "device.switch", inactiveLabel: false, height: 2, width: 2, decoration: "flat") {
state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh"
}
main "switch"
details(["switch","refresh"])
}
}
def setLevel(value) {
log.trace "setLevel($value)"
sendEvent(name: "level", value: value)
}
// handle commands
def on() {
sendEvent(name: "switch", value: 'on')
apiGet("open")
}
def off() {
sendEvent(name: "switch", value: 'off')
apiGet("close")
}
private apiGet(action) {
log.debug "Posting Body: NODEMCU"
def result = new physicalgraph.device.HubAction(
method: 'GET',
path: '/'+action,
headers: [
HOST: settings.serverIP + ':80',
'Content-Type': 'application/json'
],
body: []
)
return sendHubCommand(result)
}
def refresh() {
log.debug "Executing NodeMCU Blinds refresh'"
poll()
}
def poll() {
log.debug "Executing NodeMCU Blinds POLL'"
def cmd = new physicalgraph.device.HubAction([
method: 'GET',
path: '/status',
headers: [
HOST: settings.serverIP + ':80',
Accept: "*/*"
]
], '', [callback: calledBackHandler])
def result = sendHubCommand(cmd)
log.debug "cmd: $cmd"
log.debug "result: $result"
}
void calledBackHandler(physicalgraph.device.HubResponse hubResponse) {
def body = hubResponse.json;
log.debug "body $body"
if(body.isOpen == 1){
sendEvent(name: "switch", value: 'on')
}else if(body.isOpen == 0){
sendEvent(name: "switch", value: 'off')
}else{
log.debug "OFFFFFFFLINE"
setOffline()
}
}
def updated() {
log.debug "Executing NodeMCU Blinds UPDATED'"
unschedule()
runEvery1Minute(poll)
}
def setOffline() {
sendEvent(name: "switch", value: "offline", descriptionText: "The device is offline")
}