forked from sburke781/hubitat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMELCloudACUnit_driver.groovy
448 lines (361 loc) · 14.6 KB
/
MELCloudACUnit_driver.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* Mitsubishi Electric MELCloud AC Unit Child Driver
*
* Copyright 2019 Simon Burke
*
* 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.
*
* Change History:
*
* Date Who What
* ---- --- ----
* 2019-12-06 Simon Burke Original Creation
* 2020-01-04 Simon Burke Started adding Thermostat capability
* 2020-02-09 Simon Burke Thermostat capability and refreshing of state from MELCloud appear to be working
* Starting work on splitting into Parent / Child Driver
* 2020-02-16 Simon Burke Adjusted code to use new parent driver logging, i.e. Debug, Info and Error Logging methods
* Ensured all attributes were setup when device is created
* Called the on() method in various other commands, ensuring the unit is turned on first
* Various minor code refinements
*
*
*/
import java.text.DecimalFormat;
metadata {
definition (name: "MELCloud AC Unit", namespace: "simnet", author: "Simon Burke") {
capability "Refresh"
capability "Thermostat"
attribute "authCode", "string"
attribute "unitId", "number"
//Thermostat capability attributes - showing default values from HE documentation
/*
attribute "coolingSetpoint", "NUMBER"
attribute "heatingSetpoint", "NUMBER"
attribute "schedule", "JSON_OBJECT"
attribute "supportedThermostatFanModes", "ENUM", ['on', 'circulate', 'auto']
attribute "supportedThermostatModes", "ENUM", ['auto', 'off', 'heat', 'emergency heat', 'cool']
attribute "temperature", "NUMBER"
attribute "thermostatFanMode", "ENUM", ['on', 'circulate', 'auto']
attribute "thermostatMode", "ENUM", ['auto', 'off', 'heat', 'emergency heat', 'cool']
attribute "thermostatOperatingState", "ENUM", ['heating', 'pending cool', 'pending heat', 'vent economizer', 'idle', 'cooling', 'fan only']
attribute "thermostatSetpoint", "NUMBER"
*/
attribute "lastRunningMode", "STRING"
//MELCloud specific commands:
command "on"
//Thermostat capability commands
/*
command "auto"
command "cool"
command "emergencyHeat" //Unsupported in MELCloud
command "fanAuto"
command "fanCirculate"
command "fanOn"
command "heat"
command "off"
command "setCoolingSetpoint", [[name:"temperature*", type: "NUMBER", description: "Enter the Cooling Set Point" ] ]
// temperature required (NUMBER) - Cooling setpoint in degrees
command "setHeatingSetpoint", [[name:"temperature*", type: "NUMBER", description: "Enter the Heating Set Point" ] ]
// temperature required (NUMBER) - Heating setpoint in degrees
command "setSchedule", [[name:"JSON_OBJECT", type: "JSON_OBJECT", description: "Enter the JSON for the schedule" ] ]
// JSON_OBJECT (JSON_OBJECT) - JSON_OBJECT
*/
//Providing command with fan modes supported by MELCloud
//command "setThermostatFanMode", [[name:"fanmode*", type: "ENUM", description: "Pick a Fan Mode", constraints: ["Low", "Mid", "High", "Auto"] ] ]
// fanmode required (ENUM) - Fan mode to set
//command "setThermostatMode", [[name:"thermostatmode*", type: "ENUM", description: "Pick a Thermostat Mode", constraints: ['Heat', 'Dry', 'Cool', 'Fan', 'Auto'] ] ]
// thermostatmode required (ENUM) - Thermostat mode to set
}
}
def refresh() {
// Retrieve current state information from MEL Cloud Service
//getRooms()
//initialize()
parent.infoLog("refresh unit ${device.currentValue("unitId", true)}");
unitCommand();
}
def initialize() {
// Adjust default enumerations setup as part of the Hubitat Thermostat capability
sendEvent(name: 'supportedThermostatFanModes', value: ['Low', 'Mid', 'High', 'Auto'])
sendEvent(name: 'supportedThermostatModes', value: ['heat', 'dry', 'cool', 'fan', 'auto', 'off'])
if ("${device.currentValue("coolingSetpoint")}" > 40)
{ sendEvent(name: "coolingSetpoint", value: "23.0")}
if ("${device.currentValue("heatingSetpoint")}" > 40)
{ sendEvent(name: "heatingSetpoint", value: "23.0")}
}
def getFanModeMap() {
[
0:"auto",
1:"low",
2:"low-medium",
3:"medium",
5:"medium-high",
6:"high"
]
}
def getModeMap() {
[
1:"heat",
2:"dry",
3:"cool",
7:"fan",
8:"auto"
]
}
def getRooms() {
//retrieves current stat information for the ac unit
def vUnitId = ""
def vRoom = ""
def vPower = ""
def vMode = ""
def vModeDesc = ""
def vTemp = ""
def vSetTemp = ""
def bodyJson = "{ }"
def headers = [:]
headers.put("Content-Type", "application/json")
headers.put("Cookie", "auth=${parent.currentValue("authCode", true)}")
headers.put("accept", "application/json, text/javascript, */*; q=0.01")
def postParams = [
uri: "${parent.getBaseURL()}rooms.aspx",
headers: headers,
contentType: "application/json",
body : bodyJson
]
parent.debugLog("${bodyJson}, ${headers.Cookie}")
try {
httpPost(postParams) { resp ->
parent.debugLog("GetRooms: Initial data returned from rooms.aspx: ${resp.data}")
resp?.data?.each { building -> // Each Building
building?.units?.each // Each AC Unit / Room
{ acUnit ->
vRoom = acUnit.room
vUnitId = acUnit.unitid
vPower = acUnit.power
vMode = acUnit.mode
vTemp = acUnit.temp
vSetTemp = acUnit.settemp
parent.debugLog("updating ${vUnitId}")
def statusInfo = [:]
statusInfo.unitid = acUnit.unitid
statusInfo.power = acUnit.power
statusInfo.setmode = acUnit.mode.toInteger()
statusInfo.settemp = acUnit.settemp
statusInfo.roomtemp = acUnit.temp
log.debug("${vRoom}(${vUnitId}): ${statusInfo}")
applyResponseStatus(statusInfo)
}
}
}
}
catch (Exception e) {
parent.errorLog("GetRooms : Unable to query Mitsubishi Electric cloud: ${e}")
}
}
def unitCommand(command) {
// Re-usable method that submits a command to the MEL Cloud Service, based on the command text passed in
// See https://github.com/NovaGL/diy-melview for more details on commands and this API more generally
def bodyJson
if (command != null) {
bodyJson = "{ \"unitid\": \"${device.currentValue("unitId", true)}\", \"v\": 2, \"commands\": \"${command}\", \"lc\": 1 }"
} else {
bodyJson = "{ \"unitid\": \"${device.currentValue("unitId", true)}\", \"v\": 2 }"
}
def headers = [:]
headers.put("Content-Type", "application/json")
headers.put("Cookie", "auth=${parent.currentValue("authCode", true)}")
headers.put("accept", "application/json")
def postParams = [
uri: "${parent.getBaseURL()}unitcommand.aspx",
headers: headers,
contentType: "application/json",
body : bodyJson
]
debugLog("will post: ${bodyJson}") //, ${headers.Cookie}")
try {
def statusInfo = [:]
httpPost(postParams) { resp ->
debugLog("UnitCommand (${command}) got response - ${resp.data}")
if (resp?.data?.error == "ok") {
def unit = resp.data
statusInfo.unitid = unit.id
statusInfo.power = unit.power
statusInfo.setmode = unit.setmode
statusInfo.settemp = unit.settemp
statusInfo.roomtemp = unit.roomtemp
statusInfo.setfan = unit.setfan
debugLog(statusInfo)
applyResponseStatus(statusInfo)
} else {
parent.errorLog("UnitCommand (${command}): Error from Mitsubishi Electric cloud: ${resp?.data?.error?.fault}")
}
}
}
catch (Exception e) {
parent.errorLog("UnitCommand (${command}): Unable to query Mitsubishi Electric cloud: ${e}")
}
}
def applyResponseStatus(statusInfo) {
debugLog("applyResponseStatus: incoming mode: ${statusInfo.setmode}")
def vModeDesc = ""
if (statusInfo.power == "q" || statusInfo.power == 0) {vModeDesc = "off"}
else {
vModeDesc = modeMap[statusInfo.setmode]
}
//log.debug(modeMap[8])
debugLog("parsed mode: ${vModeDesc}")
def tempscaleUnit = "°${location.temperatureScale}"
def roomtempValue = convertTemperatureIfNeeded(statusInfo.roomtemp.toFloat(),"c",1)
sendEvent(name: "temperature", value: roomtempValue, unit: tempscaleUnit)
sendEvent(name: "thermostatMode", value: "${vModeDesc}")
def operatingState
if (vModeDesc == "off") {
operatingState = "idle"
} else if (vModeDesc == "heat") {
operatingState = "heating"
} else if (vModeDesc == "cool") {
operatingState = "cooling"
} else if (vModeDesc == "auto") {
operatingState = "cooling"
}
log.debug("operatingState: ${operatingState}")
if (operatingState != null) {
sendEvent(name: "thermostatOperatingState", value: operatingState)
if (operatingState != "idle") {
sendEvent(name: "lastRunningMode", value: "${vModeDesc}")
}
}
debugLog("checking settemp: ${statusInfo.settemp}")
def setTemp = statusInfo.settemp
def setTempValue = convertTemperatureIfNeeded(statusInfo.settemp.toFloat(),"c",1)
sendEvent(name: "coolingSetpoint" , value: setTempValue, unit: tempscaleUnit)
sendEvent(name: "heatingSetpoint" , value: setTempValue, unit: tempscaleUnit)
sendEvent(name: "thermostatSetpoint" , value: setTempValue, unit: tempscaleUnit)
debugLog("checking setfan: ${statusInfo.setfan}")
if (statusInfo.setfan != null) {
def fanMode = fanModeMap[statusInfo.setfan]
sendEvent(name: "thermostatFanMode" , value: fanMode)
}
}
//Unsupported commands from Thermostat capability
def emergencyHeat() { parent.debugLog("Emergency Heat Command not supported by MELCloud") }
//Fan Adjustments
//MELCloud Commands ('FS' 2 - LOW, 3 - MID, 5 - HIGH, 0 - AUTO)
def fanAuto() {
unitCommand("FS0")
}
def fanCirculate() {
unitCommand("FS5")
}
//fanOn - see modes section
//Scheduling Adjustments - Unsupported at present
def setSchedule(JSON_OBJECT) {parent.debugLog("setSchedule not currently supported by MELCloud")}
//Temperature Adjustments
def setCoolingSetpoint(temperature) {
def temperatureValue = convertTemperatureIfNeeded(temperature.toFloat(),"c",1)
sendEvent(name: "coolingSetpoint", value : temperatureValue, unit: "°${location.temperatureScale}")
parent.infoLog("${device.label} - Cooling Set Point adjusted to ${temperature}")
def setTemp = setTemperatureCommand(temperature)
}
def setHeatingSetpoint(temperature) {
def temperatureValue = convertTemperatureIfNeeded(temperature.toFloat(),"c",1)
sendEvent(name: "heatingSetpoint", value : temperatureValue, unit: "°${location.temperatureScale}")
parent.infoLog("${device.label} - Heating Set Point adjusted to ${temperature}")
def setTemp = setTemperatureCommand(temperature)
}
def setThermostatFanMode(fanmode) {
debugLog("setThermostatFanMode: Fan Mode set to ${fanmode}")
sendEvent(name: "thermostatFanMode", value : fanmode)
parent.infoLog("${device.label} - Fan Mode set to ${fanmode}")
}
def setThermostatMode(thermostatmodeX) {
if (thermostatmodeX != device.currentValue("thermostatMode")) {
sendEvent(name: "thermostatMode", value : thermostatmodeX)
sendEvent(name: "thermostatOperatingState", value : thermostatmodeX)
parent.infoLog("${device.label} - Thermostat Mode Set to ${thermostatmodeX}")
if (thermostatmodeX == 'dry') { dry() }
if (thermostatmodeX == 'cool') { cool() }
if (thermostatmodeX == 'heat') { heat() }
if (thermostatmodeX == 'auto') { auto() }
if (thermostatmodeX == 'fan') { fanOn() }
if (thermostatmodeX == 'off') { off() }
}
}
def setTemperature(temperature) {
debugLog("setTemperature: ${temperature}")
def tempSet = setTemperatureCommand(temperature)
sendEvent(name: "thermostatSetpoint", value: tempSet, unit: "°${location.temperatureScale}")
}
def setTemperatureCommand(temperature) {
debugLog("setTemperatureCommand: '${temperature}'")
if (temperature == null) {
return;
}
DecimalFormat tf = new DecimalFormat ("##.0");
def tempParam = tf.format(temperature);
debugLog("setTemperatureCommand: will request ${tempParam}")
unitCommand("TS${tempParam}")
return tempParam
}
//Power and State Adjustments
def on() {
unitCommand("PW1")
}
def off() {
unitCommand("PW0")
}
//Modes:
//"MD1" - HEAT
//"MD2" - DRY
//"MD3" - Cooling
//"MD7" - FAN
//"MD8" - Auto
def auto() {
on()
unitCommand("MD8")
}
def fanOn() {
on()
unitCommand("MD7")
}
def cool() {
on()
unitCommand("MD3")
if (device.currentValue("coolingSetpoint") != null) {
setTemperature(device.currentValue("coolingSetpoint"))
} else {
// rely on response parsing setting status of the setpoints so user will see what it's currently set to
}
}
def dry() {
on()
unitCommand("MD2")
}
def heat() {
on()
unitCommand("MD1")
if (device.currentValue("heatingSetpoint") != null) {
setTemperature(device.currentValue("heatingSetpoint"))
} else {
// rely on response parsing setting status of the setpoints so user will see what it's currently set to
}
}
def debugLog(debugMessage) {
//if (parent.DebugLogging == true)
log.debug(debugMessage)
}
def errorLog(errorMessage) {
//if (parent.ErrorLogging == true)
log.error(errorMessage)
}
def infoLog(infoMessage) {
//if(parent.InfoLogging == true)
log.info(infoMessage)
}