forked from edwinvdpol/homey-tedee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
310 lines (260 loc) · 7.07 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
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
'use strict';
const {OAuth2App} = require('homey-oauth2app');
const Client = require('./lib/Client');
const {Log} = require('homey-log');
const syncLocksInterval = 10 * 1000; // 10 seconds
const refreshDevicesInterval = 5 * 60 * 1000; // 5 minutes
class Tedee extends OAuth2App {
static OAUTH2_CLIENT = Client;
/*
|-----------------------------------------------------------------------------
| Application events
|-----------------------------------------------------------------------------
*/
/**
* Application initialized.
*
* @async
* @returns {Promise<void>}
*/
async onOAuth2Init() {
this.log('Application initialized');
// Sentry logging
this.homeyLog = new Log({ homey: this.homey });
// Reset timers
this.refreshTimer = null;
this.syncTimer = null;
// Register flow cards
this._registerActionFlowCards();
this._registerConditionFlowCards();
// Start timers if not already started
this.homey.setInterval(this._startTimers.bind(this), 5000);
// Register app event listeners
this.homey.on('cpuwarn', () => {
this.log('-- CPU warning! --');
}).on('memwarn', () => {
this.log('-- Memory warning! --');
}).on('unload', () => {
// Stop timers
this._stopTimers();
this.log('-- Unloaded! _o/ --');
});
}
/*
|-----------------------------------------------------------------------------
| Application actions
|-----------------------------------------------------------------------------
*/
/**
* Refresh devices (full update).
*
* @async
* @returns {Promise<void>}
* @private
*/
async _refreshDevices() {
await this._updateDevices('refresh');
}
/**
* Sync locks (delta update).
*
* @async
* @returns {Promise<void>}
* @private
*/
async _syncLocks() {
await this._updateDevices('sync');
}
/**
* Update devices by action.
*
* @async
* @param {string} action
* @returns {Promise<void>}
* @private
*/
async _updateDevices(action) {
try {
// Set oAuth client
this.oAuth2Client = this.getFirstSavedOAuth2Client();
let data = {};
// Fetch requested data from tedee API
if (action === 'refresh') {
data = await this.oAuth2Client.getAllDevicesDetails();
} else if (action === 'sync') {
data = await this.oAuth2Client.getSyncLocks();
}
// Check data
if (Object.keys(data).length === 0) {
return;
}
// Update devices from list
await this._updateDevicesList(data);
} catch (err) {
await this._stopTimers(err.message);
}
}
/**
* Update devices from list of tedee devices.
*
* @async
* @param {object} list
* @returns {Promise<void>}
* @private
*/
async _updateDevicesList(list) {
// Search for devices and set data
const drivers = this.homey.drivers.getDrivers();
for (const driverId in drivers) {
if (!drivers.hasOwnProperty(driverId)) {
return;
}
const devices = drivers[driverId].getDevices();
for (const device of devices) {
for (const data of list) {
if (data.id !== Number(device.getSetting('tedee_id'))) {
continue;
}
await device.setDeviceData(data);
}
}
}
}
/*
|-----------------------------------------------------------------------------
| Timers actions
|-----------------------------------------------------------------------------
*/
/**
* Start timers.
*
* @async
* @returns {Promise<void>}
* @private
*/
async _startTimers() {
try {
// Check if timers are already running
if (await this._timersAreRunning()) {
return;
}
// Throws error if none is available, and stop timer
this.oAuth2Client = this.getFirstSavedOAuth2Client();
this.log('Starting timers');
// Start interval for delta updates
this.syncTimer = this.homey.setInterval(this._syncLocks.bind(this), syncLocksInterval);
// Start interval for full updates
this.refreshTimer = this.homey.setInterval(this._refreshDevices.bind(this), refreshDevicesInterval);
} catch (err) {
await this._stopTimers(err.message);
}
}
/**
* Stop timers.
*
* @async
* @param {string|null} reason
* @returns {Promise<void>}
* @private
*/
async _stopTimers(reason = null) {
if (! await this._timersAreRunning()) {
return;
}
// Logging
if (reason == null) {
this.log('Stopping timers');
} else {
this.log(`Stopping timers: ${reason}`);
}
// Stop delta updates
if (this.syncTimer != null) {
this.homey.clearInterval(this.syncTimer);
this.syncTimer = null;
}
// Stop full updates
if (this.refreshTimer != null) {
this.homey.clearInterval(this.refreshTimer);
this.refreshTimer = null;
}
}
/**
* Return if timers are running.
*
* @async
* @returns {Promise<boolean>}
* @private
*/
async _timersAreRunning() {
return this.syncTimer != null && this.refreshTimer != null;
}
/**
* Verify timers.
*
* @async
* @returns {Promise<void>}
*/
async verifyTimers() {
try {
const drivers = this.homey.drivers.getDrivers();
let devices = 0;
if (Object.keys(drivers).length === 0) {
return this._stopTimers();
}
for (const driverId in drivers) {
if (!drivers.hasOwnProperty(driverId)) {
return;
}
// Get driver
const driver = this.homey.drivers.getDriver(driverId);
// Add number of devices
devices += Object.keys(driver.getDevices()).length;
}
// Stop timers when no devices found
if (devices === 0) {
return this._stopTimers('No devices found');
}
return this._startTimers();
} catch (err) {
this.error('Verify timers:', err.message);
}
}
/*
|-----------------------------------------------------------------------------
| Flow cards
|-----------------------------------------------------------------------------
*/
/**
* Register condition flow cards.
*
* @returns {void}
* @private
*/
_registerConditionFlowCards() {
// ... and is charging ...
this.homey.flow.getConditionCard('charging').registerRunListener(async (args) => {
return args.device.getCapabilityValue('charging') === true;
});
// ... and is connected ...
this.homey.flow.getConditionCard('connected').registerRunListener(async (args) => {
return args.device.getCapabilityValue('connected') === true;
});
// ... and update is available ...
this.homey.flow.getConditionCard('update_available').registerRunListener(async (args) => {
return args.device.getCapabilityValue('update_available') === true;
});
}
/**
* Register action flow cards.
*
* @returns {void}
* @private
*/
_registerActionFlowCards() {
// ... then pull the spring ...
this.homey.flow.getActionCard('open').registerRunListener(async (args) => {
return args.device.open();
});
}
}
module.exports = Tedee;