Skip to content

Commit

Permalink
rewriting plugin, getting rid of ts / making sensors optional
Browse files Browse the repository at this point in the history
  • Loading branch information
orangecoding committed Mar 10, 2021
1 parent a2688fb commit 2c81008
Show file tree
Hide file tree
Showing 13 changed files with 6,522 additions and 250 deletions.
20 changes: 8 additions & 12 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"parser": "@typescript-eslint/parser",
"parser": "babel-eslint",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended" // uses the recommended rules from the @typescript-eslint/eslint-plugin
"eslint:recommended"
],
"parserOptions": {
"ecmaVersion": 2018,
Expand All @@ -12,12 +10,15 @@
"ignorePatterns": [
"dist"
],
"env" : {
"node" : true,
"es6": true
},
"rules": {
"quotes": ["warn", "single"],
"indent": ["warn", 2, { "SwitchCase": 1 }],
"semi": ["off"],
"dot-notation": "off",
"eqeqeq": "warn",
"eqeqeq": [2, "allow-null"],
"curly": ["warn", "all"],
"brace-style": ["warn"],
"prefer-arrow-callback": ["warn"],
Expand All @@ -26,11 +27,6 @@
"comma-spacing": ["error"],
"no-multi-spaces": ["warn", { "ignoreEOLComments": true }],
"no-trailing-spaces": ["warn"],
"lines-between-class-members": ["warn", "always", {"exceptAfterSingleLine": true}],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/semi": ["warn"],
"@typescript-eslint/member-delimiter-style": ["warn"]
"lines-between-class-members": ["warn", "always", {"exceptAfterSingleLine": true}]
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Add to the `accessories` field of your Homebridge `config.json` file:
}
```

## Note
The fields in `sensor_names` are optional. You however need to set at least 1. If you do not supply `temperature` for instance, no temperature sensor will be registered.

#### Influx config
The `influx` configuration object is passed as-is to the `influx` npm library, so you can use all the options it supports.
See [here](https://node-influx.github.io/class/src/index.js~InfluxDB.html#instance-constructor-constructor)

Expand Down
2 changes: 0 additions & 2 deletions dist/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion dist/index.d.ts.map

This file was deleted.

83 changes: 0 additions & 83 deletions dist/index.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/index.js.map

This file was deleted.

120 changes: 120 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
let Service, Characteristic;

const InfluxDB = require('influx');

const TEMPERATURE_SENSOR_NAME = 'temperature';
const HUMIDITY_SENSOR_NAME = 'humidity';

const getLastMeasurement = (influx, service, schema, cb) => {
influx
.query(`SELECT LAST("${schema[service].field}") FROM ${schema[service].measurement}`)
.then(result => cb(null, result[0] == null ? -1 : result[0].last))
.catch(err => cb(err));
};

const round = (value, decimal = 0) => {
return Math.round((value + Number.EPSILON) * 10 ** decimal) / 10 ** decimal;
};

class HomebridgeInflux{
constructor(log, config){
this.log = log;

this.name = config['name'];
this.manufacturer = config['manufacturer'] || 'Christian Kellner';
this.model = config['model'] || 'homebridge-influx';
this.serial = config['serial'] || '1';

/**
* Example:
* "sensor_names": {
"temperature": "Temperature Sensor",
"humidity": "Humidity Sensor"
},
* NOTE: At least one of the sensors must be set
* @type {{[p: string]: *}}
*/
this.sensor_names = { ...config['sensor_names'] };
this.schema = { ...config['schema'] };

this.influx = new InfluxDB.InfluxDB({ ...config['influx'] });
}


getRemoteState = (service, callback) => {
getLastMeasurement(
this.influx,
service,
this.schema,
(influxError, value) => {

if (influxError) {
this.log(influxError);
return callback(new Error(influxError));
}
const v = round(value, 1);

const takenService = service === TEMPERATURE_SENSOR_NAME ? this.temperatureService : this.humidityService;

takenService.setCharacteristic(Characteristic.Name, this.sensor_names[service]);
takenService.setCharacteristic(service === TEMPERATURE_SENSOR_NAME ? Characteristic.CurrentTemperature : Characteristic.CurrentRelativeHumidity, v);
return callback(null, v);
}
);
}

// Homekit-specific getters
getTemperatureState = (callback) => {
this.getRemoteState(TEMPERATURE_SENSOR_NAME, callback);
}

getHumidityState = (callback) => {
this.getRemoteState(HUMIDITY_SENSOR_NAME, callback);
}

getServices = () => {
const informationService = new Service.AccessoryInformation();
const result = [informationService];
const sensorKeys = Object.keys(this.sensor_names);
let sensorSet = false;

informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);

if(sensorKeys.indexOf(TEMPERATURE_SENSOR_NAME) !== -1) {
this.temperatureService = new Service.TemperatureSensor(this.name);
this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({minValue: -100, maxValue: 100})
.on('get', this.getTemperatureState);
result.push(this.temperatureService);
sensorSet = true;
this.log.debug('Configured temperature sensor');
}

if(sensorKeys.indexOf(HUMIDITY_SENSOR_NAME) !== -1) {
this.humidityService = new Service.HumiditySensor(this.name);
this.humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.setProps({minValue: 0, maxValue: 100})
.on('get', this.getHumidityState);
result.push(this.humidityService);
sensorSet = true;
this.log.debug('Configured humidity sensor');
}

if(!sensorSet){
this.log.error('You need to set at least 1 sensor in the sensor_names config');
}

return result;
};
}

module.exports = homebridge => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-influx', 'Homebridge-Influx', HomebridgeInflux);
};
5 changes: 2 additions & 3 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"watch": [
"src"
"./"
],
"ext": "ts",
"ignore": [],
"exec": "tsc && homebridge -I -D",
"exec": "homebridge -I -D",
"signal": "SIGTERM",
"env": {
"NODE_OPTIONS": "--trace-warnings"
Expand Down
Loading

0 comments on commit 2c81008

Please sign in to comment.