From 898a9d822f24b1094d730fd0af3e055477cbc48d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20Hagelb=C3=A4ck?= Date: Mon, 1 Feb 2021 21:36:40 +0100 Subject: [PATCH] Lint and style fixes --- plejd/.eslintrc.js | 1 + plejd/Configuration.js | 1 + plejd/MqttClient.js | 19 ++++++++++--------- plejd/PlejdBLE.js | 38 +++++++++++++++++--------------------- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/plejd/.eslintrc.js b/plejd/.eslintrc.js index 15a3195..ff88ace 100644 --- a/plejd/.eslintrc.js +++ b/plejd/.eslintrc.js @@ -23,6 +23,7 @@ module.exports = { function getRules() { return { + 'lines-between-class-members': ['error', 'always', { exceptAfterSingleLine: true }], // Allows modification of properties passed to functions. // Notably used in array.forEach(e => {e.prop = val;}) 'no-param-reassign': ['error', { props: false }], diff --git a/plejd/Configuration.js b/plejd/Configuration.js index 7b3a1d2..690c472 100644 --- a/plejd/Configuration.js +++ b/plejd/Configuration.js @@ -13,6 +13,7 @@ class Configuration { Configuration._options = { ...defaultConfig, ...config }; + // eslint-disable-next-line no-console console.log('Config:', { ...Configuration._options, username: '---scrubbed---', diff --git a/plejd/MqttClient.js b/plejd/MqttClient.js index 88b877d..282bf65 100644 --- a/plejd/MqttClient.js +++ b/plejd/MqttClient.js @@ -109,12 +109,6 @@ class MqttClient extends EventEmitter { }); this.client.on('message', (topic, message) => { - // const command = message.toString(); - const command = - message.toString().substring(0, 1) === '{' - ? JSON.parse(message.toString()) - : message.toString(); - if (startTopics.includes(topic)) { logger.info('Home Assistant has started. lets do discovery.'); this.emit('connected'); @@ -124,6 +118,10 @@ class MqttClient extends EventEmitter { const device = this.deviceRegistry.getDevice(decodedTopic.id); const deviceName = device ? device.name : ''; + const command = message.toString().substring(0, 1) === '{' + ? JSON.parse(message.toString()) + : message.toString(); + switch (decodedTopic.command) { case 'set': logger.verbose( @@ -142,7 +140,11 @@ class MqttClient extends EventEmitter { case 'config': case 'availability': logger.verbose( - `Sent mqtt ${decodedTopic.command} command for ${decodedTopic.type}, ${deviceName} (${decodedTopic.id}). ${decodedTopic.command === 'availability' ? message : ''}`, + `Sent mqtt ${decodedTopic.command} command for ${ + decodedTopic.type + }, ${deviceName} (${decodedTopic.id}). ${ + decodedTopic.command === 'availability' ? message : '' + }`, ); break; default: @@ -172,8 +174,7 @@ class MqttClient extends EventEmitter { this.deviceRegistry.allDevices.forEach((device) => { logger.debug(`Sending discovery for ${device.name}`); - const payload = - device.type === 'switch' ? getSwitchPayload(device) : getDiscoveryPayload(device); + const payload = device.type === 'switch' ? getSwitchPayload(device) : getDiscoveryPayload(device); logger.info( `Discovered ${device.type} (${device.typeName}) named ${device.name} with PID ${device.id}.`, ); diff --git a/plejd/PlejdBLE.js b/plejd/PlejdBLE.js index 353c6a2..ec77f35 100644 --- a/plejd/PlejdBLE.js +++ b/plejd/PlejdBLE.js @@ -149,9 +149,7 @@ class PlejBLE extends EventEmitter { logger.verbose('All active BLE device connections cleaned up.'); logger.verbose('Setting up interfacesAdded subscription and discovery filter'); - this.objectManager.on('InterfacesAdded', (path, interfaces) => - this.onInterfacesAdded(path, interfaces), - ); + this.objectManager.on('InterfacesAdded', (path, interfaces) => this.onInterfacesAdded(path, interfaces)); this.adapter.SetDiscoveryFilter({ UUIDs: new dbus.Variant('as', [PLEJD_SERVICE]), @@ -313,11 +311,11 @@ class PlejBLE extends EventEmitter { const isDimmable = this.deviceRegistry.getDevice(deviceId).dimmable; if ( - transition > 1 && - isDimmable && - (initialBrightness || initialBrightness === 0) && - (targetBrightness || targetBrightness === 0) && - targetBrightness !== initialBrightness + transition > 1 + && isDimmable + && (initialBrightness || initialBrightness === 0) + && (targetBrightness || targetBrightness === 0) + && targetBrightness !== initialBrightness ) { // Transition time set, known initial and target brightness // Calculate transition interval time based on delta brightness and max steps per second @@ -452,23 +450,21 @@ class PlejBLE extends EventEmitter { this.characteristics.lastData.StartNotify(); } - async throttledInit(delay) { + async throttledInit(delayMs) { if (this.initInProgress) { logger.debug( 'ThrottledInit already in progress. Skipping this call and returning existing promise.', ); return this.initInProgress; } - this.initInProgress = new Promise((resolve) => - setTimeout(async () => { - const result = await this.init().catch((err) => { - logger.error('TrottledInit exception calling init(). Will re-throw.', err); - throw err; - }); - this.initInProgress = null; - resolve(result); - }, delay), - ); + this.initInProgress = new Promise((resolve) => setTimeout(async () => { + const result = await this.init().catch((err) => { + logger.error('TrottledInit exception calling init(). Will re-throw.', err); + throw err; + }); + this.initInProgress = null; + resolve(result); + }, delayMs)); return this.initInProgress; } @@ -562,8 +558,8 @@ class PlejBLE extends EventEmitter { if (this.writeQueue.some((item) => item.deviceId === queueItem.deviceId)) { logger.verbose( - `Skipping ${deviceName} (${queueItem.deviceId}) ` + - `${queueItem.log} due to more recent command in queue.`, + `Skipping ${deviceName} (${queueItem.deviceId}) ` + + `${queueItem.log} due to more recent command in queue.`, ); // Skip commands if new ones exist for the same deviceId // still process all messages in order