From cc008db2ad29c77c48e5b88f73c5d4bc1b1267b5 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sat, 11 Apr 2020 18:31:57 +0200 Subject: [PATCH] Refactor --- lib/controller.js | 4 +- lib/extension/deviceEvent.js | 36 ------------------ lib/extension/onEvent.js | 37 +++++++++++++++++++ ...ilability.test.js => availability.test.js} | 2 +- test/{deviceEvent.test.js => onEvent.test.js} | 2 +- 5 files changed, 41 insertions(+), 40 deletions(-) delete mode 100644 lib/extension/deviceEvent.js create mode 100644 lib/extension/onEvent.js rename test/{deviceAvailability.test.js => availability.test.js} (99%) rename test/{deviceEvent.test.js => onEvent.test.js} (98%) diff --git a/lib/controller.js b/lib/controller.js index 20e1be30c..7ede1a86d 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -20,7 +20,7 @@ const ExtensionGroups = require('./extension/groups'); const ExtensionAvailability = require('./extension/availability'); const ExtensionDeviceBind = require('./extension/deviceBind'); const ExtensionDeviceReport = require('./extension/deviceReport'); -const ExtensionDeviceEvent = require('./extension/deviceEvent'); +const ExtensionOnEvent = require('./extension/onEvent'); const ExtensionOTAUpdate = require('./extension/otaUpdate'); class Controller { @@ -43,7 +43,7 @@ class Controller { new ExtensionNetworkMap(...args), new ExtensionGroups(...args), new ExtensionDeviceBind(...args), - new ExtensionDeviceEvent(...args), + new ExtensionOnEvent(...args), new ExtensionOTAUpdate(...args), ]; diff --git a/lib/extension/deviceEvent.js b/lib/extension/deviceEvent.js deleted file mode 100644 index 9f591ef1f..000000000 --- a/lib/extension/deviceEvent.js +++ /dev/null @@ -1,36 +0,0 @@ -const Extension = require('./extension'); -const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters'); - -class DeviceEvent extends Extension { - async onZigbeeStarted() { - for (const device of this.zigbee.getClients()) { - this.callOnEvent(device, 'start', {}); - } - } - - onZigbeeEvent(type, data, resolvedEntity) { - if (data.device && resolvedEntity.definition) { - this.callOnEvent(data.device, type, data, resolvedEntity.definition); - } - } - - async stop() { - for (const device of this.zigbee.getClients()) { - this.callOnEvent(device, 'stop', {}); - } - } - - callOnEvent(device, type, data, definition) { - zigbeeHerdsmanConverters.onEvent(type, data, device); - - if (!definition) { - definition = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID); - } - - if (definition && definition.onEvent) { - definition.onEvent(type, data, device); - } - } -} - -module.exports = DeviceEvent; diff --git a/lib/extension/onEvent.js b/lib/extension/onEvent.js new file mode 100644 index 000000000..f473d9321 --- /dev/null +++ b/lib/extension/onEvent.js @@ -0,0 +1,37 @@ +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.getClients()) { + const resolvedEntity = this.zigbee.resolveEntity(device); + this.callOnEvent(resolvedEntity, 'start', {}); + } + } + + onZigbeeEvent(type, data, resolvedEntity) { + if (resolvedEntity && resolvedEntity.type === 'device') { + this.callOnEvent(resolvedEntity, type, data); + } + } + + async stop() { + for (const device of this.zigbee.getClients()) { + const resolvedEntity = this.zigbee.resolveEntity(device); + this.callOnEvent(resolvedEntity, 'stop', {}); + } + } + + callOnEvent(resolvedEntity, type, data) { + zigbeeHerdsmanConverters.onEvent(type, data, resolvedEntity.device); + + if (resolvedEntity.definition && resolvedEntity.definition.onEvent) { + resolvedEntity.definition.onEvent(type, data, resolvedEntity.device); + } + } +} + +module.exports = OnEvent; diff --git a/test/deviceAvailability.test.js b/test/availability.test.js similarity index 99% rename from test/deviceAvailability.test.js rename to test/availability.test.js index 6dece2c7e..d09f6ee79 100644 --- a/test/deviceAvailability.test.js +++ b/test/availability.test.js @@ -15,7 +15,7 @@ const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {}); const mocksClear = [MQTT.publish, logger.warn, logger.debug]; -describe('Device availability', () => { +describe('Availability', () => { let controller; function getExtension() { diff --git a/test/deviceEvent.test.js b/test/onEvent.test.js similarity index 98% rename from test/deviceEvent.test.js rename to test/onEvent.test.js index 4a5c112a3..96de1aa1a 100644 --- a/test/deviceEvent.test.js +++ b/test/onEvent.test.js @@ -17,7 +17,7 @@ const mappedLivolo = zigbeeHerdsmanConverters.findByZigbeeModel(zigbeeHerdsman.d mappedLivolo.onEvent = mockOnEvent; zigbeeHerdsmanConverters.onEvent = jest.fn(); -describe('Device event', () => { +describe('On event', () => { let controller; const device = zigbeeHerdsman.devices.LIVOLO;