forked from tibber/com.tibber.athom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
108 lines (93 loc) · 3.43 KB
/
app.ts
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
import 'newrelic';
import sourceMapSupport from 'source-map-support';
import { App } from 'homey';
import Homey from 'homey/lib/Homey';
import { setGlobalAttributes } from './lib/newrelic-transaction';
import * as appJson from './app.json';
import { HomeDevice } from './drivers/home/device';
sourceMapSupport.install();
type HomeyWithMissingTypings = Homey & {
platformVersion: string;
};
class TibberApp extends App {
async onInit() {
this.log('Tibber app is running...');
// Init Debugger
if (process.env.DEBUG === '1') {
// @ts-expect-error
if (this.homey.platform == "local") {
try {
require('inspector').waitForDebugger();
}
catch (error) {
require('inspector').open(9291, '0.0.0.0', true);
}
}
}
const { version: firmwareVersion, platformVersion } = this
.homey as HomeyWithMissingTypings;
const { version: appVersion } = appJson;
this.log(`platformVersion:`, platformVersion);
this.log(`firmwareVersion:`, firmwareVersion);
this.log(`appVersion:`, appVersion);
setGlobalAttributes({ firmwareVersion, platformVersion, appVersion });
const v = this.homey.settings.get('v');
if (v !== 2) {
this.log('Cleaning logs');
this.homey.settings.set('v', 2);
this.cleanupLogs('*').catch(console.error);
}
this.#initWidgets();
}
async cleanupLogs(prefix: string) {
if (prefix !== '*') return;
const logs = await this.homey.insights.getLogs();
await Promise.all(
logs
.filter(({ name }) => name.startsWith(prefix))
.map((log) => {
console.log('Deleting log', log.name);
return this.homey.insights.deleteLog(log);
}),
);
}
async onUninit() {
this.log('Tibber app is stopping');
}
// WIDGET Settings ==============================================================================
async #initWidgets(){
// @ts-expect-error
this.homey.dashboards.getWidget('price').registerSettingAutocompleteListener('device_home', async (query: string, settings: any) => {
let homes: { name: string; id: any; }[] = [];
let devices = this.homey.drivers.getDriver('home').getDevices();
devices.forEach(device => {
homes.push({
name: device.getName(),
id: device.getData().id
})
});
return homes.filter((item) => item.name.toLowerCase().includes(query.toLowerCase()));
});
// @ts-expect-error
this.homey.dashboards.getWidget('price').registerSettingAutocompleteListener('device_pulse', async (query: string, settings: any) => {
let homes: { name: string; id: any; }[] = [];
let devices = this.homey.drivers.getDriver('pulse').getDevices();
devices.forEach(device => {
homes.push({
name: device.getName(),
id: device.getData().id
})
});
return homes.filter((item) => item.name.toLowerCase().includes(query.toLowerCase()));
});
}
// WIDGET API ============================================================================
async apiTriggerRealtimeData(){
// let device = this.homey.drivers.getDriver('home').getDevices()[0] as HomeDevice;
this.homey.drivers.getDriver('home').getDevices().forEach( (device ) => {
(device as HomeDevice).triggerRealtimeData();
});
}
}
// workaround for `The class exported in '<filepath>' must extend Homey.<classname>` error
module.exports = TibberApp;