-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMitsubishi_Aircon_Device_Handler.groovy
586 lines (523 loc) · 18.9 KB
/
Mitsubishi_Aircon_Device_Handler.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
Mitsubishi Aircon SmartThings Device Handler
This device handler works only in combination with the
https://github.com/JMan7777/Mitsubishi-Aircon-SmartThings
Arduino sketch installed on an ESP32
Copyright (c) 2020 Markus Jessl. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import groovy.json.JsonSlurper
metadata {
definition (
name: "Mitsubishi Aircon Device Handler",
namespace: "JMan7777",
author: "Markus Jessl")
{
capability "Switch"
capability "Refresh"
capability "Temperature Measurement"
command "tempUp"
command "tempDown"
command "fan"
command "vSwing"
command "hSwing"
command "mode"
}
preferences {
input name: "DeviceIP", type: "text", title: "Device IP Address", description: "ESP32 ip address", defaultValue: "192.168.1.123", required: true, displayDuringSetup: true
input name: "DevicePort", type: "text", title: "Device Port", description: "ESP32 webserver port", defaultValue: "80", required: true, displayDuringSetup: true
input name: "DeviceTemperatureUnit", type: "text", title: "Temp Unit", description: "Temperature Unit (C or F)", defaultValue: "C", required: true, displayDuringSetup: true
}
simulator {
}
tiles(scale: 2) {
multiAttributeTile(name:"thermostatFull", type:"thermostat", width:6, height:4) {
tileAttribute("device.temperature", key: "PRIMARY_CONTROL") {
attributeState("temperature", label:'${currentValue}', defaultState: true,
backgroundColors:[
//Celsius
[value: 16, color: "#153591"],
[value: 19, color: "#1e9cbb"],
[value: 21, color: "#90d2a7"],
[value: 23, color: "#44b621"],
[value: 26, color: "#f1d801"],
[value: 29, color: "#d04e00"],
[value: 31, color: "#bc2323"],
//Fahrenheit
[value: 38, color: "#153591"],
[value: 44, color: "#1e9cbb"],
[value: 59, color: "#90d2a7"],
[value: 74, color: "#44b621"],
[value: 84, color: "#f1d801"],
[value: 95, color: "#d04e00"],
[value: 96, color: "#bc2323"]
])
}
tileAttribute("device.temperature", key: "VALUE_CONTROL") {
attributeState("VALUE_UP", action: "tempUp")
attributeState("VALUE_DOWN", action: "tempDown")
}
}
standardTile("switch", "device.switch", width: 2, height: 2, decoration: "flat", canChangeIcon: true) {
state "off", label:'Off', action:"switch.on", icon:"st.thermostat.ac.air-conditioning", backgroundColor:"#ffffff", nextState:"turningOn"
state "on", label:'On', action:"switch.off", icon:"st.thermostat.ac.air-conditioning", backgroundColor:"#00a0dc", nextState:"turningOff"
state "turningOn", label:'Turning on', action:"switch.off", icon:"st.thermostat.ac.air-conditioning", backgroundColor:"#dcdcdc", nextState: "turningOff"
state "turningOff", label:'Turning off', action:"switch.on", icon:"st.thermostat.ac.air-conditioning", backgroundColor:"#dcdcdc", nextState: "turningOn"
state "?", label:'Unknown', icon:"st.thermostat.ac.air-conditioning", backgroundColor:"#ffffff"
}
valueTile("roomTemperature", "device.roomTemperature", width: 2, height: 2, decoration: "flat") {
state("roomTemperature", label:'${currentValue}', icon:"st.alarm.temperature.normal")
state("?", label:'Unknown', icon:"st.alarm.temperature.normal", backgroundColor:"#ffffff")
}
standardTile("fan", "device.fan", width: 2, height: 2, canChangeIcon: true, decoration: "flat") {
state "1", label:'1', action:"fan", icon:"st.Appliances.appliances11", backgroundColor:"#ffffff", nextState: "CHANGING"
state "2", label:'2', action:"fan", icon:"st.Appliances.appliances11", backgroundColor:"#ffffff", nextState: "CHANGING"
state "3", label:'3', action:"fan", icon:"st.Appliances.appliances11", backgroundColor:"#ffffff", nextState: "CHANGING"
state "4", label:'4', action:"fan", icon:"st.Appliances.appliances11", backgroundColor:"#ffffff", nextState: "CHANGING"
state "QUIET", label:'Quiet', action:"fan", icon:"st.Appliances.appliances11", backgroundColor:"#ffffff", nextState: "CHANGING"
state "AUTO", label:'Auto', action:"fan", icon:"st.Appliances.appliances11", backgroundColor:"#ffffff", nextState: "CHANGING"
state "CHANGING", label:'Changing', action:"fan", icon:"st.Appliances.appliances11", backgroundColor:"#dcdcdc", nextState: "AUTO"
state "?", label:'Unknown', icon:"st.Appliances.appliances11", backgroundColor:"#ffffff"
}
standardTile("vSwing", "device.vSwing", width: 2, height: 2, canChangeIcon: true, decoration: "flat") {
state "1", label:'1', action:"vSwing", icon:"st.vents.vent-open", backgroundColor:"#ffffff", nextState: "CHANGING"
state "2", label:'2', action:"vSwing", icon:"st.vents.vent-open", backgroundColor:"#ffffff", nextState: "CHANGING"
state "3", label:'3', action:"vSwing", icon:"st.vents.vent-open", backgroundColor:"#ffffff", nextState: "CHANGING"
state "4", label:'4', action:"vSwing", icon:"st.vents.vent-open", backgroundColor:"#ffffff", nextState: "CHANGING"
state "5", label:'5', action:"vSwing", icon:"st.vents.vent-open", backgroundColor:"#ffffff", nextState: "CHANGING"
state "SWING", label:'Swing', action:"vSwing", icon:"st.vents.vent-open", backgroundColor:"#ffffff", nextState: "CHANGING"
state "AUTO", label:'Auto', action:"vSwing", icon:"st.vents.vent-open", backgroundColor:"#ffffff", nextState: "CHANGING"
state "CHANGING", label:'Changing', action:"vSwing", icon:"st.vents.vent-open", backgroundColor:"#dcdcdc", nextState: "AUTO"
state "?", label:'Unknown', icon:"st.vents.vent-open", backgroundColor:"#ffffff"
}
standardTile("hSwing", "device.hSwing", width: 2, height: 2, canChangeIcon: true, decoration: "flat") {
state "<<", label:'<<', action:"hSwing", icon:"st.motion.motion.inactive", backgroundColor:"#ffffff", nextState: "CHANGING"
state "<", label:'<', action:"hSwing", icon:"st.motion.motion.inactive", backgroundColor:"#ffffff", nextState: "CHANGING"
state "|", label:'|', action:"hSwing", icon:"st.motion.motion.inactive", backgroundColor:"#ffffff", nextState: "CHANGING"
state ">", label:'>', action:"hSwing", icon:"st.motion.motion.inactive", backgroundColor:"#ffffff", nextState: "CHANGING"
state ">>", label:'>>', action:"hSwing", icon:"st.motion.motion.inactive", backgroundColor:"#ffffff", nextState: "CHANGING"
state "<>", label:'<>', action:"hSwing", icon:"st.motion.motion.inactive", backgroundColor:"#ffffff", nextState: "CHANGING"
state "SWING", label:'Swing', action:"hSwing", icon:"st.motion.motion.inactive" , backgroundColor:"#ffffff", nextState: "CHANGING"
state "CHANGING", label:'Changing', action:"hSwing", icon:"st.motion.motion.inactive", backgroundColor:"#dcdcdc", nextState: "AUTO"
state "?", label:'Unknown', icon:"st.motion.motion.inactive", backgroundColor:"#ffffff"
}
standardTile("mode", "device.mode", width: 2, height: 2, canChangeIcon: true, decoration: "flat") {
state "COOL", label:'Cool', action:"mode", icon:"st.Weather.weather7", backgroundColor:"#ffffff", nextState: "CHANGING"
state "HEAT", label:'Heat', action:"mode", icon:"st.Weather.weather14", backgroundColor:"#ffffff", nextState: "CHANGING"
state "FAN", label:'Fan', action:"mode", icon:"st.Weather.weather1", backgroundColor:"#ffffff", nextState: "CHANGING"
state "DRY", label:'Dry', action:"mode", icon:"st.Weather.weather10", backgroundColor:"#ffffff", nextState: "CHANGING"
state "AUTO", label:'Auto', action:"mode", icon:"st.Weather.weather11", backgroundColor:"#ffffff", nextState: "CHANGING"
state "CHANGING", label:'Changing', action:"mode", icon:"st.Weather.weather11", backgroundColor:"#dcdcdc", nextState: "AUTO"
state "?", label:'Unknown', icon:"st.Weather.weather11", backgroundColor:"#ffffff"
}
standardTile("refresh", "device.refresh", height: 2, width: 2, decoration: "flat") {
state "default", label:'Refresh', action:"refresh", icon:"st.secondary.refresh", backgroundColor:"#ffffff", nextState:"refreshing"
state "refreshing", label:'Refreshing', icon:"st.secondary.refresh", backgroundColor:"#dcdcdc", nextState: "default"
}
main("switch")
details([
"thermostatFull",
"switch",
"refresh",
"roomTemperature",
"fan",
"vSwing",
"hSwing",
"mode"
])
}
}
def int fahrenheitToCelcius(int f) {
log.debug "Converting ${f}F to C"
//T(°C) = (T(°F) - 32) / 1.8
def float c = ((f - 32) / 1.8)
return c.round(0)
}
def int celsiusToFahrenheit(int c) {
log.debug "Converting ${c}C to F"
//T(°F) = T(°C) × 1.8 + 32
def float f = ((c * 1.8) + 32)
return f.round(0)
}
def installed() {
initialize()
}
def updated() {
initialize()
}
def initialize() {
resetRefreshTile()
unschedule(autoRefresh)
runEvery1Minute(autoRefresh)
}
def autoRefresh() {
def String refresh = device.currentValue("refresh")
if (refresh.equals("default"))
{
log.debug "Auto - Refresh"
sendEvent(name: "refresh", value: "refreshing", isStateChange: true)
runCmd("GET_SETTINGS", "")
}
else
{
log.debug "Auto - Refresh skipped, already refreshing"
}
}
def refresh() {
log.debug "Refresh"
runCmd("GET_SETTINGS", "")
}
def on() {
log.debug "Power on"
runCmd("POWER", "ON")
}
def off() {
log.debug "Power off"
runCmd("POWER", "OFF")
}
def tempUp() {
log.debug "Temp up"
def int temp = device.currentValue("temperature")
def String tempUnit = "${DeviceTemperatureUnit}"
log.debug "Temp Unit = ${tempUnit}"
if (tempUnit.equals("F"))
{
temp = fahrenheitToCelcius(temp)
}
else
{
log.debug "No temp conversion needed."
}
temp = temp + 1
if (temp <= 31)
{
runCmd("TEMP", "${temp}")
}
else
{
log.error "Temp out of range"
}
}
def tempDown() {
log.debug "Temp down"
def int temp = device.currentValue("temperature")
def String tempUnit = "${DeviceTemperatureUnit}"
log.debug "Temp Unit = ${tempUnit}"
if (tempUnit.equals("F"))
{
temp = fahrenheitToCelcius(temp)
}
else
{
log.debug "No temp conversion needed."
}
temp = temp - 1
if (temp >= 16)
{
runCmd("TEMP", "${temp}")
}
else
{
log.error "Temp out of range"
}
}
def fan() {
log.debug "Fan change"
def String fan = device.currentValue("fan")
def String fanNew = "${fan}"
switch (fan) {
case "1":
fanNew = "2"
break
case "2":
fanNew = "3"
break
case "3":
fanNew = "4"
break
case "4":
fanNew = "AUTO"
break
case "AUTO":
fanNew = "QUIET"
break
case "QUIET":
fanNew = "1"
break
default:
log.error "Unknown fan setting: ${fan}"
break
}
if (!fanNew.equals(fan))
{
runCmd("FAN", "${fanNew}")
}
else
{
log.debug "Fan settings unchanged."
}
}
def vSwing() {
log.debug "vSwing change"
def String vSwing = device.currentValue("vSwing")
def String vSwingNew = "${vSwing}"
switch (vSwing) {
case "1":
vSwingNew = "2"
break
case "2":
vSwingNew = "3"
break
case "3":
vSwingNew = "4"
break
case "4":
vSwingNew = "5"
break
case "5":
vSwingNew = "AUTO"
break
case "AUTO":
vSwingNew = "SWING"
break
case "SWING":
vSwingNew = "1"
break
default:
log.error "Unknown vSwing setting: ${vSwing}"
break
}
if (!vSwingNew.equals(vSwing))
{
runCmd("V_SWING", "${vSwingNew}")
}
else
{
log.debug "vSwing settings unchanged."
}
}
def hSwing() {
log.debug "hSwing change"
def String hSwing = device.currentValue("hSwing")
def String hSwingNew = "${hSwing}"
switch (hSwing) {
case "<<":
hSwingNew = "<"
break
case "<":
hSwingNew = "|"
break
case "|":
hSwingNew = ">"
break
case ">":
hSwingNew = ">>"
break
case ">>":
hSwingNew = "<>"
break
case "<>":
hSwingNew = "SWING"
break
case "SWING":
hSwingNew = "<<"
break
default:
log.error "Unknown hSwing setting: ${hSwing}"
break
}
if (!hSwingNew.equals(hSwing))
{
runCmd("H_SWING", "${hSwingNew}")
}
else
{
log.debug "vSwing settings unchanged."
}
}
def mode() {
log.debug "Mode change"
def String mode = device.currentValue("mode")
def String modeNew = "${mode}"
switch (mode) {
case "COOL":
modeNew = "HEAT"
break
case "HEAT":
modeNew = "FAN"
break
case "FAN":
modeNew = "DRY"
break
case "DRY":
modeNew = "AUTO"
break
case "AUTO":
modeNew = "COOL"
break
default:
log.error "Unknown mode setting: ${mode}"
break
}
if (!modeNew.equals(mode))
{
runCmd("MODE", "${modeNew}")
}
else
{
log.debug "Mode settings unchanged."
}
}
def runCmd(String varCommand, String value) {
def path = '/ChangeAirconSettings'
def body = ''
def method = "POST"
switch (varCommand) {
case "POWER":
body = '{"' + varCommand + '":"' + value + '"}';
log.debug "POST body is: $body"
break
case "MODE":
body = '{"' + varCommand + '":"' + value + '"}';
log.debug "POST body is: $body"
break
case "TEMP":
body = '{"' + varCommand + '":"' + value + '"}';
log.debug "POST body is: $body"
break
case "FAN":
body = '{"' + varCommand + '":"' + value + '"}';
log.debug "POST body is: $body"
break
case "V_SWING":
body = '{"' + varCommand + '":"' + value + '"}';
log.debug "POST body is: $body"
break
case "H_SWING":
body = '{"' + varCommand + '":"' + value + '"}';
log.debug "POST body is: $body"
break
case "GET_SETTINGS":
path = "/GetAirconSettings"
method = "GET"
break
default:
log.error "Wrong command = $varCommand"
break
}
def host = DeviceIP
def hosthex = convertIPtoHex(host).toUpperCase()
def porthex = convertPortToHex(DevicePort).toUpperCase()
device.deviceNetworkId = "$hosthex:$porthex"
log.debug "Device ID hex = $device.deviceNetworkId"
def headers = [:]
headers.put("HOST", "$host:$DevicePort")
headers.put("Content-Type", "application/json")
log.debug "ESP32 IP:Port = $headers"
try {
runIn(15, commandTimeOut)
def hubAction = new physicalgraph.device.HubAction(
method: method,
path: path,
body: body,
headers: headers
)
hubAction.options = [outputMsgToS3:false]
log.debug hubAction
sendHubCommand(hubAction)
}
catch (Exception e) {
log.error "Hit Exception $e on $hubAction"
resetRefreshTile()
}
}
def resetRefreshTile(){
sendEvent(name: "refresh", value: "default", isStateChange: true)
}
def commandTimeOut(){
log.error "Command to ESP32 timed out."
resetRefreshTile();
}
def parse(String description) {
def whichTile = ''
def map = [:]
def retResult = []
def descMap = parseDescriptionAsMap(description)
def bodyReturned = ' '
def headersReturned = ' '
if (descMap["body"]) {
bodyReturned = new String(descMap["body"].decodeBase64())
log.debug "Response: $bodyReturned"
}
if (descMap["headers"]) { headersReturned = new String(descMap["headers"].decodeBase64()) }
if (bodyReturned != ' ') {
def data = parseJson(bodyReturned)
def String roomTemp = "${data."ROOM_TEMP"}"
def String temp = "${data."TEMP"}"
if (roomTemp.equals("?"))
{
roomTemp = "0"
}
if (temp.equals("?"))
{
temp = "0"
}
def String tempUnit = "${DeviceTemperatureUnit}"
log.debug "Temp Unit = ${tempUnit}"
if (tempUnit.equals("F"))
{
roomTemp = "${celsiusToFahrenheit(roomTemp.toInteger())}"
temp = "${celsiusToFahrenheit(temp.toInteger())}"
}
else
{
log.debug "No temp conversion needed."
}
sendEvent(name: "roomTemperature", value: roomTemp, isStateChange: true)
sendEvent(name: "temperature", value: temp, isStateChange: true)
sendEvent(name: "mode", value: "${data."MODE"}", isStateChange: true)
sendEvent(name: "fan", value: "${data."FAN"}", isStateChange: true)
sendEvent(name: "vSwing", value: "${data."V_SWING"}", isStateChange: true)
sendEvent(name: "hSwing", value: "${data."H_SWING"}", isStateChange: true)
sendEvent(name: "switch", value: "${data."POWER"}".toLowerCase(), isStateChange: true)
}
log.debug "Response processed"
unschedule(commandTimeOut)
resetRefreshTile()
}
def parseDescriptionAsMap(description) {
description.split(",").inject([:]) { map, param ->
def nameAndValue = param.split(":")
map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
}
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
return hexport
}
private Integer convertHexToInt(hex) {
Integer.parseInt(hex,16)
}
private String convertHexToIP(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(":")
def ip = convertHexToIP(parts[0])
def port = convertHexToInt(parts[1])
return ip + ":" + port
}