forked from MiguelAngelLV/esphaier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHaier.h
325 lines (211 loc) · 7.14 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
/**
* 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 00
#define MODE_COOL 01
#define MODE_HEAT 02
#define MODE_ONLY_FAN 03
#define MODE_DRY 04
#define FAN_SPEED 25
#define FAN_MIN 02
#define FAN_MIDDLE 01
#define FAN_MAX 00
#define FAN_AUTO 03
#define SWING 27
#define SWING_OFF 00
#define SWING_VERTICAL 01
#define SWING_HORIZONTAL 02
#define SWING_BOTH
#define LOCK 28
#define LOCK_ON 80
#define LOCK_OFF 00
#define POWER 29
#define POWER_ON 9
#define POWER_OFF 8
#define FRESH 31
#define FRESH_ON 01
#define FRESH_OFF 00
#define SET_TEMPERATURE 35
#define CRC 36
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_away(false);
traits.set_supports_dry_mode(true);
traits.set_supports_heat_mode(true);
traits.set_supports_cool_mode(true);
traits.set_supports_fan_only_mode(true);
traits.set_supports_heat_cool_mode(true);
traits.set_supports_fan_mode_low(true);
traits.set_supports_fan_mode_auto(true);
traits.set_supports_fan_mode_high(true);
traits.set_supports_fan_mode_medium(true);
traits.set_visual_max_temperature(16);
traits.set_visual_max_temperature(30);
traits.set_visual_temperature_step(1.0f);
traits.set_supports_current_temperature(true);
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_LOGD("Haier", "Invalid checksum");
return;
}
lastCRC = check;
current_temperature = data[TEMPERATURE];
target_temperature = data[SET_TEMPERATURE] + 16;
if (data[POWER] == 0 || data[POWER] == 8 ) {
mode = CLIMATE_MODE_OFF;
} else {
switch (data[MODE]) {
case MODE_COOL:
mode = CLIMATE_MODE_COOL;
break;
case MODE_HEAT:
mode = CLIMATE_MODE_HEAT;
break;
case MODE_ONLY_FAN:
mode = CLIMATE_MODE_FAN_ONLY;
break;
case MODE_DRY:
mode = CLIMATE_MODE_DRY;
break;
default:
mode = CLIMATE_MODE_HEAT_COOL;
}
switch(data[FAN_SPEED]) {
case FAN_AUTO:
fan_mode = CLIMATE_FAN_AUTO;
break;
case FAN_MIN:
fan_mode = CLIMATE_FAN_LOW;
break;
case FAN_MIDDLE:
fan_mode = CLIMATE_FAN_MEDIUM;
break;
case FAN_MAX:
fan_mode = CLIMATE_FAN_HIGH;
break;
}
}
this->publish_state();
}
void control(const ClimateCall &call) override {
if (call.get_mode().has_value())
switch (call.get_mode().value()) {
case CLIMATE_MODE_OFF:
data[POWER] = POWER_OFF;
break;
case CLIMATE_MODE_HEAT_COOL:
case CLIMATE_MODE_AUTO:
data[POWER] = POWER_ON;
data[MODE] = MODE_SMART;
break;
case CLIMATE_MODE_HEAT:
data[POWER] = POWER_ON;
data[MODE] = MODE_HEAT;
break;
case CLIMATE_MODE_COOL:
data[POWER] = POWER_ON;
data[MODE] = MODE_COOL;
break;
case CLIMATE_MODE_FAN_ONLY:
data[POWER] = POWER_ON;
data[MODE] = MODE_ONLY_FAN;
break;
case CLIMATE_MODE_DRY:
data[POWER] = POWER_ON;
data[MODE] = MODE_DRY;
break;
}
if (call.get_target_temperature().has_value()) {
data[SET_TEMPERATURE] = (uint16) call.get_target_temperature().value() - 16;
}
if (call.get_fan_mode().has_value()) {
switch(call.get_fan_mode().value()) {
case CLIMATE_FAN_AUTO:
data[FAN_SPEED] = FAN_AUTO;
break;
case CLIMATE_FAN_LOW:
data[FAN_SPEED] = FAN_MIN;
break;
case CLIMATE_FAN_MEDIUM:
data[FAN_SPEED] = FAN_MIDDLE;
break;
case CLIMATE_FAN_HIGH:
data[FAN_SPEED] = FAN_MAX;
break;
}
}
//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