-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
489 lines (407 loc) · 19.1 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
'use strict';
const Homey = require('homey');
const { HomeyAPI } = require('homey-api');
const tinycolor = require('tinycolor2');
const flowActions = require('./lib/flows/actions');
const flowTriggers = require('./lib/flows/triggers');
const { calculateDuration, calculateComparison, formatToken, calculationType, convertNumber, convertText } = require('./lib/helpers');
const _settingsKey = `${Homey.manifest.id}.settings`;
class App extends Homey.App {
log() {
console.log.bind(this, '[log]').apply(this, arguments);
}
error() {
console.error.bind(this, '[error]').apply(this, arguments);
}
// -------------------- INIT ----------------------
async onInit() {
// Fix Homey API bug for version <= 8
const myHomey = this.homey.platform ? this.homey : { ...this.homey, platform: 'local', platformVersion: 1 };
this.log(`${this.homey.manifest.id} - ${this.homey.manifest.version} started...`);
this.TOKENS = {};
this.SRC_LIST = [
{ name: 'duration', type: 'string' },
{ name: 'durationInSeconds', type: 'number' },
{ name: 'currency', type: 'string' },
{ name: 'comparison', type: 'number' },
{ name: 'calculation', type: 'number' },
{ name: 'decimals', type: 'number' },
{ name: 'text', type: 'string' },
{ name: 'replacementString', type: 'string' }
];
this._api = await HomeyAPI.createAppAPI({
homey: myHomey,
debug: false
});
await this.initSettings();
this.log('[onInit] - Loaded settings', this.appSettings);
// this.sendNotifications();
await this.setTokens(this.appSettings.VARIABLES, this.appSettings.VARIABLES);
await flowActions.init(this);
await flowTriggers.init(this);
await this.setCheckZoneOnOffInterval();
await this.setupWidget();
}
// -------------------- Notification updates ----------------------
async sendNotifications() {
try {
const ntfy2023101701 = `[Flow Utilities] (1/2) - Zone trigger & action cards are now deprecated.`;
const ntfy2023101702 = `[Flow Utilities] (2/2) - Visit forum for more info: https://t.ly/9NeAG`;
await this.homey.notifications.createNotification({
excerpt: ntfy2023101702
});
await this.homey.notifications.createNotification({
excerpt: ntfy2023101701
});
} catch (error) {
this.log('sendNotifications - error', console.error());
}
}
// -------------------- SETTINGS ----------------------
async initSettings() {
let settingsInitialized = false;
this.homey.settings.getKeys().forEach((key) => {
if (key == _settingsKey) {
settingsInitialized = true;
}
});
if (settingsInitialized) {
this.log('[initSettings] - Found settings key', _settingsKey);
this.appSettings = this.homey.settings.get(_settingsKey);
if (!('ZONES' in this.appSettings)) {
try {
await this.updateSettings({
...this.appSettings,
ZONES: {}
});
} catch (err) {
this.error('[initSettings][ZONES]', err);
}
}
} else {
this.log(`Initializing ${_settingsKey} with defaults`);
try {
await this.updateSettings({
VARIABLES: [],
COMPARISONS: [],
TOTALS: [],
ZONES: {}
});
} catch (err) {
this.error('[initSettings][init with defaults]', err);
}
}
}
async updateSettings(settings, update = false) {
try {
this.log('[updateSettings] - New settings:', settings);
const oldSettings = this.appSettings;
this.appSettings = settings;
await this.homey.settings.set(_settingsKey, this.appSettings);
if (update) {
await this.setTokens(settings.VARIABLES, oldSettings.VARIABLES);
}
} catch (err) {
this.error('[updateSettings]', err);
}
}
async removeSettings(token) {
const COMPARISONS = this.appSettings.COMPARISONS.filter((setting) => setting.token !== token);
const TOTALS = this.appSettings.TOTALS.filter((setting) => setting.token !== token);
this.log('[removeSettings] - Remove settings for', token);
try {
await this.updateSettings({
...this.appSettings,
COMPARISONS,
TOTALS
});
} catch (err) {
this.error('[removeSettings]', err);
}
}
async updateTotals(token, newValueObj) {
const currentTokenTotals = this.appSettings.TOTALS.find((total) => total.token === token);
const otherTotals = this.appSettings.TOTALS.filter((total) => total.token !== token);
const newTokenTotals = { token, ...currentTokenTotals, ...newValueObj };
try {
await this.updateSettings({
...this.appSettings,
TOTALS: [...otherTotals, newTokenTotals]
});
} catch (err) {
this.error('[updasteTotals]', err);
}
}
async setTokens(newVariables, oldVariables = []) {
try {
await newVariables.forEach((t) => {
const existingVariableData = this.appSettings.TOTALS.find((x) => x.token === t);
this.createTokenVariants(t, existingVariableData);
});
if (oldVariables.length) {
const difference = oldVariables.filter((x) => !newVariables.includes(x));
difference.forEach(async (d) => {
await this.removeTokenVariants(d);
this.removeSettings(d);
});
}
} catch (err) {
this.error('[setTokens]', err);
}
}
async createToken(name, paramOptions) {
const defaultOptions = {
src: '',
value: '',
type: 'string'
};
const options = { ...defaultOptions, ...paramOptions };
const { src, value, type } = options;
const suffix = this.homey.__(`helpers.${src}`);
let title = `${name} ${suffix}`;
const id = formatToken(title);
if (!this.TOKENS[id]) {
try {
this.TOKENS[id] = await this.homey.flow.createToken(id, {
type,
title
});
this.log(`[createToken] - created Token => ID: ${id} - Title: ${title} - Type: ${type}`);
} catch (error) {
return this.error(`[creatingToken] ${error}`);
}
}
if (this.TOKENS[id]) {
try {
await this.TOKENS[id].setValue(value);
this.log(`[createToken] - set token value => ID: ${id} - Value: ${value}`);
} catch (error) {
return this.error(`[setToken value] ${error}`);
}
}
}
async removeToken(name, src) {
const suffix = this.homey.__(`helpers.${src}`);
let title = `${name} ${suffix}`;
const id = formatToken(title);
if (this.TOKENS[id]) {
try {
await this.TOKENS[id].unregister();
// Remove token from list, since unregister doesn't do this.
delete this.TOKENS[id];
this.log(`[removeToken] - removed Token => ID: ${id} - Title: ${title} - ${!!this.TOKENS[id]}`);
} catch (err) {
this.error('[removeToken]', err);
}
}
}
async createTokenVariants(tokenID, existingVariableData) {
this.SRC_LIST.forEach((src) => {
const suffix = this.homey.__(`helpers.${src.name}`);
const title = `${tokenID} ${suffix}`;
const id = formatToken(title);
const fallbackValue = src.type === 'string' ? '' : 0;
if (!this.TOKENS[id]) {
this.createToken(tokenID, { src: src.name, type: src.type, value: (existingVariableData && existingVariableData[src.name]) || fallbackValue });
}
});
}
async removeTokenVariants(tokenID) {
this.SRC_LIST.forEach((src) => {
this.removeToken(tokenID, src.name);
});
}
// -------------------- FUNCTIONS ----------------------
// ToDo: Remove deprecated
async setCheckZoneOnOffInterval(oldZones = [], newZones = Object.keys(this.appSettings.ZONES)) {
const devices = Object.values(await this._api.devices.getDevices());
const newC = newZones.filter((d) => !oldZones.includes(d));
const that = this;
const filteredDevices = devices.filter((d) => d.capabilitiesObj && d.capabilitiesObj.onoff && newC.includes(d.zone));
for (const device of filteredDevices) {
device.makeCapabilityInstance('onoff', () => {
that.checkZoneOnOff(filteredDevices, device.zone);
that.checkDeviceOnOff(device, device.zone);
});
}
}
// ToDo: Remove deprecated
async checkDeviceOnOff(device, zoneId) {
const onoffDevice = device.zone == zoneId && device.capabilitiesObj.onoff && !device.settings.energy_alwayson && !device.settings.override_onoff;
if (onoffDevice) {
const value = device.capabilitiesObj.onoff.value;
const key = value ? 'DEVICE_ZONE_ON' : 'DEVICE_ZONE_OFF';
const zone = await device.getZone();
const name = device.name ? device.name : 'Unknown';
const zoneName = zone ? zone.name : 'Unknown';
this.homey.app[`trigger_${key}`]
.trigger({ name, zone: zoneName, ison: value }, { zone: zoneId })
.catch((err) => this.error(`[Device][trigger_${key}]`, err))
.then((res) => this.log(`[trigger_${key}] - Triggered - ${name} - ${zoneId} - ${zoneName} - ${value}`));
}
}
// ToDo: Remove deprecated
async checkZoneOnOff(devices, zone) {
const onoffDevices = devices.filter((d) => d.zone == zone && d.capabilitiesObj.onoff && !d.settings.energy_alwayson && !d.settings.override_onoff);
if (onoffDevices) {
const isOn = onoffDevices.every((v) => v.settings && v.capabilitiesObj.onoff.value === true);
const isOff = onoffDevices.every((v) => v.capabilitiesObj.onoff.value === false);
let zoneChanges = this.appSettings.ZONES;
let key = null;
let value = null;
if (isOn && !this.appSettings.ZONES[zone]) {
value = true;
key = 'ZONE_ON';
} else if (isOff && !!this.appSettings.ZONES[zone]) {
value = false;
key = 'ZONE_OFF';
}
if (key) {
await this.updateSettings({
...this.appSettings,
ZONES: { ...zoneChanges, [zone]: value }
});
this.homey.app[`trigger_${key}`]
.trigger({}, { zone })
.catch((err) => this.error(`[Zone][trigger_${key}]`, err))
.then((res) => this.log(`[trigger_${key}] - Triggered - ${zone} - ${value}`));
}
}
}
async action_START(token, paramOptions) {
const defaultOptions = {
dateStart: null,
comparison: null
};
const options = { ...defaultOptions, ...paramOptions };
const { dateStart, comparison } = options;
const date = dateStart ? new Date() : dateStart;
const newSettings = this.appSettings.COMPARISONS.filter((setting) => setting.token !== token);
const existing_comparison = this.appSettings.COMPARISONS.find((x) => x.token === token);
await this.updateSettings({
...this.appSettings,
COMPARISONS: [...newSettings, { ...existing_comparison, token, ...(date && { date }), ...(comparison && { comparison }) }]
});
}
async action_END(token, src, value = null) {
this.homey.app.log('[action_END] -', token);
const existing_comparison = this.appSettings.COMPARISONS.find((x) => x.token === token);
if (!existing_comparison) {
throw new Error(`No comparison found for ${token}`);
}
this.homey.app.log('[action_END] - found existing comparison', existing_comparison, 'type', typeof existing_comparison.date);
const date = existing_comparison.date ? new Date() : null;
const existingDate = typeof existing_comparison.date === 'string' ? new Date(existing_comparison.date) : existing_comparison.date;
const duration = date ? calculateDuration(existingDate, date, false, this.homey.__, this.homey.app.log) : null;
const durationInSeconds = date ? calculateDuration(existingDate, date, true, this.homey.__, this.homey.app.log) : null;
const comparison = value ? calculateComparison(existing_comparison.comparison, value) : null;
const totals = this.appSettings.TOTALS.filter((total) => total.token !== token);
if (src === 'duration' && duration) {
await this.updateTotals(token, { duration, durationInSeconds });
await this.createToken(token, { src, value: duration });
await this.createToken(token, { src: 'durationInSeconds', value: durationInSeconds });
this.homey.app.trigger_DURATION
.trigger({ token, duration, durationInSeconds }, { token })
.catch((err) => this.error('[trigger_DURATION]', err))
.then((res) => this.log(`[trigger_DURATION] - Triggered: "${token}: ${duration} ${durationInSeconds}"`));
}
if (src === 'comparison' && comparison !== null) {
await this.updateTotals(token, { comparison });
await this.createToken(token, { src, value: parseFloat(comparison), type: 'number' });
this.homey.app.trigger_COMPARISON
.trigger({ token, comparison }, { token })
.catch((err) => this.error('[trigger_COMPARISON]', err))
.then((res) => this.log(`[trigger_COMPARISON] - Triggered: "${token}: ${comparison}"`));
}
}
async action_SET_CURRENCY(token, number, currency) {
const setLocalCurrency = number.toLocaleString(this.homey.__('helpers.locale'), { style: 'currency', currency: currency });
this.homey.app.log('[action_SET_CURRENCY] - args', token, number, currency, setLocalCurrency);
await this.updateTotals(token, { currency: setLocalCurrency });
await this.createToken(token, { src: 'currency', value: setLocalCurrency });
}
async action_CALCULATION(token, calcType, number1, number2) {
const calculation = calculationType(calcType, number1, number2);
this.homey.app.log('[action_CALCULATION] - args', token, calcType, number1, number2, calculation);
await this.updateTotals(token, { calculation });
await this.createToken(token, { src: 'calculation', value: calculation, type: 'number' });
}
async action_CONVERT_NUMBER(token, number, decimals) {
const calculation = convertNumber(number, decimals);
this.homey.app.log('[action_CONVERT_NUMBER] - args', token, number, decimals);
await this.updateTotals(token, { decimals: calculation });
await this.createToken(token, { src: 'decimals', value: calculation, type: 'number' });
}
async action_CONVERT_TEXT(token, type, text) {
const result = convertText(type, text);
this.homey.app.log('[action_CONVERT_TEXT] - args', token, type, text);
await this.updateTotals(token, { string: result });
await this.createToken(token, { src: 'text', value: result });
}
// ToDo: Remove deprecated
async action_SET_ZONE_PERCENTAGE(zoneId, type, percentage) {
this.homey.app.log('[action_SET_ZONE_PERCENTAGE] - args', zoneId, type, percentage);
const devices = await this._api.devices.getDevices();
for (const device of Object.values(devices)) {
if (device.zone === zoneId && device.capabilities.includes(type)) {
try {
await device.setCapabilityValue(type, percentage / 100);
} catch (error) {
this.error('[action_SET_ZONE_PERCENTAGE][setCapabilityValue]', error);
}
}
}
}
// ToDo: Remove deprecated
async action_SET_ZONE_COLOR(zoneId, color) {
this.homey.app.log('[action_SET_ZONE_COLOR] - args', zoneId, color);
const hsv = tinycolor(color).toHsv();
const hue = Number((hsv.h / 360).toFixed(2));
this.homey.app.log('[action_SET_ZONE_COLOR] - setting color to ', hsv.s, hue);
const devices = await this._api.devices.getDevices();
for (const device of Object.values(devices)) {
if (device.zone === zoneId && device.capabilities.includes('light_hue')) {
try {
await device.setCapabilityValue('light_hue', hue);
} catch (error) {
this.error('[action_SET_ZONE_COLOR][light_hue][setCapabilityValue]', error);
}
}
if (device.zone === zoneId && device.capabilities.includes('light_saturation')) {
try {
await device.setCapabilityValue('light_saturation', hsv.s);
} catch (error) {
this.error('[action_SET_ZONE_COLOR][light_saturation][setCapabilityValue]', error);
}
}
}
}
async action_REPLACE_STRING(token, stringContains, replacementString, inputString) {
if (typeof stringContains === 'string' && typeof replacementString === 'string' && typeof inputString === 'string') {
const result = inputString.replaceAll(stringContains, replacementString);
this.homey.app.log('[action_REPLACE_STRING] - args', token, result);
try {
await this.updateTotals(token, { replacementString: result });
await this.createToken(token, { src: 'replacementString', value: result });
} catch (error) {
this.error('[action_REPLACE_STRING]', error);
}
} else {
this.error('Input format not string type');
}
}
async setupWidget() {
const widget = this.homey.dashboards.getWidget('flow-utilities-variables');
widget.registerSettingAutocompleteListener('selectVariable', async (query, settings) => {
this.log('[setupWidget] - Autocomplete query:', query, this.appSettings.VARIABLES);
const variables = this.appSettings.VARIABLES.map((item) => {
return {
id: item,
name: item
};
});
return variables.filter((item) => item.name.toLowerCase().includes(query.toLowerCase()));
});
}
}
module.exports = App;