diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..9577589 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,35 @@ +module.exports = { + "env": { + "node": true, + "jasmine": true, + "es6": true + }, + "extends": "eslint:recommended", + "rules": { + "indent": [ + "error", + 2 + ], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "single" + ], + "semi": [ + "error", + "never" + ], + "no-unused-vars": [ + "error", + { "vars": "all", "args": "none" } + ], + "max-len": [ + "error", + 80 + ], + "no-trailing-spaces": "error" + } +}; diff --git a/.travis.yml b/.travis.yml index 1b8ef0e..3724a5d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ node_js: before_script: - npm install script: + - npm run lint - npm test notifications: email: false diff --git a/package.json b/package.json index 55c623d..4dcf444 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "./dist/index.js", "scripts": { "rsync": "fswatch -0 -o src | xargs -0 -n 1 -I {} ./sync.sh", + "lint": "eslint . --ignore-path .gitignore", "test": "node_modules/.bin/jasmine JASMINE_CONFIG_PATH=jasmine.json", "build": "babel src --presets babel-preset-es2015 --out-dir dist", "prepublish": "npm run build" @@ -20,8 +21,9 @@ "devDependencies": { "babel-cli": "^6.6.5", "babel-preset-es2015": "^6.6.0", - "proxyquire": "^1.7.4", - "jasmine": "^2.4.1" + "eslint": "^3.9.0", + "jasmine": "^2.4.1", + "proxyquire": "^1.7.4" }, "repository": { "type": "git", diff --git a/spec/telldus-platform.spec.js b/spec/telldus-platform.spec.js index a0201ef..0b54440 100644 --- a/spec/telldus-platform.spec.js +++ b/spec/telldus-platform.spec.js @@ -3,9 +3,9 @@ const proxyquire = require('proxyquire') describe('test', () => { - let instance, log, injector, homebridgeInjector, tdMocks + let instance, log, homebridgeInjector - const registerInjector = (mocks) => { + const registerInjector = mocks => { homebridgeInjector = proxyquire('../src/index', mocks) log = jasmine.createSpy() diff --git a/src/index.js b/src/index.js index 48d43f7..7bcb50f 100644 --- a/src/index.js +++ b/src/index.js @@ -21,7 +21,8 @@ const modelToAccessoryMap = { const githubRepo = 'https://github.com/amlinger/homebridge-telldus-tdtool' const foundOfTypeString = (type, length, source) => - `Found ${length || 'no'} item${length != 1 ? 's' : ''} of type "${type}" from "${source}".` + `Found ${length || 'no'} item${length != 1 ? 's' : ''} of type "${type}"` + + ` from "${source}".` /** * Platform wrapper that fetches the accessories connected to the @@ -38,35 +39,40 @@ class TelldusTDToolPlatform { this.log('Loading devices...') TDtool.listDevices().then(deviceCandidates => { const devices = deviceCandidates.filter(d => d.type === 'device') - this.log(foundOfTypeString('device', devices.length, 'tdtool --list-devices')) + this.log(foundOfTypeString( + 'device', devices.length, 'tdtool --list-devices')) return devices }).then(devices => { if (this.config.sensors !== undefined && this.config.sensors.length > 0) { - this.log(foundOfTypeString('sensor', this.config.sensors.length, 'config.json')) + this.log(foundOfTypeString( + 'sensor', this.config.sensors.length, 'config.json')) this.config.sensors.forEach((current, index) => { if (this.config.sensors[index].name === undefined) { - this.config.sensors[index].name = `Sensor ${current.id}`; + this.config.sensors[index].name = `Sensor ${current.id}` } }) - return devices.concat(this.config.sensors); + return devices.concat(this.config.sensors) } else { return TDtool.listSensors().then(sensors => { - this.log(foundOfTypeString('sensor', sensors.length, 'tdtool --list-sensors')) - sensors.forEach((current, index) => {sensors[index].name = `Thermometer ${current.id}`}) + this.log(foundOfTypeString('sensor', sensors.length, + 'tdtool --list-sensors')) + sensors.forEach((current, index) => { + sensors[index].name = `Thermometer ${current.id}` + }) return devices.concat(sensors) }) } }).then(accessories => { callback(accessories.map(data => { - const Accessory = modelToAccessoryMap[data.model.split(':')[0]]; + const Accessory = modelToAccessoryMap[data.model.split(':')[0]] if (Accessory === undefined) { this.log( `Model "${data.model.split(':')[0]}" is not supported, try ` + `[${Object.keys(modelToAccessoryMap).join(', ')}]. If you still` + - `have not found what you're looking for, submit a pull ` + + 'have not found what you\'re looking for, submit a pull ' + `at ${githubRepo}`) - return null + return null } return new Accessory(data, this.log, this.homebridge, this.config) @@ -82,5 +88,5 @@ class TelldusTDToolPlatform { */ module.exports = homebridge => { homebridge.registerPlatform( - 'homebridge-telldus-tdtool', "Telldus-TD-Tool", TelldusTDToolPlatform) -}; + 'homebridge-telldus-tdtool', 'Telldus-TD-Tool', TelldusTDToolPlatform) +} diff --git a/src/lib/tdtool.js b/src/lib/tdtool.js index edd27e4..edda999 100644 --- a/src/lib/tdtool.js +++ b/src/lib/tdtool.js @@ -5,7 +5,7 @@ const confParser = require('tellstick.conf-parser') const LINE_DELIMETER = '\n' const PAIR_DELIMETER = '\t' -const exec = require('child_process').exec; +const exec = require('child_process').exec const execute = cmd => new Promise((resolve, reject) => { exec(cmd, (err, stdout, stderr) => err ? reject(stderr) : resolve(stdout))}) diff --git a/src/lib/telldus-accessory.js b/src/lib/telldus-accessory.js index bef4dbd..3343a63 100644 --- a/src/lib/telldus-accessory.js +++ b/src/lib/telldus-accessory.js @@ -22,7 +22,7 @@ class TelldusAccessory { /** * Setup data for accessory, and inject everything used by the class. * - * Dependency injection is used here for easier testing, and avoiding + * Dependency injection is used here for easier testing, and avoiding * global imports at the top of the file as we don't know anything about * Service, Characteristic and other Homebridge things that are injected * into exported provider function. @@ -59,12 +59,13 @@ class TelldusAccessory { * @returns {boolean} */ isStale(sensor) { - const maxAge = this.data.maxAge || this.config.maxAge || SENSOR_MAX_AGE_SECONDS; - return (parseInt(sensor.age) >= parseInt(maxAge)); + const maxAge = this.data.maxAge || this.config.maxAge + || SENSOR_MAX_AGE_SECONDS + return (parseInt(sensor.age) >= parseInt(maxAge)) } /** - * This is a noop action by default, but make sure to log that's been + * This is a noop action by default, but make sure to log that's been * called. * * @param {Function} callback Invoked when logging has been done. @@ -156,7 +157,7 @@ class TelldusDimmer extends TelldusSwitch { /** * Return the last known state of the telldus device, which could either * be ON or OFF, or DIMMED. When it is dimmed, the dimlevel is present - * and that could be used for + * and that could be used for * * @param {Function} callback To be invoked when result is * obtained. @@ -268,7 +269,7 @@ class TelldusThermometer extends TelldusAccessory { * the temperature. Is currently always set to Celcius. */ getTemperatureUnits(callback) { - this.log("Getting temperature units") + this.log('Getting temperature units') // 1 = F and 0 = C callback (null, 0) @@ -283,13 +284,14 @@ class TelldusThermometer extends TelldusAccessory { * @param {object} context */ getTemperature(callback, context) { - this.log(`Checking temperature...`) + this.log('Checking temperature...') TDtool.sensor(this.id, this.log).then(s => { if (s === undefined) { callback(true, null) } else { const isStale = this.isStale(s) - this.log(`Found temperature ${s.temperature} age ${s.age}s stale:${isStale}`) + this.log(`Found temperature ${s.temperature} age ${s.age}s ` + + `stale:${isStale}`) callback(isStale, parseFloat(s.temperature)) } }) @@ -299,8 +301,8 @@ class TelldusThermometer extends TelldusAccessory { * Return the supported services by this Accessory. This only supports * fetching of the temperature. * - * Homebridges default minValue is 0, which can't handle negative temperatures. - * We'll set it to -50 which should cover most usecases. + * Homebridges default minValue is 0, which can't handle negative + * temperatures. We'll set it to -50 which should cover most usecases. * * @return {Array} An array of services supported by this accessory. */