Files
zigbee2mqtt/lib/extension/onEvent.ts
T
Koen Kanters dd1e0db407 fix(ignore): Use zhc types (#20006)
* fix(ignore): Use zhc TS types

* updates
2023-12-04 10:05:05 +01:00

52 lines
2.0 KiB
TypeScript

import * as zhc from 'zigbee-herdsman-converters';
import Extension from './extension';
/**
* This extension calls the zigbee-herdsman-converters onEvent.
*/
export default class OnEvent extends Extension {
override async start(): Promise<void> {
for (const device of this.zigbee.devices(false)) {
this.callOnEvent(device, 'start', {});
}
this.eventBus.onDeviceMessage(this, (data) => this.callOnEvent(data.device, 'message', this.convertData(data)));
this.eventBus.onDeviceJoined(this,
(data) => this.callOnEvent(data.device, 'deviceJoined', this.convertData(data)));
this.eventBus.onDeviceInterview(this,
(data) => this.callOnEvent(data.device, 'deviceInterview', this.convertData(data)));
this.eventBus.onDeviceAnnounce(this,
(data) => this.callOnEvent(data.device, 'deviceAnnounce', this.convertData(data)));
this.eventBus.onDeviceNetworkAddressChanged(this,
(data) => this.callOnEvent(data.device, 'deviceNetworkAddressChanged', this.convertData(data)));
this.eventBus.onEntityOptionsChanged(this,
(data) => {
if (data.entity.isDevice()) {
this.callOnEvent(data.entity, 'deviceOptionsChanged', data)
.then(() => this.eventBus.emitDevicesChanged());
}
});
}
private convertData(data: KeyValue): KeyValue {
return {...data, device: data.device.zh};
}
override async stop(): Promise<void> {
super.stop();
for (const device of this.zigbee.devices(false)) {
await this.callOnEvent(device, 'stop', {});
}
}
private async callOnEvent(device: Device, type: zhc.OnEventType, data: KeyValue): Promise<void> {
const state = this.state.get(device);
zhc.onEvent(type, data, device.zh);
if (device.definition?.onEvent) {
const options: KeyValue = device.options;
await device.definition.onEvent(type, data, device.zh, options, state);
}
}
}