-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathexternalExtensions.ts
70 lines (63 loc) · 2.31 KB
/
externalExtensions.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
import type Extension from "./extension";
import logger from "../util/logger";
import * as settings from "../util/settings";
import ExternalJSExtension from "./externalJS";
type TModule = new (...args: ConstructorParameters<typeof Extension>) => Extension;
export default class ExternalExtensions extends ExternalJSExtension<TModule> {
constructor(
zigbee: Zigbee,
mqtt: Mqtt,
state: State,
publishEntityState: PublishEntityState,
eventBus: EventBus,
enableDisableExtension: (enable: boolean, name: string) => Promise<void>,
restartCallback: () => Promise<void>,
addExtension: (extension: Extension) => Promise<void>,
) {
super(
zigbee,
mqtt,
state,
publishEntityState,
eventBus,
enableDisableExtension,
restartCallback,
addExtension,
"extension",
"external_extensions",
);
}
protected async removeJS(_name: string, mod: TModule): Promise<void> {
await this.enableDisableExtension(false, mod.name);
}
protected async loadJS(name: string, mod: TModule, newName?: string): Promise<void> {
try {
// stop if already started
await this.enableDisableExtension(false, mod.name);
await this.addExtension(
new mod(
this.zigbee,
this.mqtt,
this.state,
this.publishEntityState,
this.eventBus,
this.enableDisableExtension,
this.restartCallback,
this.addExtension,
// @ts-expect-error additional params that don't fit the internal `Extension` type
settings,
logger,
),
);
/* v8 ignore start */
logger.info(`Loaded external extension '${newName ?? name}'.`);
} catch (error) {
logger.error(
/* v8 ignore next */
`Failed to load external extension '${newName ?? name}'. Check the code for syntax error and make sure it is up to date with the current Zigbee2MQTT version.`,
);
throw error;
}
/* v8 ignore stop */
}
}