forked from arthurkrupa/gree-hvac-mqtt-bridge
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
237 lines (220 loc) · 9.73 KB
/
index.js
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
#!/usr/bin/env node
'use strict';
const mqtt = require('mqtt');
const commands = require('./app/commandEnums');
const argv = require('minimist')(process.argv.slice(2), {
string: ['hvac-host', 'mqtt-broker-url', 'mqtt-topic-prefix', 'mqtt-username', 'mqtt-password', 'interval', 'name'],
'--': true,
});
/**
* Helper: get property key for value
* @param {*} value
*/
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
/**
* Connect to device
*/
const mqttTopicPrefix = argv['mqtt-topic-prefix'];
const deviceState = {
temperature: null,
fanSpeed: null,
swingHor: null,
swingVert: null,
power: null,
health: null,
powerSave: null,
lights: null,
quiet: null,
blow: null,
air: null,
sleep: null,
turbo: null,
mode: null
};
/**
* Check if incoming device setting differs from last state and publish change if yes
* @param {string} stateProp State property to be updated/compared with
* @param {string} newValue New incoming device state value
* @param {string} mqttTopic Topic (without prefix) to send with new value
*/
const publishIfChanged = function(stateProp, newValue, mqttTopic) {
if (newValue !== deviceState[stateProp]) {
deviceState[stateProp] = newValue;
client.publish(mqttTopicPrefix + mqttTopic, newValue);
}
};
const deviceOptions = {
host: argv['hvac-host'],
onStatus: (deviceModel) => {
publishIfChanged('temperature', deviceModel.props["wdNumber"].toString(), '/temperature/get');
//client.publish(mqttTopicPrefix + '/temperature/get', deviceModel.props["wdNumber"].toString());
publishIfChanged('temperature_in', deviceModel.props[commands.temperature.code].toString(), '/temperature_in/get');
//client.publish(mqttTopicPrefix + '/temperature_in/get', deviceModel.props[commands.temperature.code].toString());
publishIfChanged('fanSpeed', getKeyByValue(commands.fanSpeed.value, deviceModel.props[commands.fanSpeed.code]).toString(), '/fanspeed/get');
//client.publish(mqttTopicPrefix + '/fanspeed/get', commands.fanSpeed.value.getKeyByValue(deviceModel.props[commands.fanSpeed.code]).toString());
publishIfChanged('power', getKeyByValue(commands.power.value, deviceModel.props[commands.power.code]).toString(), '/power/get');
//client.publish(mqttTopicPrefix + '/power/get', commands.power.value.getKeyByValue(deviceModel.props[commands.power.code]).toString());
publishIfChanged('health', getKeyByValue(commands.health.value, deviceModel.props[commands.health.code]).toString(), '/health/get');
//client.publish(mqttTopicPrefix + '/health/get', commands.health.value.getKeyByValue(deviceModel.props[commands.health.code]).toString());
//publishIfChanged('powerSave', getKeyByValue(commands.powerSave.value, deviceModel.props[commands.powerSave.code]).toString(), '/powersave/get')
//client.publish(mqttTopicPrefix + '/powersave/get', commands.energySave.value.getKeyByValue(deviceModel.props[commands.energySave.code]).toString());
publishIfChanged('lights', getKeyByValue(commands.lights.value, deviceModel.props[commands.lights.code]).toString(), '/lights/get');
//client.publish(mqttTopicPrefix + '/lights/get', commands.lights.value.getKeyByValue(deviceModel.props[commands.lights.code]).toString());
publishIfChanged('sleep', getKeyByValue(commands.sleep.value, deviceModel.props[commands.sleep.code]).toString(), '/sleep/get');
//client.publish(mqttTopicPrefix + '/sleep/get', commands.sleep.value.getKeyByValue(deviceModel.props[commands.sleep.code]).toString());
/**
* Handle "off" mode status
* Hass.io MQTT climate control doesn't support power commands through GUI,
* so an additional pseudo mode is added
*/
const extendedMode = (deviceModel.props[commands.power.code] === commands.power.value.on) ?
getKeyByValue(commands.mode.value, deviceModel.props[commands.mode.code]).toString() :
'off';
publishIfChanged('mode', extendedMode, '/mode/get');
},
onUpdate: (deviceModel) => {
console.log('[UDP] Status updated on %s', deviceModel.name);
},
onConnected: (deviceModel) => {
client.subscribe(mqttTopicPrefix + '/temperature/set');
client.subscribe(mqttTopicPrefix + '/mode/set');
client.subscribe(mqttTopicPrefix + '/fanspeed/set');
client.subscribe(mqttTopicPrefix + '/swinghor/set');
client.subscribe(mqttTopicPrefix + '/swingvert/set');
client.subscribe(mqttTopicPrefix + '/power/set');
client.subscribe(mqttTopicPrefix + '/health/set');
client.subscribe(mqttTopicPrefix + '/powersave/set');
client.subscribe(mqttTopicPrefix + '/lights/set');
client.subscribe(mqttTopicPrefix + '/quiet/set');
client.subscribe(mqttTopicPrefix + '/blow/set');
client.subscribe(mqttTopicPrefix + '/air/set');
client.subscribe(mqttTopicPrefix + '/sleep/set');
client.subscribe(mqttTopicPrefix + '/turbo/set');
}
};
let hvac;
/**
* Connect to MQTT broker
*/
const mqttOptions = {};
let authLog = '';
if (argv['mqtt-username'] && argv['mqtt-password']) {
mqttOptions.username = argv['mqtt-username'];
mqttOptions.password = argv['mqtt-password'];
authLog = ' as "' + mqttOptions.username + '"';
}
let interval = 60;
if (argv['interval']) {
interval = argv['interval'];
}
var name = "";
if (argv['name']) {
name = argv['name'];
}
console.log("brocker: " + argv['mqtt-broker-url']);
const client = mqtt.connect(argv['mqtt-broker-url'], mqttOptions);
client.on('connect', () => {
console.log('[MQTT] Connected to broker on ' + argv['mqtt-broker-url'] + authLog);
hvac = require('./app/deviceFactory').connect(deviceOptions);
//TODO: auto discoverty
sendDiscoveryMessage(name);
setInterval(x => {
console.log("--Get status from AC ");
hvac.requestDeviceStatus();
}, interval * 1000);
});
function sendDiscoveryMessage(name = "LivingRoom AC") {
let uniqId = "ChAC" + deviceOptions.host.replace(/\D/g, '');
let discoveryObj = {
"name": name,
"mode_cmd_t": mqttTopicPrefix + "/mode/set",
"mode_stat_t": mqttTopicPrefix + "/mode/get",
"curr_temp_t": mqttTopicPrefix + "/temperature_in/get",
"temp_cmd_t": mqttTopicPrefix + "/temperature/set",
"temp_stat_t": mqttTopicPrefix + "/temperature/get",
"fan_mode_command_topic": mqttTopicPrefix + "/fanspeed/set",
"fan_mode_state_topic": mqttTopicPrefix + "/fanspeed/get",
"fan_modes": ["l0", "l1", "l2", "l3", "l4", "l5", "l6"],
"uniq_id": uniqId,
"pl_on": "on",
"pl_off": "off",
"swing_mode_state_topic": mqttTopicPrefix + "/swingvert/get",
"swing_mode_command_topic": mqttTopicPrefix + "/swingvert/set",
"power_state_topic": mqttTopicPrefix + "/power/get",
"power_command_topic": mqttTopicPrefix + "/power/set",
"device": {
"name": "AC Livingroom",
"manufacturer": "Cooper&Hunter",
"sw_version": "v1.3",
"via_device": "mqtt",
"model": "Nordic Evo 2",
"identifiers": [
"Ch-s09ftn-e2wf"
]
},
"modes": ["off", "heat", "auto", "cool", "dry"]
};
var mqttTopic = "/config";
client.publish(mqttTopicPrefix + mqttTopic, JSON.stringify(discoveryObj));
}
client.on('message', (topic, message) => {
message = message.toString();
console.log('[MQTT] Message "%s" received for %s', message, topic);
switch (topic) {
case mqttTopicPrefix + '/temperature/set':
hvac.setTemp(parseInt(message));
return;
case mqttTopicPrefix + '/mode/set':
if (message === 'off' || message === 'none') {
// Power off when "off" mode
hvac.setPower(commands.power.value.off);
} else {
// Power on and set mode if other than 'off'
if (hvac.device.props[commands.power.code] === commands.power.value.off) {
hvac.setPower(commands.power.value.on);
}
hvac.setMode(commands.mode.value[message]);
}
return;
case mqttTopicPrefix + '/fanspeed/set':
hvac.setFanSpeed(commands.fanSpeed.value[message]);
return;
case mqttTopicPrefix + '/swinghor/set':
hvac.setSwingHor(commands.swingHor.value[message]);
return;
case mqttTopicPrefix + '/swingvert/set':
hvac.setSwingVert(commands.swingVert.value[message]);
return;
case mqttTopicPrefix + '/power/set':
hvac.setPower(parseInt(commands.power.value[message]));
console.log("mqttTopicPrefix set power:" + message);
return;
case mqttTopicPrefix + '/health/set':
hvac.setHealthMode(parseInt(message));
return;
case mqttTopicPrefix + '/powersave/set':
hvac.setPowerSave(parseInt(message));
return;
case mqttTopicPrefix + '/lights/set':
hvac.setLights(parseInt(message));
return;
case mqttTopicPrefix + '/quiet/set':
hvac.setQuietMode(parseInt(message));
return;
case mqttTopicPrefix + '/blow/set':
hvac.setBlow(parseInt(message));
return;
case mqttTopicPrefix + '/air/set':
hvac.setAir(parseInt(message));
return;
case mqttTopicPrefix + '/sleep/set':
hvac.setSleepMode(parseInt(message));
return;
case mqttTopicPrefix + '/turbo/set':
hvac.setTurbo(parseInt(message));
return;
}
console.log('[MQTT] No handler for topic %s', topic);
});