From 9146f9c7d353989b8a7371490db0b00bf3930718 Mon Sep 17 00:00:00 2001 From: swevictor Date: Fri, 6 Oct 2023 14:11:23 +0200 Subject: [PATCH] Lint and code style fixes --- plejd/DeviceRegistry.js | 9 ++++++--- plejd/PlejdApi.js | 8 +++++--- plejd/PlejdBLEHandler.js | 25 +++++++++++++++++-------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/plejd/DeviceRegistry.js b/plejd/DeviceRegistry.js index 173ab09..491b92a 100644 --- a/plejd/DeviceRegistry.js +++ b/plejd/DeviceRegistry.js @@ -55,9 +55,13 @@ class DeviceRegistry { /** @param outputDevice {import('types/DeviceRegistry').OutputDevice} */ addOutputDevice(outputDevice) { - const alreadyExistingBLEDevice = this.getOutputDeviceByBleOutputAddress(outputDevice.bleOutputAddress); + const alreadyExistingBLEDevice = this.getOutputDeviceByBleOutputAddress( + outputDevice.bleOutputAddress, + ); if (alreadyExistingBLEDevice) { - logger.warn(`Device with output id ${outputDevice.bleOutputAddress} already exists named ${alreadyExistingBLEDevice.name}. These two devices are probably grouped in the Plejd app. If this seems to be an error, please log a GitHub issue.`); + logger.warn( + `Device with output id ${outputDevice.bleOutputAddress} already exists named ${alreadyExistingBLEDevice.name}. These two devices are probably grouped in the Plejd app. If this seems to be an error, please log a GitHub issue.`, + ); logger.info(`NOT adding ${outputDevice.name} to device registry`); logger.verbose(`Details of device NOT added: ${JSON.stringify(outputDevice)}`); return; @@ -195,7 +199,6 @@ class DeviceRegistry { return this.mainBleIdByDeviceId[deviceId]; } - /** * @param {string } deviceId The physical device serial number * @return {import('./types/ApiSite').Device} diff --git a/plejd/PlejdApi.js b/plejd/PlejdApi.js index ebe0496..fffcc2f 100644 --- a/plejd/PlejdApi.js +++ b/plejd/PlejdApi.js @@ -398,7 +398,7 @@ class PlejdApi { broadcastClicks: false, }; // PLEASE CREATE AN ISSUE WITH THE HARDWARE ID if you own one of these devices! - // case + // case // return { // name: 'DWN-02', // description: 'Smart tunable downlight with a built-in dimmer function, 8W', @@ -406,7 +406,7 @@ class PlejdApi { // dimmable: true, // broadcastClicks: false, // }; - // case + // case // return { // name: 'OUT-01', // description: 'Outdoor wall light with built-in LED, 2x5W', @@ -415,7 +415,9 @@ class PlejdApi { // broadcastClicks: false, // }; default: - throw new Error(`Unknown device type with hardware id ${plejdDevice.hardwareId}. --- PLEASE POST THIS AND THE NEXT LOG ROWS to https://github.com/icanos/hassio-plejd/issues/ --- `); + throw new Error( + `Unknown device type with hardware id ${plejdDevice.hardwareId}. --- PLEASE POST THIS AND THE NEXT LOG ROWS to https://github.com/icanos/hassio-plejd/issues/ --- `, + ); } } diff --git a/plejd/PlejdBLEHandler.js b/plejd/PlejdBLEHandler.js index 2c6b504..7f50d55 100644 --- a/plejd/PlejdBLEHandler.js +++ b/plejd/PlejdBLEHandler.js @@ -778,10 +778,14 @@ class PlejBLEHandler extends EventEmitter { } this.connectedDevice = device.device; - this.connectedDeviceId = this.deviceRegistry.getMainBleIdByDeviceId(this.connectedDevice.deviceId); + this.connectedDeviceId = this.deviceRegistry.getMainBleIdByDeviceId( + this.connectedDevice.deviceId, + ); logger.verbose('The connected Plejd device has the right charecteristics!'); - logger.info(`Connected to Plejd device ${this.connectedDevice.title} (${this.connectedDevice.deviceId}, BLE id ${this.connectedDeviceId}).`); + logger.info( + `Connected to Plejd device ${this.connectedDevice.title} (${this.connectedDevice.deviceId}, BLE id ${this.connectedDeviceId}).`, + ); await this._authenticate(); @@ -820,13 +824,18 @@ class PlejBLEHandler extends EventEmitter { // Bytes 2-3 is Command/Request const cmd = decoded.readUInt16BE(3); - const state = decoded.length > PAYLOAD_POSITION_OFFSET ? decoded.readUInt8(PAYLOAD_POSITION_OFFSET) : 0; + const state = + decoded.length > PAYLOAD_POSITION_OFFSET ? decoded.readUInt8(PAYLOAD_POSITION_OFFSET) : 0; - const dim = decoded.length > DIM_LEVEL_POSITION_OFFSET ? decoded.readUInt8(DIM_LEVEL_POSITION_OFFSET) : 0; + const dim = + decoded.length > DIM_LEVEL_POSITION_OFFSET ? decoded.readUInt8(DIM_LEVEL_POSITION_OFFSET) : 0; if (Logger.shouldLog('silly')) { // Full dim level is 2 bytes, we could potentially use this - const dimFull = decoded.length > DIM_LEVEL_POSITION_OFFSET ? decoded.readUInt16LE(DIM_LEVEL_POSITION_OFFSET - 1) : 0; + const dimFull = + decoded.length > DIM_LEVEL_POSITION_OFFSET + ? decoded.readUInt16LE(DIM_LEVEL_POSITION_OFFSET - 1) + : 0; logger.silly(`Dim: ${dim.toString(16)}, full precision: ${dimFull.toString(16)}`); } @@ -877,7 +886,6 @@ class PlejBLEHandler extends EventEmitter { data = { sceneId: scene.uniqueId }; this.emit(PlejBLEHandler.EVENTS.commandReceived, outputUniqueId, command, data); } else if (cmd === BLE_CMD_TIME_UPDATE) { - if (decoded.length < PAYLOAD_POSITION_OFFSET + 4) { if (Logger.shouldLog('debug')) { // decoded.toString() could potentially be expensive @@ -886,13 +894,14 @@ class PlejBLEHandler extends EventEmitter { // ignore the notification since too small return; } - + const now = new Date(); // Guess Plejd timezone based on HA time zone const offsetSecondsGuess = now.getTimezoneOffset() * 60 + 250; // Todo: 4 min off // Plejd reports local unix timestamp adjust to local time zone - const plejdTimestampUTC = (decoded.readInt32LE(PAYLOAD_POSITION_OFFSET) + offsetSecondsGuess) * 1000; + const plejdTimestampUTC = + (decoded.readInt32LE(PAYLOAD_POSITION_OFFSET) + offsetSecondsGuess) * 1000; const diffSeconds = Math.round((plejdTimestampUTC - now.getTime()) / 1000); if ( bleOutputAddress !== BLE_BROADCAST_DEVICE_ID ||