From e71485cd50bc890ccc7d987b27c0887d949481f8 Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sun, 5 Apr 2020 20:36:09 +0200 Subject: [PATCH] Refactor --- lib/controller.js | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/controller.js b/lib/controller.js index 07d7ab32..e7ef3d1c 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -231,22 +231,25 @@ class Controller { } async publishEntityState(IDorName, payload, stateChangeReason=null) { - const entity = this.zigbee.resolveEntity(IDorName); - if (!entity || !entity.settings) { + const resolvedEntity = this.zigbee.resolveEntity(IDorName); + if (!resolvedEntity || !resolvedEntity.settings) { logger.error(`'${IDorName}' does not exist, skipping publish`); return; } - if (entity.type === 'device' && settings.get().advanced.last_seen !== 'disable' && entity.device.lastSeen) { - payload.last_seen = utils.formatDate(entity.device.lastSeen, settings.get().advanced.last_seen); + const isDevice = resolvedEntity.type === 'device'; + + if (isDevice && settings.get().advanced.last_seen !== 'disable' && resolvedEntity.device.lastSeen) { + payload.last_seen = utils.formatDate(resolvedEntity.device.lastSeen, settings.get().advanced.last_seen); } let messagePayload = {...payload}; - const currentState = this.state.exists(entity.settings.ID) ? this.state.get(entity.settings.ID) : {}; + const currentState = this.state.exists(resolvedEntity.settings.ID) ? + this.state.get(resolvedEntity.settings.ID) : {}; const newState = objectAssignDeep.noMutate(currentState, payload); // Update state cache with new state. - this.state.set(entity.settings.ID, newState, stateChangeReason); + this.state.set(resolvedEntity.settings.ID, newState, stateChangeReason); if (settings.get().advanced.cache_state) { // Add cached state to payload @@ -255,50 +258,49 @@ class Controller { const deviceOptions = settings.get().device_options; const options = { - retain: utils.getObjectsProperty([entity.settings, deviceOptions], 'retain', false), - qos: utils.getObjectsProperty([entity.settings, deviceOptions], 'qos', 0), + retain: utils.getObjectsProperty([resolvedEntity.settings, deviceOptions], 'retain', false), + qos: utils.getObjectsProperty([resolvedEntity.settings, deviceOptions], 'qos', 0), }; - const retention = utils.getObjectsProperty([entity.settings, deviceOptions], 'retention', false); + const retention = utils.getObjectsProperty([resolvedEntity.settings, deviceOptions], 'retention', false); if (retention !== false) { options.properties = {messageExpiryInterval: retention}; } - if (entity.type === 'device' && settings.get().mqtt.include_device_information) { - const device = this.zigbee.getDeviceByIeeeAddr(entity.device.ieeeAddr); + if (isDevice && settings.get().mqtt.include_device_information) { const attributes = [ 'ieeeAddr', 'networkAddress', 'type', 'manufacturerID', 'manufacturerName', 'powerSource', 'applicationVersion', 'stackVersion', 'zclVersion', 'hardwareVersion', 'dateCode', 'softwareBuildID', ]; messagePayload.device = { - friendlyName: entity.name, - model: entity.definition ? entity.definition.model : 'unknown', + friendlyName: resolvedEntity.name, + model: resolvedEntity.definition ? resolvedEntity.definition.model : 'unknown', }; - attributes.forEach((a) => messagePayload.device[a] = device[a]); + attributes.forEach((a) => messagePayload.device[a] = resolvedEntity.device[a]); } // filter mqtt message attributes if (deviceOptions.filtered_attributes) { deviceOptions.filtered_attributes.forEach((a) => delete messagePayload[a]); } - if (entity.settings.filtered_attributes) { - entity.settings.filtered_attributes.forEach((a) => delete messagePayload[a]); + if (resolvedEntity.settings.filtered_attributes) { + resolvedEntity.settings.filtered_attributes.forEach((a) => delete messagePayload[a]); } - this.eventBus.emit('publishEntityState', {payload: messagePayload, entity}); + this.eventBus.emit('publishEntityState', {payload: messagePayload, entity: resolvedEntity}); if (Object.entries(messagePayload).length) { if (settings.get().experimental.output === 'attribute_and_json') { - await this.mqtt.publish(entity.name, JSON.stringify(messagePayload), options); - await this.iteratePayloadAttributeOutput(`${entity.name}/`, messagePayload, options); + await this.mqtt.publish(resolvedEntity.name, JSON.stringify(messagePayload), options); + await this.iteratePayloadAttributeOutput(`${resolvedEntity.name}/`, messagePayload, options); } else if (settings.get().experimental.output === 'json') { - await this.mqtt.publish(entity.name, JSON.stringify(messagePayload), options); + await this.mqtt.publish(resolvedEntity.name, JSON.stringify(messagePayload), options); } else { /* istanbul ignore else */ if (settings.get().experimental.output === 'attribute') { - await this.iteratePayloadAttributeOutput(`${entity.name}/`, messagePayload, options); + await this.iteratePayloadAttributeOutput(`${resolvedEntity.name}/`, messagePayload, options); } } }