Refactor onEvent extension to TypeScript

This commit is contained in:
Koen Kanters 2021-09-19 11:37:08 +02:00
parent 48de2fa647
commit 7f4a9c2a9b
5 changed files with 50 additions and 48 deletions

View File

@ -87,7 +87,7 @@ class Configure extends ExtensionTS {
}
private async configure(device: Device, event: 'started' | 'zigbee_event' | 'reporting_disabled' | 'mqtt_message',
force=false, thowError=false): Promise<boolean> {
force=false, thowError=false): Promise<void> {
if (!force) {
if (!device.definition?.configure || device.zhDevice.interviewing) {
return;
@ -105,7 +105,7 @@ class Configure extends ExtensionTS {
}
if (this.configuring.has(device.ieeeAddr) || (this.attempts[device.ieeeAddr] >= 3 && !force)) {
return false;
return;
}
this.configuring.add(device.ieeeAddr);

View File

@ -1,38 +0,0 @@
const Extension = require('./extension');
const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
/**
* This extension calls the zigbee-herdsman-converters onEvent.
*/
class OnEvent extends Extension {
async onZigbeeStarted() {
for (const device of this.zigbee.getClientsLegacy()) {
const resolvedEntity = this.zigbee.resolveEntityLegacy(device);
this.callOnEvent(resolvedEntity, 'start', {});
}
}
onZigbeeEvent(type, data, resolvedEntity) {
if (resolvedEntity && resolvedEntity.type === 'device') {
this.callOnEvent(resolvedEntity, type, data);
}
}
async stop() {
super.stop();
for (const device of this.zigbee.getClientsLegacy()) {
const resolvedEntity = this.zigbee.resolveEntityLegacy(device);
this.callOnEvent(resolvedEntity, 'stop', {});
}
}
callOnEvent(resolvedEntity, type, data) {
zigbeeHerdsmanConverters.onEvent(type, data, resolvedEntity.device, resolvedEntity.settings);
if (resolvedEntity.definition && resolvedEntity.definition.onEvent) {
resolvedEntity.definition.onEvent(type, data, resolvedEntity.device, resolvedEntity.settings);
}
}
}
module.exports = OnEvent;

45
lib/extension/onEvent.ts Normal file
View File

@ -0,0 +1,45 @@
// @ts-ignore
import zhc from 'zigbee-herdsman-converters';
import ExtensionTS from './extensionts';
/**
* This extension calls the zigbee-herdsman-converters onEvent.
*/
class OnEvent extends ExtensionTS {
override async start(): Promise<void> {
for (const device of this.zigbee.getClients()) {
await 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)));
}
private convertData(data: KeyValue): KeyValue {
return {...data, device: data.device.zhDevice};
}
override async stop(): Promise<void> {
super.stop();
for (const device of this.zigbee.getClients()) {
await this.callOnEvent(device, 'stop', {});
}
}
private async callOnEvent(device: Device, type: string, data: KeyValue): Promise<void> {
zhc.onEvent(type, data, device.zhDevice, device.settings);
if (device.definition?.onEvent) {
await device.definition.onEvent(type, data, device.zhDevice, device.settings);
}
}
}
module.exports = OnEvent;

View File

@ -221,6 +221,7 @@ declare global {
exposes: unknown[] // TODO
ota: unknown // TODO
configure?: (device: ZHDevice, coordinatorEndpoint: ZZHEndpoint, logger: unknown) => Promise<void>;
onEvent?: (type: string, data: KeyValue, device: ZHDevice, settings: KeyValue) => Promise<void>;
}
interface ResolvedDevice {

View File

@ -66,10 +66,7 @@ describe('On event', () => {
await zigbeeHerdsman.events.deviceAnnounce({device});
await flushPromises();
expect(mockOnEvent).toHaveBeenCalledTimes(1);
const call = mockOnEvent.mock.calls[0];
expect(call[0]).toBe('deviceAnnounce')
expect(call[1]).toStrictEqual({device})
expect(call[2]).toBe(device);
expect(mockOnEvent).toHaveBeenCalledWith('deviceAnnounce', {device}, device, settings.getDevice(device.ieeeAddr));
});
it('Should call index onEvent with zigbee event', async () => {
@ -77,9 +74,6 @@ describe('On event', () => {
await zigbeeHerdsman.events.deviceAnnounce({device});
await flushPromises();
expect(zigbeeHerdsmanConverters.onEvent).toHaveBeenCalledTimes(1);
const call = zigbeeHerdsmanConverters.onEvent.mock.calls[0];
expect(call[0]).toBe('deviceAnnounce')
expect(call[1]).toStrictEqual({device})
expect(call[2]).toBe(device);
expect(zigbeeHerdsmanConverters.onEvent).toHaveBeenCalledWith('deviceAnnounce', {device}, device, settings.getDevice(device.ieeeAddr));
});
});