Linting of all fixes

This commit is contained in:
swevictor 2023-08-16 15:32:53 +02:00
parent fc23230490
commit cacd9fc4ba
7 changed files with 55 additions and 34 deletions

View file

@ -66,8 +66,8 @@ class DeviceRegistry {
this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId] = []; this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId] = [];
} }
if ( if (
outputDevice.roomId !== outputDevice.uniqueId outputDevice.roomId !== outputDevice.uniqueId &&
&& !this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId].includes(outputDevice.uniqueId) !this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId].includes(outputDevice.uniqueId)
) { ) {
this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId].push(outputDevice.uniqueId); this.outputDeviceUniqueIdsByRoomId[outputDevice.roomId].push(outputDevice.uniqueId);
logger.verbose( logger.verbose(

View file

@ -1,8 +1,6 @@
const winston = require('winston'); const winston = require('winston');
const { const { colorize, combine, label, printf, timestamp } = winston.format;
colorize, combine, label, printf, timestamp,
} = winston.format;
const Configuration = require('./Configuration'); const Configuration = require('./Configuration');
@ -34,15 +32,17 @@ class Logger {
static getLogLevel() { static getLogLevel() {
const config = Configuration.getOptions(); const config = Configuration.getOptions();
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
const level = (config.logLevel && LEVELS.find((l) => l.startsWith(config.logLevel[0].toLowerCase()))) const level =
|| 'info'; (config.logLevel && LEVELS.find((l) => l.startsWith(config.logLevel[0].toLowerCase()))) ||
'info';
return level; return level;
} }
static shouldLog(logLevel) { static shouldLog(logLevel) {
if (!Logger.shouldLogLookup[logLevel]) { if (!Logger.shouldLogLookup[logLevel]) {
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
Logger.shouldLogLookup[logLevel] = Logger.logLevels().levels[logLevel] <= Logger.logLevels().levels[Logger.getLogLevel()]; Logger.shouldLogLookup[logLevel] =
Logger.logLevels().levels[logLevel] <= Logger.logLevels().levels[Logger.getLogLevel()];
} }
return Logger.shouldLogLookup[logLevel]; return Logger.shouldLogLookup[logLevel];
} }

View file

@ -27,7 +27,8 @@ const TOPIC_TYPES = {
COMMAND: 'set', COMMAND: 'set',
}; };
const getBaseTopic = (/** @type { string } */ uniqueId, /** @type { string } */ mqttDeviceType) => `${discoveryPrefix}/${mqttDeviceType}/${nodeId}/${uniqueId}`; const getBaseTopic = (/** @type { string } */ uniqueId, /** @type { string } */ mqttDeviceType) =>
`${discoveryPrefix}/${mqttDeviceType}/${nodeId}/${uniqueId}`;
const getTopicName = ( const getTopicName = (
/** @type { string } */ uniqueId, /** @type { string } */ uniqueId,
@ -35,9 +36,11 @@ const getTopicName = (
/** @type { import('./types/Mqtt').TopicType } */ topicType, /** @type { import('./types/Mqtt').TopicType } */ topicType,
) => `${getBaseTopic(uniqueId, mqttDeviceType)}/${topicType}`; ) => `${getBaseTopic(uniqueId, mqttDeviceType)}/${topicType}`;
const getButtonEventTopic = (/** @type {string} */ deviceId) => `${getTopicName(deviceId, MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.STATE)}`; const getButtonEventTopic = (/** @type {string} */ deviceId) =>
`${getTopicName(deviceId, MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.STATE)}`;
const getTriggerUniqueId = (/** @type { string } */ uniqueId) => `${uniqueId}_trig`; const getTriggerUniqueId = (/** @type { string } */ uniqueId) => `${uniqueId}_trig`;
const getSceneEventTopic = (/** @type {string} */ sceneId) => `${getTopicName(getTriggerUniqueId(sceneId), MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.STATE)}`; const getSceneEventTopic = (/** @type {string} */ sceneId) =>
`${getTopicName(getTriggerUniqueId(sceneId), MQTT_TYPES.DEVICE_AUTOMATION, TOPIC_TYPES.STATE)}`;
const getSubscribePath = () => `${discoveryPrefix}/+/${nodeId}/#`; const getSubscribePath = () => `${discoveryPrefix}/+/${nodeId}/#`;
const decodeTopicRegexp = new RegExp( const decodeTopicRegexp = new RegExp(

View file

@ -293,7 +293,8 @@ class PlejdApi {
case 6: case 6:
return { return {
name: 'WPH-01', name: 'WPH-01',
description: 'Wireless push button, 4 buttons. 2 channels, on and off buttons for each channel', description:
'Wireless push button, 4 buttons. 2 channels, on and off buttons for each channel',
type: 'device_automation', type: 'device_automation',
dimmable: false, dimmable: false,
broadcastClicks: true, broadcastClicks: true,
@ -481,13 +482,19 @@ class PlejdApi {
}; };
this.deviceRegistry.addOutputDevice(outputDevice); this.deviceRegistry.addOutputDevice(outputDevice);
} catch (error) { } catch (error) {
logger.error(`Error trying to create output device: ${error}`); logger.error(`Error trying to create output device: ${error}`);
logger.warn(`device (from API response) when error happened: ${JSON.stringify(device, null, 2)}`); logger.warn(
logger.warn(`plejdDevice (from API response) when error happened: ${JSON.stringify(plejdDevice, null, 2)}`); `device (from API response) when error happened: ${JSON.stringify(device, null, 2)}`,
);
logger.warn(
`plejdDevice (from API response) when error happened: ${JSON.stringify(
plejdDevice,
null,
2,
)}`,
);
} }
} }
} else { } else {
// The device does not have an output. It can be assumed to be a WPH-01 or a WRT-01 // The device does not have an output. It can be assumed to be a WPH-01 or a WRT-01
@ -530,8 +537,16 @@ class PlejdApi {
} }
} catch (error) { } catch (error) {
logger.error(`Error trying to create input device: ${error}`); logger.error(`Error trying to create input device: ${error}`);
logger.warn(`device (from API response) when error happened: ${JSON.stringify(device, null, 2)}`); logger.warn(
logger.warn(`plejdDevice (from API response) when error happened: ${JSON.stringify(plejdDevice, null, 2)}`); `device (from API response) when error happened: ${JSON.stringify(device, null, 2)}`,
);
logger.warn(
`plejdDevice (from API response) when error happened: ${JSON.stringify(
plejdDevice,
null,
2,
)}`,
);
} }
}); });
} }
@ -547,8 +562,9 @@ class PlejdApi {
const deviceIdsByRoom = this.deviceRegistry.getOutputDeviceIdsByRoomId(roomId); const deviceIdsByRoom = this.deviceRegistry.getOutputDeviceIdsByRoomId(roomId);
const dimmable = deviceIdsByRoom const dimmable =
&& deviceIdsByRoom.some( deviceIdsByRoom &&
deviceIdsByRoom.some(
(deviceId) => this.deviceRegistry.getOutputDevice(deviceId).dimmable, (deviceId) => this.deviceRegistry.getOutputDevice(deviceId).dimmable,
); );

View file

@ -414,7 +414,9 @@ class PlejBLEHandler extends EventEmitter {
async _startGetPlejdDevice() { async _startGetPlejdDevice() {
logger.verbose('Setting up interfacesAdded subscription and discovery filter'); 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({ this.adapter.SetDiscoveryFilter({
UUIDs: new dbus.Variant('as', [PLEJD_SERVICE]), UUIDs: new dbus.Variant('as', [PLEJD_SERVICE]),
@ -873,9 +875,9 @@ class PlejBLEHandler extends EventEmitter {
const plejdTimestampUTC = (decoded.readInt32LE(5) + offsetSecondsGuess) * 1000; const plejdTimestampUTC = (decoded.readInt32LE(5) + offsetSecondsGuess) * 1000;
const diffSeconds = Math.round((plejdTimestampUTC - now.getTime()) / 1000); const diffSeconds = Math.round((plejdTimestampUTC - now.getTime()) / 1000);
if ( if (
bleOutputAddress !== BLE_BROADCAST_DEVICE_ID bleOutputAddress !== BLE_BROADCAST_DEVICE_ID ||
|| Logger.shouldLog('verbose') Logger.shouldLog('verbose') ||
|| Math.abs(diffSeconds) > 60 Math.abs(diffSeconds) > 60
) { ) {
const plejdTime = new Date(plejdTimestampUTC); const plejdTime = new Date(plejdTimestampUTC);
logger.debug( logger.debug(

View file

@ -140,11 +140,11 @@ class PlejdDeviceCommunication extends EventEmitter {
const isDimmable = this.deviceRegistry.getOutputDevice(uniqueOutputId).dimmable; const isDimmable = this.deviceRegistry.getOutputDevice(uniqueOutputId).dimmable;
if ( if (
transition > 1 transition > 1 &&
&& isDimmable isDimmable &&
&& (initialBrightness || initialBrightness === 0) (initialBrightness || initialBrightness === 0) &&
&& (targetBrightness || targetBrightness === 0) (targetBrightness || targetBrightness === 0) &&
&& targetBrightness !== initialBrightness targetBrightness !== initialBrightness
) { ) {
// Transition time set, known initial and target brightness // Transition time set, known initial and target brightness
// Calculate transition interval time based on delta brightness and max steps per second // Calculate transition interval time based on delta brightness and max steps per second
@ -269,8 +269,8 @@ class PlejdDeviceCommunication extends EventEmitter {
if (this.writeQueue.some((item) => item.uniqueOutputId === queueItem.uniqueOutputId)) { if (this.writeQueue.some((item) => item.uniqueOutputId === queueItem.uniqueOutputId)) {
logger.verbose( logger.verbose(
`Skipping ${device.name} (${queueItem.uniqueOutputId}) ` `Skipping ${device.name} (${queueItem.uniqueOutputId}) ` +
+ `${queueItem.command} due to more recent command in queue.`, `${queueItem.command} due to more recent command in queue.`,
); );
// Skip commands if new ones exist for the same uniqueOutputId // Skip commands if new ones exist for the same uniqueOutputId
// still process all messages in order // still process all messages in order

View file

@ -21,8 +21,8 @@
"scripts": { "scripts": {
"lint": "npm run lint:prettier & npm run lint:scripts", "lint": "npm run lint:prettier & npm run lint:scripts",
"lint:fix": "npm run lint:prettier:fix & npm run lint:scripts:fix", "lint:fix": "npm run lint:prettier:fix & npm run lint:scripts:fix",
"lint:prettier": "prettier --check \"**/*.{js*,md}\"", "lint:prettier": "prettier --check --config ../.prettierrc.js \"**/*.{js*,md}\"",
"lint:prettier:fix": "prettier --check --write \"**/*.{js*,md}\"", "lint:prettier:fix": "prettier --check --config ../.prettierrc.js --write \"**/*.{js*,md}\"",
"lint:scripts": "eslint --config ./.eslintrc.js \"**/*.js\"", "lint:scripts": "eslint --config ./.eslintrc.js \"**/*.js\"",
"lint:scripts:fix": "eslint --config ./.eslintrc.js --fix \"**/*.js\"" "lint:scripts:fix": "eslint --config ./.eslintrc.js --fix \"**/*.js\""
} }