-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
138 lines (108 loc) · 3.76 KB
/
app.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
'use strict';
const Homey = require('homey');
const { EufyClean } = require('./lib/eufy-clean');
const flowActions = require('./lib/flow/actions.js');
const { decrypt, sleep } = require('./lib/helpers.js');
class App extends Homey.App {
trace() {
console.trace.bind(this, '[log]').apply(this, arguments);
}
debug() {
console.debug.bind(this, '[debug]').apply(this, arguments);
}
info() {
console.log.bind(this, '[info]').apply(this, arguments);
}
log() {
console.log.bind(this, '[log]').apply(this, arguments);
}
warn() {
console.warn.bind(this, '[warn]').apply(this, arguments);
}
error() {
console.error.bind(this, '[error]').apply(this, arguments);
}
fatal() {
console.error.bind(this, '[fatal]').apply(this, arguments);
}
// -------------------- INIT ----------------------
async onInit() {
this.log(`${this.homey.manifest.id} - ${this.homey.manifest.version} started...`);
this.deviceList = [];
this.driversInitialized = false;
this.appInitialized = false;
this.eufyClean = null;
}
async initApp() {
this.initEufyClean();
flowActions.init(this.homey);
}
// ---------------------------- GETTERS/SETTERS ----------------------------------
async setDevice(device) {
this.deviceList = [...this.deviceList, device];
}
async setDevices(devices) {
this.deviceList = [...this.deviceList, ...devices];
if (!this.driversInitialized) {
this.driversInitialized = true;
await sleep(2000);
this.initApp();
}
}
async removeDevice(deviceId) {
try {
this.homey.app.log('removeDevice', deviceId);
const filteredList = this.deviceList.filter((dl) => {
const data = dl.getData();
return data.id !== deviceId;
});
this.deviceList = filteredList;
} catch (error) {
this.error(error);
}
}
async initDevices() {
this.deviceList.every(async (device, index) => {
await device.onStartup(index);
});
}
async disableDevices() {
this.deviceList.every(async (device, index) => {
try {
await device.disableDevice();
} catch (error) {
console.log(error);
}
});
}
async enableDevices(loginData) {
this.deviceList.every(async (device, index) => {
device.setSettings({...loginData});
await device.enableDevice();
});
}
// -------------------------- EUFY CLEAN -------------------------
async initEufyClean(driverUsername = null, driverPassword = null) {
if (driverUsername && driverPassword) {
// Initialize when called from driver
this.eufyClean = new EufyClean(driverUsername, driverPassword);
await this.eufyClean.init();
} else if (!this.eufyClean && this.deviceList.length) {
// Initialize on startup when there are paired devices
const device = this.deviceList[0];
const settings = device.getSettings();
const { username, password } = settings;
if(username && password) {
this.eufyClean = new EufyClean(decrypt(username), decrypt(password) );
await this.eufyClean.init();
await this.initDevices();
} else {
// No login data found, initialize without login - only for LocalConnect
this.eufyClean = new EufyClean();
await this.initDevices();
}
}
this.appInitialized = true;
}
}
module.exports = App;