-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
117 lines (95 loc) · 3.45 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
109
110
111
112
113
114
115
116
117
import Homey from 'homey';
import { AlexaApi } from './lib/api';
import { Log } from 'homey-log';
import { Logger } from './lib/logger';
class EchoRemoteApp extends Homey.App {
private logger: Logger | undefined;
public api: AlexaApi | undefined;
private auditInterval: NodeJS.Timeout | undefined;
private getSetting = (key: string): any => this.homey.settings.get(key);
private setSetting = (key: string, value: any): void => this.homey.settings.set(key, value);
/**
* onInit is called when the app is initialized.
*/
async onInit() {
const page = this.getSetting('page');
// TODO: Remove legacy fix for settings from old versions
if (page?.startsWith('https://www.')) {
this.setSetting('page', page.replace('https://www.', ''));
}
// Disable homey logger for now because we were running into rate limiting.
// For now people should submit diagnostic reports if they run into issues.
const homeyLogger = undefined; // new Log({ homey: this.homey });
this.logger = new Logger(homeyLogger, this.getSetting('diagnosticLogging'), 'debug');
const errorTrigger = this.homey.flow.getTriggerCard('error');
const auth = this.getSetting('auth');
this.api = new AlexaApi(auth, this.logger);
this.api.on('authenticated', (auth) => this.setSetting('auth', auth));
this.api.on('connected', async (payload) => this.emit('connected', payload));
this.api.on('device-info', (info) => {
this.deviceEmit(info.id, 'device-info', info);
});
this.api.on('error', (error) => {
this.error(error);
this.logger?.exception(error);
errorTrigger.trigger({ error: error.message });
});
try {
auth &&
(await this.api.connect({
page: this.getSetting('page'),
language: this.homey.i18n.getLanguage(),
}));
} catch (e) {
this.error(e);
}
if (this.auditInterval) this.homey.clearInterval(this.auditInterval);
this.auditInterval = this.homey.setInterval(() => this.api?.checkConnection(), 5 * 60 * 1000);
}
async connect() {
this.logger!.diagnosticLogging = this.getSetting('diagnosticLogging');
return await this.api?.connect({
page: this.getSetting('page'),
language: this.homey.i18n.getLanguage(),
});
}
async disconnect() {
return this.api?.reset();
}
async status() {
return {
connected: this.api?.connected,
};
}
async reset() {
this.setSetting('auth', undefined);
return this.api?.reset();
}
private deviceEmit = async (id: string, event: string, payload: any) => {
try {
const devices = [...this.homey.drivers.getDriver('echo').getDevices(), ...this.homey.drivers.getDriver('group').getDevices()];
const device = devices.find((d) => d.getData().id === id);
const apiDevices = (await this.api?.getDevices()) ?? [];
if (device) {
device.emit(event, payload);
apiDevices
.filter((d) => d.parentGroups?.includes(id))
?.forEach((d) =>
this.homey.drivers
.getDriver('echo')
.getDevice({
id: d.id,
})
.emit(event, payload),
);
} else {
apiDevices
.filter((d) => d.parentGroups?.includes(id))
.forEach((x) => devices.find((d) => d.getData().id === x.id)?.emit(event, payload));
}
} catch (e) {
this.error('Error emitting device event', e);
}
};
}
module.exports = EchoRemoteApp;