Skip to content

Commit

Permalink
Closes #35: Adding ESLint.
Browse files Browse the repository at this point in the history
This will help contributors of this repository adhere to a more similar
style of code.
  • Loading branch information
amlinger committed Oct 30, 2016
1 parent b516040 commit babf503
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 27 deletions.
35 changes: 35 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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"
}
};
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_js:
before_script:
- npm install
script:
- npm run lint
- npm test
notifications:
email: false
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions spec/telldus-platform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
30 changes: 18 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion src/lib/tdtool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))})
Expand Down
22 changes: 12 additions & 10 deletions src/lib/telldus-accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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))
}
})
Expand All @@ -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.
*/
Expand Down

0 comments on commit babf503

Please sign in to comment.