-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathexample-extension.js
65 lines (55 loc) · 2.13 KB
/
example-extension.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
class MyExampleExtension_TS_ {
constructor(
zigbee,
mqtt,
state,
publishEntityState,
eventBus,
enableDisableExtension,
restartCallback,
addExtension,
settings,
logger,
) {
this.zigbee = zigbee;
this.mqtt = mqtt;
this.state = state;
this.publishEntityState = publishEntityState;
this.eventBus = eventBus;
this.enableDisableExtension = enableDisableExtension;
this.restartCallback = restartCallback;
this.addExtension = addExtension;
this.settings = settings;
this.logger = logger;
this.logger.info('Loaded MyExampleExtension_TS_');
this.mqttBaseTopic = this.settings.get().mqtt.base_topic;
}
/**
* Called when the extension starts (on Zigbee2MQTT startup, or when the extension is saved at runtime)
*/
start() {
this.mqtt.publish('example/extension', 'hello from MyExampleExtension_TS_');
// all possible events can be seen here: https://github.com/Koenkk/zigbee2mqtt/blob/master/lib/eventBus.ts
this.eventBus.onStateChange(this, this.onStateChange.bind(this));
}
/**
* Called when the extension stops (on Zigbee2MQTT shutdown, or when the extension is saved/removed at runtime)
*/
stop() {
this.eventBus.removeListeners(this);
}
async onStateChange(data) {
// see typing (properties) here: https://github.com/Koenkk/zigbee2mqtt/blob/master/lib/types/types.d.ts => namespace eventdata
const { entity, update } = data;
// example how to toggle state
if (entity.ID === '0x00158d000224154d') {
this.logger.info(`State changed for 0x00158d000224154d: ${JSON.stringify(data)}`);
// state changed for some device (example: clicked a button)
if (update.action === 'single') {
const myLampIeeAddr = '0x00124b001e73227f'; // change this
this.mqtt.onMessage(`${this.mqttBaseTopic}/${myLampIeeAddr}/set`, JSON.stringify({ state: 'toggle' }));
}
}
}
}
module.exports = MyExampleExtension_TS_;