From 531aa8ee3e237631292f117eb7cc2245bf104dab Mon Sep 17 00:00:00 2001 From: faanskit Date: Wed, 5 May 2021 19:34:34 +0200 Subject: [PATCH] Fixed errors from npm run lint:fix --- plejd/DeviceRegistry.js | 10 ++++--- plejd/MqttClient.js | 29 +++++++++++------- plejd/PlejdApi.js | 64 ++++++++++++++++++++-------------------- plejd/PlejdBLEHandler.js | 9 ++++-- 4 files changed, 63 insertions(+), 49 deletions(-) diff --git a/plejd/DeviceRegistry.js b/plejd/DeviceRegistry.js index a5efba6..ebc66ec 100644 --- a/plejd/DeviceRegistry.js +++ b/plejd/DeviceRegistry.js @@ -43,7 +43,7 @@ class DeviceRegistry { } output devices in total.`, ); this.outputUniqueIdByBleOutputAddress[`${inputDevice.bleInputAddress}_${inputDevice.input}`] = inputDevice.uniqueId; - }; + } /** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */ addOutputDevice(outputDevice) { @@ -121,7 +121,7 @@ class DeviceRegistry { /** * @returns {import('./types/DeviceRegistry').InputDevice[]} */ - getAllInputDevices() { + getAllInputDevices() { return Object.values(this.inputDevices); } @@ -147,7 +147,7 @@ class DeviceRegistry { /** * @param {string} uniqueInputId */ - getInputDevice(uniqueInputId) { + getInputDevice(uniqueInputId) { return this.inputDevices[uniqueInputId]; } @@ -158,7 +158,9 @@ class DeviceRegistry { /** @returns {import('./types/DeviceRegistry').InputDevice} */ getInputDeviceByBleInputAddress(bleInputAddress, inputButton) { - return this.inputDevices[this.outputUniqueIdByBleOutputAddress[`${bleInputAddress}_${inputButton}`]]; + return this.inputDevices[ + this.outputUniqueIdByBleOutputAddress[`${bleInputAddress}_${inputButton}`] + ]; } /** @returns {string[]} */ diff --git a/plejd/MqttClient.js b/plejd/MqttClient.js index fb1d0ab..c44f685 100644 --- a/plejd/MqttClient.js +++ b/plejd/MqttClient.js @@ -96,7 +96,7 @@ const getInputDeviceTriggerDiscoveryPayload = ( qos: 1, topic: `~/${TOPIC_TYPES.STATE}`, type: 'button_short_press', - subtype: `button_${inputDevice.input+1}`, + subtype: `button_${inputDevice.input + 1}`, device: { identifiers: `${inputDevice.deviceId}`, manufacturer: 'Plejd', @@ -105,7 +105,6 @@ const getInputDeviceTriggerDiscoveryPayload = ( }, }); - const getSceneDeviceTriggerhDiscoveryPayload = ( /** @type {import('./types/DeviceRegistry').OutputDevice} */ sceneDevice, ) => ({ @@ -334,14 +333,23 @@ class MqttClient extends EventEmitter { logger.info( `Discovered ${inputDevice.typeName} (${inputDevice.type}) named ${inputDevice.name} (${inputDevice.bleInputAddress} : ${inputDevice.uniqueId}).`, ); - logger.verbose(`Publishing ${getTopicName(inputDevice.uniqueId, MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.CONFIG)} with payload ${JSON.stringify(inputInputPayload)}`); + logger.verbose( + `Publishing ${getTopicName( + inputDevice.uniqueId, + MQTT_TYPES.DEVICE_AUTOMATION, + TOPIC_TYPES.CONFIG, + )} with payload ${JSON.stringify(inputInputPayload)}`, + ); - this.client.publish(getTopicName(inputDevice.uniqueId, MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.CONFIG), JSON.stringify(inputInputPayload), { - retain: true, - qos: 1, - }); - - }) + this.client.publish( + getTopicName(inputDevice.uniqueId, MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.CONFIG), + JSON.stringify(inputInputPayload), + { + retain: true, + qos: 1, + }, + ); + }); const allSceneDevices = this.deviceRegistry.getAllSceneDevices(); logger.info(`Sending discovery for ${allSceneDevices.length} Plejd scene devices`); @@ -438,10 +446,11 @@ class MqttClient extends EventEmitter { // ); } - buttonPressed(data) { + buttonPressed(data) { logger.verbose(`Button ${data.deviceInput} pressed for deviceId ${data.deviceId}`); this.client.publish(getButtonEventTopic(data.deviceId), `${data.deviceInput}`, { qos: 1 }); } + /** * @param {string} sceneId */ diff --git a/plejd/PlejdApi.js b/plejd/PlejdApi.js index 0b83b9b..db73ce1 100644 --- a/plejd/PlejdApi.js +++ b/plejd/PlejdApi.js @@ -379,42 +379,42 @@ class PlejdApi { // The device does not have an output. It can be assumed to be a WPH-01 or a WRT-01 // Filter inputSettings for available buttons const inputSettings = this.siteDetails.inputSettings.filter( - (x) => x.deviceId === device.deviceId && ((x.buttonType == 'DirectionUp') || (x.buttonType == 'DirectionDown') || (x.buttonType == 'RotateMesh'))); + (x) => x.deviceId === device.deviceId + && (x.buttonType === 'DirectionUp' + || x.buttonType === 'DirectionDown' + || x.buttonType === 'RotateMesh'), + ); - // For each found button, register the device as an inputDevice - inputSettings.forEach((input) => { + // For each found button, register the device as an inputDevice + inputSettings.forEach((input) => { + const bleInputAddress = this.siteDetails.deviceAddress[input.deviceId]; + logger.verbose( + `Found input device (${input.deviceId}), with input ${input.input} having BLE address (${bleInputAddress})`, + ); - const bleInputAddress = this.siteDetails.deviceAddress[input.deviceId]; - logger.verbose( - `Found input device (${input.deviceId}), with input ${input.input} having BLE address (${bleInputAddress})`, - ); + const plejdDevice = this.siteDetails.plejdDevices.find( + (x) => x.deviceId === device.deviceId, + ); - const plejdDevice = this.siteDetails.plejdDevices.find( - (x) => x.deviceId === device.deviceId, - ); + const uniqueInputId = this.deviceRegistry.getUniqueInputId(device.deviceId, input.input); - const uniqueInputId = this.deviceRegistry.getUniqueInputId( - device.deviceId, - input.input, - ); - - const { name: typeName, type } = this._getDeviceType(plejdDevice); - - /** @type {import('types/DeviceRegistry').InputDevice} */ - const inputDevice = { - bleInputAddress: bleInputAddress, - deviceId: device.deviceId, - name: device.title, - input: input.input, - roomId: device.roomId, - type, - typeName, - version: plejdDevice.firmware.version, - uniqueId: uniqueInputId, - }; - this.deviceRegistry.addInputDevice(inputDevice); - }); - }; + const { name: typeName, type } = this._getDeviceType(plejdDevice); + + /** @type {import('types/DeviceRegistry').InputDevice} */ + const inputDevice = { + bleInputAddress, + deviceId: device.deviceId, + name: device.title, + input: input.input, + roomId: device.roomId, + type, + typeName, + version: plejdDevice.firmware.version, + uniqueId: uniqueInputId, + }; + this.deviceRegistry.addInputDevice(inputDevice); + }); + } }); } diff --git a/plejd/PlejdBLEHandler.js b/plejd/PlejdBLEHandler.js index c81e2f9..7c0c0ad 100644 --- a/plejd/PlejdBLEHandler.js +++ b/plejd/PlejdBLEHandler.js @@ -911,7 +911,10 @@ class PlejBLEHandler extends EventEmitter { const inputBleAddress = state; const inputButton = decoded.length > 7 ? decoded.readUInt8(6) : 0; - const sourceDevice = this.deviceRegistry.getInputDeviceByBleInputAddress(inputBleAddress, inputButton); + const sourceDevice = this.deviceRegistry.getInputDeviceByBleInputAddress( + inputBleAddress, + inputButton, + ); if (!sourceDevice) { logger.warn( `Scene with BLE address ${inputBleAddress} could not be found, can't process message`, @@ -919,10 +922,10 @@ class PlejBLEHandler extends EventEmitter { return; } logger.verbose( - `WPH-10 button ${inputButton} at BLE address ${inputBleAddress} was pressed. Unique Id is ${sourceDevice.uniqueId}` + `WPH-10 button ${inputButton} at BLE address ${inputBleAddress} was pressed. Unique Id is ${sourceDevice.uniqueId}`, ); command = COMMANDS.BUTTON_CLICK; - data = { deviceId: sourceDevice.deviceId, deviceInput: sourceDevice.input}; + data = { deviceId: sourceDevice.deviceId, deviceInput: sourceDevice.input }; this.emit(PlejBLEHandler.EVENTS.commandReceived, outputUniqueId, command, data); } else { logger.verbose(