-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathHTTPdevice-fan.groovy
230 lines (215 loc) · 9.4 KB
/
HTTPdevice-fan.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* Generic HTTP Device v1.0.20160402
*
* Source code can be found here: https://github.com/JZ-SmartThings/SmartThings/blob/master/Devices/Generic%20HTTP%20Device/GenericHTTPDevice.groovy
*
* Copyright 2016 JZ
*
* 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.
*
*/
import groovy.json.JsonSlurper
metadata {
definition (name: "Generic HTTP Device - ESP8266 - Tower Fan", author: "JZ", namespace:"JZ") {
capability "Switch"
attribute "triggerswitch", "string"
attribute "oscswitch", "string"
attribute "timeradd", "string"
attribute "timerminus", "string"
attribute "speed", "string"
attribute "modeswitch", "string"
command "DeviceTrigger"
command "oscTrigger"
command "speedTrigger"
command "timerAddTrigger"
command "timerMinusTrigger"
command "modeTrigger"
}
preferences {
input("DeviceIP", "string", title:"Device IP Address", description: "Please enter your device's IP Address", required: true, displayDuringSetup: true)
input("DevicePort", "string", title:"Device Port", description: "Please enter port 80 or your device's Port", required: true, displayDuringSetup: true)
input("DevicePath", "string", title:"URL Path", description: "Rest of the URL, include forward slash.", displayDuringSetup: true)
input(name: "DevicePostGet", type: "enum", title: "POST or GET", options: ["POST","GET"], required: true, displayDuringSetup: true)
input("DeviceBodyText", "string", title:'Body Content', description: 'Type in "GateTrigger=" or "CustomTrigger="', required: true, displayDuringSetup: true)
input("UseJSON", "bool", title:"Use JSON instead of HTML?", description: "Use JSON instead of HTML?", defaultValue: false, required: false, displayDuringSetup: true)
section() {
input("HTTPAuth", "bool", title:"Requires User Auth?", description: "Choose if the HTTP requires basic authentication", defaultValue: false, required: true, displayDuringSetup: true)
input("HTTPUser", "string", title:"HTTP User", description: "Enter your basic username", required: false, displayDuringSetup: true)
input("HTTPPassword", "string", title:"HTTP Password", description: "Enter your basic password", required: false, displayDuringSetup: true)
}
}
simulator {
}
tiles {
standardTile("DeviceTrigger", "device.triggerswitch", width: 2, height: 2, canChangeIcon: true, canChangeBackground: true) {
state "triggeroff", label:'OFF' , action: "on", icon: "st.Appliances.appliances11", backgroundColor:"#ffffff", nextState: "trying"
state "triggeron", label: 'ON', action: "off", icon: "st.Appliances.appliances11", backgroundColor: "#79b821", nextState: "trying"
state "trying", label: 'TRYING', action: "", icon: "st.Appliances.appliances11", backgroundColor: "#FFAA33"
}
standardTile("oscTrigger", "device.oscswitch", width: 2, height: 2, canChangeIcon: true, canChangeBackground: true) {
state "default", label:'' , action: "oscTrigger", icon: "st.thermostat.fan-circulate", backgroundColor:"#8fff6c", nextState: "trying"
state "trying", label: 'TRYING', action: "", icon: "st.thermostat.fan-circulate", backgroundColor: "#FFAA33"
}
standardTile("speedTrigger", "device.speed", width: 1, height: 1, canChangeIcon: true, canChangeBackground: true, decoration: "flat") {
state "default", label:'Speed +' , action: "speedTrigger", icon: "st.Weather.weather1", backgroundColor:"#ff4c4c", nextState: "trying"
state "trying", label: 'TRYING', action: "", icon: "st.Weather.weather1", backgroundColor: "#FFAA33"
}
standardTile("modeTrigger", "device.modeswitch", width: 1, height: 1, canChangeIcon: true, canChangeBackground: true, decoration: "flat") {
state "default", label:'MODE' , action: "modeTrigger", icon: "st.Weather.weather4", backgroundColor:"#03f4fb", nextState: "trying"
state "trying", label: 'TRYING', action: "", icon: "st.Weather.weather4", backgroundColor: "#FFAA33"
}
standardTile("timerAddTrigger", "device.timeradd", width: 1, height: 1, canChangeIcon: true, canChangeBackground: true, decoration: "flat") {
state "default", label:'Timer +', action: "timerAddTrigger", icon: "st.thermostat.thermostat-up", backgroundColor:"#fff400", nextState: "trying"
state "trying", label: 'TRYING', action: "", icon: "st.thermostat.thermostat-up", backgroundColor: "#FFAA33"
}
standardTile("timerMinusTrigger", "device.timerminus", width: 1, height: 1, canChangeIcon: true, canChangeBackground: true, decoration: "flat") {
state "default", label:'Timer -' , action: "timerMinusTrigger", icon: "st.thermostat.thermostat-down", backgroundColor:"#fff400", nextState: "trying"
state "trying", label: 'TRYING', action: "", icon: "st.thermostat.thermostat-down", backgroundColor: "#FFAA33"
}
main "DeviceTrigger"
details(["DeviceTrigger", "oscTrigger", "modeTrigger", "speedTrigger", "timerAddTrigger", "timerMinusTrigger"])
}
}
def on() {
log.debug "Triggered on!!!"
sendEvent(name: "triggerswitch", value: "triggeron", isStateChange: true)
state.fan = "on";
runCmd("fan=on")
}
def off() {
log.debug "Triggered off!!!"
sendEvent(name: "triggerswitch", value: "triggeroff", isStateChange: true)
state.fan = "off";
runCmd("fan=power")
}
def oscTrigger() {
log.debug "OSC triggered!!!"
sendEvent(name: "oscswitch", value: "default", isStateChange: true)
runCmd("fan=osc")
}
def speedTrigger() {
log.debug "Add Speed!!!"
sendEvent(name: "speed", value: "default", isStateChange: true)
runCmd("fan=speed")
}
def timerAddTrigger() {
log.debug "Timer ++!!!"
sendEvent(name: "timeradd", value: "default", isStateChange: true)
runCmd("fan=timeradd")
}
def timerMinusTrigger() {
log.debug "Timer --!!!"
sendEvent(name: "timerminus", value: "default", isStateChange: true)
runCmd("fan=timerminus")
}
def modeTrigger() {
log.debug "Mode changed!!!"
sendEvent(name: "modeswitch", value: "default", isStateChange: true)
runCmd("fan=mode")
}
def runCmd(String varCommand) {
def host = DeviceIP
def hosthex = convertIPtoHex(host).toUpperCase()
def porthex = convertPortToHex(DevicePort).toUpperCase()
device.deviceNetworkId = "$hosthex:$porthex"
def userpassascii = "${HTTPUser}:${HTTPPassword}"
def userpass = "Basic " + userpassascii.encodeAsBase64().toString()
log.debug "The device id configured is: $device.deviceNetworkId"
//def path = DevicePath
def path = DevicePath + varCommand
log.debug "path is: $path"
log.debug "Uses which method: $DevicePostGet"
def body = ""//varCommand
log.debug "body is: $body"
def headers = [:]
headers.put("HOST", "$host:$DevicePort")
headers.put("Content-Type", "application/x-www-form-urlencoded")
if (HTTPAuth) {
headers.put("Authorization", userpass)
}
log.debug "The Header is $headers"
def method = "GET"
try {
if (DevicePostGet.toUpperCase() == "GET") {
method = "GET"
}
}
catch (Exception e) {
settings.DevicePostGet = "POST"
log.debug e
log.debug "You must not have set the preference for the DevicePOSTGET option"
}
log.debug "The method is $method"
try {
def hubAction = new physicalgraph.device.HubAction(
method: method,
path: path,
body: body,
headers: headers
)
hubAction.options = [outputMsgToS3:false]
//log.debug hubAction
hubAction
}
catch (Exception e) {
log.debug "Hit Exception $e on $hubAction"
}
}
def parse(String description) {
//log.debug "Parsing '${description}'"
def whichTile = ''
log.debug "state.fan " + state.fan
if (state.fan == "on") {
//sendEvent(name: "triggerswitch", value: "triggergon", isStateChange: true)
whichTile = 'mainon'
}
if (state.fan == "off") {
//sendEvent(name: "triggerswitch", value: "triggergoff", isStateChange: true)
whichTile = 'mainoff'
}
//RETURN BUTTONS TO CORRECT STATE
log.debug 'whichTile: ' + whichTile
switch (whichTile) {
case 'mainon':
def result = createEvent(name: "switch", value: "on", isStateChange: true)
return result
case 'mainoff':
def result = createEvent(name: "switch", value: "off", isStateChange: true)
return result
default:
def result = createEvent(name: "testswitch", value: "default", isStateChange: true)
//log.debug "testswitch returned ${result?.descriptionText}"
return result
}
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
//log.debug "IP address entered is $ipAddress and the converted hex code is $hex"
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
//log.debug hexport
return hexport
}
private Integer convertHexToInt(hex) {
Integer.parseInt(hex,16)
}
private String convertHexToIP(hex) {
//log.debug("Convert hex to ip: $hex")
[convertHexToInt(hex[0..1]),convertHexToInt(hex[2..3]),convertHexToInt(hex[4..5]),convertHexToInt(hex[6..7])].join(".")
}
private getHostAddress() {
def parts = device.deviceNetworkId.split(":")
//log.debug device.deviceNetworkId
def ip = convertHexToIP(parts[0])
def port = convertHexToInt(parts[1])
return ip + ":" + port
}