|
1 | 1 | class MyExampleExtension_TS_ { |
2 | | - constructor(zigbee, mqtt, state, publishEntityState, eventBus, settings, logger) { |
3 | | - logger.info('Loaded MyExampleExtension_TS_'); |
4 | | - mqtt.publish('example/extension', 'hello from MyExampleExtension_TS_'); |
5 | | - this.mqttBaseTopic = settings.get().mqtt.base_topic; |
6 | | - this.eventBus = eventBus; |
| 2 | + constructor( |
| 3 | + zigbee, |
| 4 | + mqtt, |
| 5 | + state, |
| 6 | + publishEntityState, |
| 7 | + eventBus, |
| 8 | + enableDisableExtension, |
| 9 | + restartCallback, |
| 10 | + addExtension, |
| 11 | + settings, |
| 12 | + logger, |
| 13 | + ) { |
| 14 | + this.zigbee = zigbee; |
7 | 15 | this.mqtt = mqtt; |
8 | | - this.eventBus.on('stateChange', this.onStateChange.bind(this), this.constructor.name); |
| 16 | + this.state = state; |
| 17 | + this.publishEntityState = publishEntityState; |
| 18 | + this.eventBus = eventBus; |
| 19 | + this.enableDisableExtension = enableDisableExtension; |
| 20 | + this.restartCallback = restartCallback; |
| 21 | + this.addExtension = addExtension; |
| 22 | + this.settings = settings; |
| 23 | + this.logger = logger; |
| 24 | + |
| 25 | + this.logger.info('Loaded MyExampleExtension_TS_'); |
| 26 | + this.mqttBaseTopic = this.settings.get().mqtt.base_topic; |
9 | 27 | } |
10 | 28 |
|
11 | | - async onStateChange(data) { |
12 | | - console.log('State changed', data); // comment this out if clutters logs |
| 29 | + /** |
| 30 | + * Called when the extension starts (on Zigbee2MQTT startup, or when the extension is saved at runtime) |
| 31 | + */ |
| 32 | + start() { |
| 33 | + this.mqtt.publish('example/extension', 'hello from MyExampleExtension_TS_'); |
| 34 | + |
| 35 | + // all possible events can be seen here: https://github.com/Koenkk/zigbee2mqtt/blob/master/lib/eventBus.ts |
| 36 | + |
| 37 | + this.eventBus.onStateChange(this, this.onStateChange.bind(this)); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Called when the extension stops (on Zigbee2MQTT shutdown, or when the extension is saved/removed at runtime) |
| 42 | + */ |
| 43 | + stop() { |
| 44 | + this.eventBus.removeListeners(this); |
| 45 | + } |
13 | 46 |
|
| 47 | + async onStateChange(data) { |
| 48 | + // see typing (properties) here: https://github.com/Koenkk/zigbee2mqtt/blob/master/lib/types/types.d.ts => namespace eventdata |
14 | 49 | const { entity, update } = data; |
15 | 50 |
|
16 | | - //example how to toggle state |
| 51 | + // example how to toggle state |
17 | 52 | if (entity.ID === '0x00158d000224154d') { |
18 | | - //state changed for some device (example: clicked a button) |
| 53 | + this.logger.info(`State changed for 0x00158d000224154d: ${JSON.stringify(data)}`); |
| 54 | + |
| 55 | + // state changed for some device (example: clicked a button) |
19 | 56 | if (update.action === 'single') { |
20 | 57 | const myLampIeeAddr = '0x00124b001e73227f'; // change this |
| 58 | + |
21 | 59 | this.mqtt.onMessage(`${this.mqttBaseTopic}/${myLampIeeAddr}/set`, JSON.stringify({ state: 'toggle' })); |
22 | 60 | } |
23 | 61 | } |
24 | 62 | } |
25 | | - |
26 | | - async onMQTTMessage(topic, message) { |
27 | | - // console.log({topic, message}); |
28 | | - } |
29 | | - |
30 | | - async stop() { |
31 | | - this.eventBus.removeListeners(this.constructor.name); |
32 | | - } |
33 | 63 | } |
34 | 64 |
|
35 | 65 | module.exports = MyExampleExtension_TS_; |
0 commit comments