-
Notifications
You must be signed in to change notification settings - Fork 14
/
mipurifier.h
241 lines (222 loc) · 7.43 KB
/
mipurifier.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
#include "esphome.h"
class MiPurifier : public Component, public UARTDevice, public CustomAPIDevice {
public:
static const int max_line_length = 80;
char recv_buffer[max_line_length];
char send_buffer[max_line_length];
bool is_preset;
int last_heartbeat, last_query;
Sensor *airquality_sensor = new Sensor();
Sensor *humidity_sensor = new Sensor();
Sensor *temperature_sensor = new Sensor();
Sensor *filterlife_sensor = new Sensor();
MiPurifier(UARTComponent *uart) : UARTDevice(uart) {}
int readline(int readch, char *buffer, int len) {
static int pos = 0;
int rpos;
if (readch > 0) {
switch (readch) {
case '\r': // Return on CR
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
default:
if (pos < len-1) {
buffer[pos++] = readch;
buffer[pos] = 0;
}
}
}
// No end of line has been found, so return -1.
return -1;
}
// only run setup() after a Wi-Fi connection has been established successfully
float get_setup_priority() const override { return esphome::setup_priority::AFTER_WIFI; }
void turn_on() {
strcpy(send_buffer, "down set_properties 2 2 true");
}
void turn_off() {
strcpy(send_buffer, "down set_properties 2 2 false");
}
void enable_beeper() {
strcpy(send_buffer, "down set_properties 5 1 true");
}
void disable_beeper() {
strcpy(send_buffer, "down set_properties 5 1 false");
}
void lock() {
strcpy(send_buffer, "down set_properties 7 1 true");
}
void unlock() {
strcpy(send_buffer, "down set_properties 7 1 false");
}
void set_mode(std::string mode) {
// 0: auto, 1: sleep, 2: manual, 3: low, 4: med, 5: high
if (mode == "auto") {
strcpy(send_buffer, "down set_properties 2 5 0");
} else if (mode == "night") {
strcpy(send_buffer, "down set_properties 2 5 1");
} else if (mode == "manual") {
strcpy(send_buffer, "down set_properties 2 5 2");
} else if (mode == "low") {
strcpy(send_buffer, "down set_properties 2 4 1");
} else if (mode == "medium") {
strcpy(send_buffer, "down set_properties 2 4 2");
} else if (mode == "high") {
strcpy(send_buffer, "down set_properties 2 4 3");
}
}
void set_brightness(std::string brightness) {
if (brightness == "off") {
strcpy(send_buffer, "down set_properties 6 1 2");
} else if (brightness == "low") {
strcpy(send_buffer, "down set_properties 6 1 1");
} else if (brightness == "high") {
strcpy(send_buffer, "down set_properties 6 1 0");
}
}
void set_manualspeed(int speed) {
snprintf(send_buffer, max_line_length, "down set_properties 10 10 %i", speed);
}
void send_command(std::string s) {
strcpy(send_buffer, s.c_str());
}
void update_property(char* id, char* val) {
if (strcmp(id, "36") == 0) {
airquality_sensor->publish_state(atof(val));
} else if (strcmp(id, "37") == 0) {
humidity_sensor->publish_state(atof(val));
} else if (strcmp(id, "38") == 0) {
temperature_sensor->publish_state(atof(val));
} else if (strcmp(id, "43") == 0) {
filterlife_sensor->publish_state(atof(val));
} else if (strcmp(id, "22") == 0) {
// power (on, off)
if (strcmp(val, "true") == 0) {
auto call = XiaomiPurifierFan->turn_on();
call.perform();
} else {
auto call = XiaomiPurifierFan->turn_off();
call.perform();
}
} else if (strcmp(id, "25") == 0) {
// mode (auto, night, manual, preset)
is_preset = false;
auto call = XiaomiPurifierFan->make_call();
switch (atoi(val)) {
case 0:
call.set_preset_mode("auto");
break;
case 1:
call.set_preset_mode("night");
break;
case 2:
call.set_preset_mode("manual");
break;
case 3:
is_preset = true;
break;
}
call.perform();
} else if (strcmp(id, "24") == 0) {
// preset (low, medium, high)
if (is_preset) {
auto call = XiaomiPurifierFan->make_call();
switch (atoi(val)) {
case 1:
call.set_preset_mode("low");
break;
case 2:
call.set_preset_mode("medium");
break;
case 3:
call.set_preset_mode("high");
break;
}
call.perform();
}
} else if (strcmp(id, "51") == 0) {
// beeper (on, off)
beeper_switch->publish_state(strcmp(val, "true") == 0);
} else if (strcmp(id, "71") == 0) {
// lock (on, off)
lock_switch->publish_state(strcmp(val, "true") == 0);
} else if (strcmp(id, "61") == 0) {
// display brightness (off, low, high)
switch (atoi(val)) {
case 0:
brightness_select->publish_state("high");
break;
case 1:
brightness_select->publish_state("low");
break;
case 2:
brightness_select->publish_state("off");
break;
}
} else if (strcmp(id, "1010") == 0) {
// manual speed
XiaomiPurifierFan->speed = atof(val)+1;
}
}
void setup() override {
register_service(&MiPurifier::send_command, "send_command", {"command"});
// get initial state & settings
strcpy(send_buffer, "down get_properties 2 2 2 5 5 1 6 1 7 1 10 10");
}
void loop() override {
while (available()) {
if(readline(read(), recv_buffer, max_line_length) > 0) {
char *cmd = strtok(recv_buffer, " ");
if (strcmp(cmd, "net") == 0) {
write_str("local");
} else if (strcmp(cmd, "time") == 0) {
write_str("0");
} else if (strcmp(cmd, "get_down") == 0) {
// send command from send_buffer
if (strlen(send_buffer) > 0) {
write_str(send_buffer);
send_buffer[0] = '\0';
ESP_LOGD("mipurifier", "sent send_buffer");
} else if (millis() - last_heartbeat > 60000) {
// send mysterious heartbeat message
write_str("down set_properties 13 9 60");
last_heartbeat = millis();
ESP_LOGD("purifier", "sent heartbeat");
} else if (millis() - last_query > 60000) {
// force sensor update
write_str("down get_properties 3 6 3 7 3 8 4 3");
last_query = millis();
ESP_LOGD("purifier", "sent query string");
} else {
write_str("down none");
}
} else if (strcmp(cmd, "properties_changed") == 0) {
ESP_LOGD("mipurifier", "parsing properties_changed message");
char *id1 = strtok(NULL, " ");
char *id2 = strtok(NULL, " ");
char *id = strcat(id1, id2);
char *val = strtok(NULL, " ");
update_property(id, val);
write_str("ok");
} else if (strcmp(cmd, "result") == 0) {
// loop over all properties and update
ESP_LOGD("mipurifier", "parsing result message");
char *id1, *id2, *id, *val;
while (true) {
if (!(id1 = strtok(NULL, " "))) break;
if (!(id2 = strtok(NULL, " "))) break;
id = strcat(id1, id2);
strtok(NULL, " "); // skip 0
if (!(val = strtok(NULL, " "))) break;
update_property(id, val);
}
write_str("ok");
} else {
// just acknowledge any other message
write_str("ok");
}
}
}
}
};