forked from Pittini/iobroker-nodemihome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirpurifier3H-V0.1.4.js
284 lines (238 loc) · 12.9 KB
/
Airpurifier3H-V0.1.4.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
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
const SkriptVersion = "0.1.4"; //vom 5.12.2020 / Link zu Git: https://github.com/Pittini/iobroker-nodemihome / Forum: https://forum.iobroker.net/topic/39388/vorlage-xiaomi-airpurifier-3h-u-a-inkl-token-auslesen
const mihome = require('node-mihome');
const username = '';
const password = '';
const options = { country: 'de' }; // 'ru', 'us', 'tw', 'sg', 'cn', 'de' (Default: 'cn');
const refresh = 10000;
const praefix0 = "javascript.0.MiHome.";
const logging = true;
const WriteAllXiaomis = true;
//Ab hier nix mehr ändern!
const DeviceData = [];
let AllDevicesRaw = [];
let device;
const States = [];
let DpCount = 0;
let TriggerLock = true;
log("Starting MiHome-Airpurifier3H-V" + SkriptVersion);
const DeviceGets = ["Power", "Mode", "FanLevel", "Buzzer", "LcdBrightness", "Temperature", "Humidity", "PM2_5", "FilterRemaining"]
const DeviceSets = ["Power", "Mode", "FanLevel", "Buzzer", "LcdBrightness", "ChildLock"]
Init();
function PrepareDeviceDps(did) {
//DeviceDps
if (did == 317335021) { //Purifier 3H
States[DpCount] = { id: praefix0 + did + ".Power", initial: false, forceCreation: false, common: { read: true, write: true, name: "Power", type: "boolean", role: "switch.power", def: false } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".Mode", initial: "", forceCreation: false, common: { read: true, write: true, name: "Mode", type: "string", role: "value", states: { "none": "=Fanlevel", "auto": "auto", "sleep": "sleep", "favorite": "favorite" }, def: "" } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".FanLevel", initial: 1, forceCreation: false, common: { read: true, write: true, name: "FanLevel", type: "number", role: "level.fan", def: 1, min: 1, max: 3 } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".Temperature", initial: 0, forceCreation: false, common: { read: true, write: false, name: "Temperature", type: "number", unit: "°C", role: "value.temperature", def: 0 } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".Humidity", initial: 0, forceCreation: false, common: { read: true, write: false, name: "Humidity", type: "number", unit: "%", role: "value.humidity", def: 0 } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".PM2_5", initial: 0, forceCreation: false, common: { read: true, write: false, name: "PM 2.5", type: "number", unit: "", role: "value", def: 0 } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".FilterRemaining", initial: 0, forceCreation: false, common: { read: true, write: false, name: "Filter Remaining", type: "number", unit: "days", role: "value", def: 0 } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".Buzzer", initial: false, forceCreation: false, common: { read: true, write: true, name: "Buzzer", type: "boolean", role: "switch", def: false } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".LcdBrightness", initial: 1, forceCreation: false, common: { read: true, write: true, name: "LcdBrightness", type: "number", role: "level.brightness", def: 1, min: 0, max: 2 } }; // 0-brightest, 1-glimmer, 2-led_closed
DpCount++;
States[DpCount] = { id: praefix0 + did + ".ChildLock", initial: false, forceCreation: false, common: { read: true, write: true, name: "ChildLock", type: "boolean", role: "switch", def: false } }; //
}
}
function PrepareGenericDps(did, name) {
//StaticDps
States[DpCount] = { id: praefix0 + did + ".Info.IpAdress", initial: "", forceCreation: false, common: { read: true, write: true, name: "Ip Adress", type: "string", role: "value", def: "" } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".Info.Token", initial: "", forceCreation: false, common: { read: true, write: true, name: "Token", type: "string", role: "value", def: "" } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".Info.DeviceId", initial: "", forceCreation: false, common: { read: true, write: true, name: "Device Id", type: "string", role: "value", def: "" } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".Info.Model", initial: "", forceCreation: false, common: { read: true, write: true, name: "Model", type: "string", role: "value", def: "" } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".Info.Rssi", initial: 0, forceCreation: false, common: { read: true, write: false, name: "rssi", type: "number", role: "value.rssi", def: 0 } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".Info.Name", initial: "", forceCreation: false, common: { read: true, write: true, name: "Name", type: "string", role: "value", def: "" } }; //
DpCount++;
States[DpCount] = { id: praefix0 + did + ".Info.IsOnline", initial: false, forceCreation: false, common: { read: true, write: true, name: "Is online", type: "boolean", role: "value", def: false } }; //
DpCount++;
}
function CreateStates() {
//Alle States anlegen, Main aufrufen wenn fertig
let numStates = States.length;
States.forEach(function (state) {
createState(state.id, state.initial, state.forceCreation, state.common, function () {
numStates--;
if (numStates === 0) {
if (logging) log("CreateStates fertig!");
main();
};
});
});
}
function main() {
if (logging) log("Reaching main");
WriteGenericDpValues();
CreateDpTrigger();
}
function WriteGenericDpValues() { //Alle vorhandenen generischen Werte einlesen und in Dps schreiben
for (let x = 0; x < AllDevicesRaw.length; x++) { //Alle vorhandenen Xiaomi Devices durchgehen
setState(praefix0 + AllDevicesRaw[x].did + ".Info.IpAdress", AllDevicesRaw[x].localip);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.Token", AllDevicesRaw[x].token);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.DeviceId", AllDevicesRaw[x].did);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.Model", AllDevicesRaw[x].model);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.Rssi", AllDevicesRaw[x].rssi);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.Name", AllDevicesRaw[x].name);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.IsOnline", AllDevicesRaw[x].isOnline);
log(AllDevicesRaw[x])
};
}
function WriteGenericDpValues() { //Alle vorhandenen generischen Werte einlesen und in Dps schreiben
for (let x = 0; x < AllDevicesRaw.length; x++) { //Alle vorhandenen Xiaomi Devices durchgehen
setState(praefix0 + AllDevicesRaw[x].did + ".Info.IpAdress", AllDevicesRaw[x].localip);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.Token", AllDevicesRaw[x].token);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.DeviceId", AllDevicesRaw[x].did);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.Model", AllDevicesRaw[x].model);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.Rssi", AllDevicesRaw[x].rssi);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.Name", AllDevicesRaw[x].name);
setState(praefix0 + AllDevicesRaw[x].did + ".Info.IsOnline", AllDevicesRaw[x].isOnline);
if (AllDevicesRaw[x].did == 317335021) Create_device(AllDevicesRaw[x].did, AllDevicesRaw[x].model, AllDevicesRaw[x].localip, AllDevicesRaw[x].token);
log(AllDevicesRaw[x])
};
}
async function Init() { //Cloudlogin und auslesen der gesamten Clouddaten
if (logging) log("Reaching init");
// local miIO
mihome.miioProtocol.init();
try {// cloud MIoT
await mihome.miCloudProtocol.login(username, password); //Versuch einzuloggen
}
catch {
log("You are already logged in, login canceled"); //Wenn schon eingeloggt
}
AllDevicesRaw = await mihome.miCloudProtocol.getDevices(null, options); //Gibt alle vorhandenen Devices zurück
//if (logging) console.warn(await mihome.miCloudProtocol.getDevice([317335021], options)); // get devices information from list ids
for (let x = 0; x < AllDevicesRaw.length; x++) { //Alle vorhandenen Xiaomi Devices durchgehen
if (WriteAllXiaomis) {
PrepareGenericDps(AllDevicesRaw[x].did, AllDevicesRaw[x].name); //Allgemeine generische Infos Dps vorbereiten
}
};
PrepareDeviceDps(317335021); //Devicespezifische DPs vorbereiten
CreateStates();
}
async function Create_device(did, model, adress, token) {
if (logging) log("Reaching CreateDevice did=" + did + " model=" + model + " adress=" + adress + " token=" + token);
device = mihome.device({
id: did, // required, device id
model: model, // required, device model "zhimi.airpurifier.mb3"
address: adress, // miio-device option, local ip address
token: token, // miio-device option, device token 4ff8a96292d0451c5148142a0a851e4f
refresh: 10000 // miio-device option, device properties refresh interval in ms
});
device.on('properties', (data) => {
device.Power = device.getPower(); // liefert ein bestimmtes Attribut
device.Mode = device.getMode(); // liefert ein bestimmtes Attribut - auto/sleep/none
device.FanLevel = device.getFanLevel(); // liefert ein bestimmtes Attribut
device.Temperature = device.getTemperature(); // liefert ein bestimmtes Attribut
device.Humidity = device.getHumidity(); // liefert ein bestimmtes Attribut
device.PM2_5 = device.getPM2_5(); // liefert ein bestimmtes Attribut
device.FilterRemaining = device.getFilterRemaining(); // liefert ein bestimmtes Attribut
device.Buzzer = device.getBuzzer(); // liefert ein bestimmtes Attribut
device.LcdBrightness = device.getLcdBrightness(); // liefert ein bestimmtes Attribut
RefreshDps(did);
});
await device.init(); // connect to device and poll for properties
onStop(function () { //Bei Scriptende Device löschen
device.destroy();
unsubscribe('properties');
}, 10);
}
function RefreshDps(did) {
if (logging) log("Reaching RefreshDps ");
TriggerLock = true;
if (device.Power != DeviceData[0]) {
DeviceData[0] = device.Power;
setState(praefix0 + did + "." + DeviceGets[0], DeviceData[0]);
}
if (device.Mode != DeviceData[1]) {
DeviceData[1] = device.Mode;
setState(praefix0 + did + "." + DeviceGets[1], DeviceData[1]);
}
if (device.FanLevel != DeviceData[2]) {
DeviceData[2] = device.FanLevel;
setState(praefix0 + did + "." + DeviceGets[2], DeviceData[2]);
}
if (device.Buzzer != DeviceData[3]) {
DeviceData[3] = device.Buzzer;
setState(praefix0 + did + "." + DeviceGets[3], DeviceData[3]);
}
if (device.LcdBrightness != DeviceData[4]) {
DeviceData[4] = device.LcdBrightness;
setState(praefix0 + did + "." + DeviceGets[4], DeviceData[4]);
}
if (device.Temperature != DeviceData[5]) {
DeviceData[5] = device.Temperature;
setState(praefix0 + did + "." + DeviceGets[5], DeviceData[5]);
}
if (device.Humidity != DeviceData[6]) {
DeviceData[6] = device.Humidity;
setState(praefix0 + did + "." + DeviceGets[6], DeviceData[6]);
}
if (device.PM2_5 != DeviceData[7]) {
DeviceData[7] = device.PM2_5;
setState(praefix0 + did + "." + DeviceGets[7], DeviceData[7]);
}
if (device.FilterRemaining != DeviceData[8]) {
DeviceData[8] = device.FilterRemaining;
setState(praefix0 + did + "." + DeviceGets[8], DeviceData[8]);
}
TriggerLock = false;
log(DeviceData)
}
async function SetDevice(x, data) {
if (logging) log("Reaching SetDevice x=" + x + " data=" + data);
DeviceData[x] = data;
//await device.init(); // connect to device and poll for properties
switch (x) {
case 0:
log(await device.setPower(data));
break;
case 1:
log(await device.setMode(data));
break;
case 2:
log(await device.setFanLevel(data));
break;
case 3:
log(await device.setBuzzer(data));
break;
case 4:
log(device.setLcdBrightness(data));
break;
case 5:
log(await device.setChildLock(data));
break;
default:
}
}
function CreateDpTrigger() {
if (logging) log("Reaching CreateDpTrigger");
for (let x = 0; x < AllDevicesRaw.length; x++) {
if (AllDevicesRaw[x].did = 317335021) {
for (let y = 0; y < DeviceSets.length; y++) {
on(praefix0 + AllDevicesRaw[x].did + "." + DeviceSets[y], function (dp) { //Bei Statusänderung
log("Triggered y=" + y + " Triggerlock=" + TriggerLock)
if (TriggerLock) {
TriggerLock = false;
log("Refresh write, triggering canceled");
} else {
log("Wonna write now");
SetDevice(y, dp.state.val);
};
});
};
}
}
}