Skip to content

Commit

Permalink
fix(external-extension): update extensions editor example to 2.0.0 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec authored Dec 9, 2024
1 parent 3c1de72 commit dc95dd7
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions src/components/extensions-editor/example-extension.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,65 @@
class MyExampleExtension_TS_ {
constructor(zigbee, mqtt, state, publishEntityState, eventBus, settings, logger) {
logger.info('Loaded MyExampleExtension_TS_');
mqtt.publish('example/extension', 'hello from MyExampleExtension_TS_');
this.mqttBaseTopic = settings.get().mqtt.base_topic;
this.eventBus = eventBus;
constructor(
zigbee,
mqtt,
state,
publishEntityState,
eventBus,
enableDisableExtension,
restartCallback,
addExtension,
settings,
logger,
) {
this.zigbee = zigbee;
this.mqtt = mqtt;
this.eventBus.on('stateChange', this.onStateChange.bind(this), this.constructor.name);
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;
}

async onStateChange(data) {
console.log('State changed', data); // comment this out if clutters logs
/**
* 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
// example how to toggle state
if (entity.ID === '0x00158d000224154d') {
//state changed for some device (example: clicked a button)
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' }));
}
}
}

async onMQTTMessage(topic, message) {
// console.log({topic, message});
}

async stop() {
this.eventBus.removeListeners(this.constructor.name);
}
}

module.exports = MyExampleExtension_TS_;

0 comments on commit dc95dd7

Please sign in to comment.