Lint and style fixes

This commit is contained in:
Victor Hagelbäck 2021-02-01 21:36:40 +01:00
parent 4e7ec6a1da
commit 898a9d822f
4 changed files with 29 additions and 30 deletions

View file

@ -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 }],

View file

@ -13,6 +13,7 @@ class Configuration {
Configuration._options = { ...defaultConfig, ...config };
// eslint-disable-next-line no-console
console.log('Config:', {
...Configuration._options,
username: '---scrubbed---',

View file

@ -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}.`,
);

View file

@ -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