-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHaier.h
415 lines (318 loc) · 11.8 KB
/
Haier.h
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
/**
* Create by Miguel Ángel López on 20/07/19
**/
#ifndef HAIER_ESP_HAIER_H
#define HAIER_ESP_HAIER_H
#include "esphome.h"
using namespace esphome;
using namespace esphome::climate;
#define TEMPERATURE 13
#define COMMAND 17
#define MODE 23
#define MODE_SMART 0
#define MODE_COOL 1
#define MODE_HEAT 2
#define MODE_FAN_ONLY 3
#define MODE_DRY 4
#define FAN_SPEED 25
#define FAN_HIGH 0
#define FAN_MIDDLE 1
#define FAN_MEDIUM 2
#define FAN_AUTO 3
#define SWING 27
#define SWING_OFF 0
#define SWING_BOTH 1
#define LOCK 28
#define LOCK_ON 80
#define LOCK_OFF 00
#define POWER 29
// bit position >>
#define POWER_BIT_ON 0
#define HEALTH_MODE_BIT_ON 3
#define COMPRESSOR_MODE_BIT_ON 4
#define SWING_POS 31
// bit position >>
#define SWING_UNDEFINED_BIT 0
#define TURBO_MODE_BIT_ON 1
#define SILENT_MODE_BIT_ON 2
#define SWING_HORIZONTAL_BIT 3
#define SWING_VERTICAL_BIT 4
#define LED_BIT_OFF 5
#define SET_TEMPERATURE 35
#define CRC 36
// temperatures supported by AC system
#define MIN_SET_TEMPERATURE 16
#define MAX_SET_TEMPERATURE 30
#define STEP_TEMPERATURE 1
#define TEMPERATURE_FOR_FAN_ONLY 8
//if internal temperature is outside of those boundaries, message will be discarded
#define MIN_VALID_INTERNAL_TEMP 10
#define MAX_VALID_INTERNAL_TEMP 50
class Haier : public Climate, public PollingComponent {
private:
byte lastCRC;
byte data[37];
byte poll[13] = {255,255,10,0,0,0,0,0,1,1,77,1,90};
byte on[13] = {255,255,10,0,0,0,0,0,1,1,77,2,91};
public:
Haier() : PollingComponent(5 * 1000) {
lastCRC = 0;
}
void setup() override {
Serial.begin(9600);
}
void loop() override {
if (Serial.available() > 0) {
if (Serial.read() != 255) return;
if (Serial.read() != 255) return;
data[0] = 255;
data[1] = 255;
Serial.readBytes(data+2, sizeof(data)-2);
readData();
}
}
void update() override {
Serial.write(poll, sizeof(poll));
auto raw = getHex(poll, sizeof(poll));
ESP_LOGD("Haier", "POLL: %s ", raw.c_str());
}
protected:
ClimateTraits traits() override {
auto traits = climate::ClimateTraits();
traits.set_supports_action(true);
traits.set_supported_modes(
{
climate::CLIMATE_MODE_OFF,
climate::CLIMATE_MODE_HEAT_COOL,
climate::CLIMATE_MODE_COOL,
climate::CLIMATE_MODE_HEAT,
climate::CLIMATE_MODE_FAN_ONLY,
climate::CLIMATE_MODE_DRY
});
traits.set_supported_fan_modes(
{
climate::CLIMATE_FAN_AUTO,
climate::CLIMATE_FAN_LOW,
climate::CLIMATE_FAN_MEDIUM,
climate::CLIMATE_FAN_HIGH,
climate::CLIMATE_FAN_MIDDLE,
});
traits.set_supported_swing_modes(
{
climate::CLIMATE_SWING_OFF,
climate::CLIMATE_SWING_BOTH,
climate::CLIMATE_SWING_VERTICAL,
climate::CLIMATE_SWING_HORIZONTAL
});
traits.set_visual_min_temperature(MIN_SET_TEMPERATURE);
traits.set_visual_max_temperature(MAX_SET_TEMPERATURE);
traits.set_visual_temperature_step(STEP_TEMPERATURE);
traits.set_supports_current_temperature(true);
traits.set_supports_two_point_target_temperature(false);
return traits;
}
public:
void readData() {
auto raw = getHex(data, sizeof(data));
ESP_LOGD("Haier", "Readed message: %s ", raw.c_str());
byte check = data[CRC];
getChecksum(data, sizeof(data));
if (check != data[CRC]) {
ESP_LOGW("Haier", "Invalid checksum");
return;
}
lastCRC = check;
current_temperature = data[TEMPERATURE];
target_temperature = data[SET_TEMPERATURE] + 16;
if(current_temperature < MIN_VALID_INTERNAL_TEMP || current_temperature > MAX_VALID_INTERNAL_TEMP
|| target_temperature < MIN_SET_TEMPERATURE || target_temperature > MAX_SET_TEMPERATURE){
ESP_LOGW("Haier", "Invalid temperatures");
return;
}
if (data[POWER] & ( 1 << POWER_BIT_ON )) {
switch (data[MODE]) {
case MODE_COOL:
mode = CLIMATE_MODE_COOL;
break;
case MODE_HEAT:
mode = CLIMATE_MODE_HEAT;
break;
case MODE_DRY:
mode = CLIMATE_MODE_DRY;
break;
case MODE_FAN_ONLY:
mode = CLIMATE_MODE_FAN_ONLY;
break;
default:
mode = CLIMATE_MODE_HEAT_COOL;
}
if ( data[SWING_POS] & ( 1 << SILENT_MODE_BIT_ON )) {
fan_mode = CLIMATE_FAN_LOW;
} else {
switch (data[FAN_SPEED]) {
case FAN_AUTO:
fan_mode = CLIMATE_FAN_AUTO;
break;
case FAN_MEDIUM:
fan_mode = CLIMATE_FAN_MEDIUM;
break;
case FAN_MIDDLE:
fan_mode = CLIMATE_FAN_MIDDLE;
break;
case FAN_HIGH:
fan_mode = CLIMATE_FAN_HIGH;
break;
default:
fan_mode = CLIMATE_FAN_AUTO;
}
}
switch (data[SWING]) {
case SWING_OFF:
if ( data[SWING_POS] & ( 1 << SWING_VERTICAL_BIT )) {
swing_mode = CLIMATE_SWING_VERTICAL;
} else if ( data[SWING_POS] & ( 1 << SWING_HORIZONTAL_BIT )) {
swing_mode = CLIMATE_SWING_HORIZONTAL;
} else {
swing_mode = CLIMATE_SWING_OFF;
}
break;
case SWING_BOTH:
swing_mode = CLIMATE_SWING_BOTH;
break;
}
} else {
mode = CLIMATE_MODE_OFF;
//fan_mode = CLIMATE_FAN_OFF;
//swing_mode = CLIMATE_SWING_OFF;
}
this->publish_state();
}
// Climate control
void control(const ClimateCall &call) override {
if (call.get_mode().has_value())
switch (call.get_mode().value()) {
case CLIMATE_MODE_OFF:
data[POWER] &= ~(1 << POWER_BIT_ON);
break;
case CLIMATE_MODE_COOL:
data[POWER] |= (1 << POWER_BIT_ON);
data[MODE] = MODE_COOL;
break;
case CLIMATE_MODE_HEAT:
data[POWER] |= (1 << POWER_BIT_ON);
data[MODE] = MODE_HEAT;
break;
case CLIMATE_MODE_DRY:
data[POWER] |= (1 << POWER_BIT_ON);
data[MODE] = MODE_DRY;
break;
case CLIMATE_MODE_FAN_ONLY:
if (data[POWER] & ( 1 << POWER_BIT_ON )) {
//ESP_LOGW("Haier", "Cold start");
} else {
data[SET_TEMPERATURE] = TEMPERATURE_FOR_FAN_ONLY;
}
data[POWER] |= (1 << POWER_BIT_ON);
data[FAN_SPEED] = FAN_MIDDLE; // set fan speed for change work mode
data[MODE] = MODE_FAN_ONLY;
break;
case CLIMATE_MODE_HEAT_COOL:
data[POWER] |= (1 << POWER_BIT_ON);
data[MODE] = MODE_SMART;
break;
}
//Set fan speed
if (call.get_fan_mode().has_value())
switch(call.get_fan_mode().value()) {
case CLIMATE_FAN_LOW:
data[POWER] |= (1 << POWER_BIT_ON);
data[SWING_POS] |= (1 << SILENT_MODE_BIT_ON);
data[SWING_POS] &= ~(1 << TURBO_MODE_BIT_ON);
break;
case CLIMATE_FAN_MIDDLE:
data[POWER] |= (1 << POWER_BIT_ON);
data[FAN_SPEED] = FAN_MIDDLE;
data[SWING_POS] &= ~(1 << SILENT_MODE_BIT_ON);
data[SWING_POS] &= ~(1 << TURBO_MODE_BIT_ON);
break;
case CLIMATE_FAN_MEDIUM:
data[POWER] |= (1 << POWER_BIT_ON);
data[FAN_SPEED] = FAN_MEDIUM;
data[SWING_POS] &= ~(1 << SILENT_MODE_BIT_ON);
data[SWING_POS] &= ~(1 << TURBO_MODE_BIT_ON);
break;
case CLIMATE_FAN_HIGH:
data[POWER] |= (1 << POWER_BIT_ON);
data[FAN_SPEED] = FAN_HIGH;
data[SWING_POS] &= ~(1 << SILENT_MODE_BIT_ON);
data[SWING_POS] &= ~(1 << TURBO_MODE_BIT_ON);
break;
case CLIMATE_FAN_AUTO:
data[POWER] |= (1 << POWER_BIT_ON);
data[FAN_SPEED] = FAN_AUTO;
data[SWING_POS] &= ~(1 << SILENT_MODE_BIT_ON);
data[SWING_POS] &= ~(1 << TURBO_MODE_BIT_ON);
break;
}
//Set swing mode
if (call.get_swing_mode().has_value())
switch(call.get_swing_mode().value()) {
case CLIMATE_SWING_OFF:
data[POWER] |= (1 << POWER_BIT_ON);
data[SWING] = SWING_OFF;
data[SWING_POS] &= ~(1 << SWING_UNDEFINED_BIT);
break;
case CLIMATE_SWING_VERTICAL:
data[POWER] |= (1 << POWER_BIT_ON);
data[SWING] = SWING_OFF;
data[SWING_POS] |= (1 << SWING_VERTICAL_BIT);
data[SWING_POS] &= ~(1 << SWING_HORIZONTAL_BIT);
break;
case CLIMATE_SWING_HORIZONTAL:
data[POWER] |= (1 << POWER_BIT_ON);
data[SWING] = SWING_OFF;
data[SWING_POS] |= (1 << SWING_HORIZONTAL_BIT);
data[SWING_POS] &= ~(1 << SWING_VERTICAL_BIT);
break;
case CLIMATE_SWING_BOTH:
data[POWER] |= (1 << POWER_BIT_ON);
data[SWING] = SWING_BOTH;
data[SWING_POS] &= ~(1 << SWING_UNDEFINED_BIT);
data[SWING_POS] &= ~(1 << SWING_VERTICAL_BIT);
data[SWING_POS] &= ~(1 << SWING_HORIZONTAL_BIT);
break;
}
if (call.get_target_temperature().has_value()) {
data[SET_TEMPERATURE] = (uint16) call.get_target_temperature().value() - 16;
}
//Values for "send"
data[COMMAND] = 0;
data[9] = 1;
data[10] = 77;
data[11] = 95;
sendData(data, sizeof(data));
}
void sendData(byte * message, byte size) {
byte crc = getChecksum(message, size);
Serial.write(message, size-1);
Serial.write(crc);
auto raw = getHex(message, size);
ESP_LOGD("Haier", "Sended message: %s ", raw.c_str());
}
String getHex(byte * message, byte size) {
String raw;
for (int i=0; i < size; i++){
raw += "\n" + String(i) + "-" + String(message[i]);
}
raw.toUpperCase();
return raw;
}
byte getChecksum(const byte * message, size_t size) {
byte position = size - 1;
byte crc = 0;
for (int i = 2; i < position; i++)
crc += message[i];
return crc;
}
};
#endif //HAIER_ESP_HAIER_H