homebridge-am2320-plugin
Version:
Provides the AM2320 Sensor as accessories to your homebridge.
113 lines • 3.98 kB
JavaScript
"use strict";
let hap;
class AM2320HumiditySensor {
constructor(log, config /*, api: API*/) {
this.humidity = 0;
this.log = log;
this.name = config.name;
this.humidityService = new hap.Service.HumiditySensor(this.name);
this.humidityService
.getCharacteristic(hap.Characteristic.CurrentRelativeHumidity)
.on("get" /* GET */, async (callback) => {
try {
const sensorData = await readSensorData();
this.humidity = sensorData.humidity;
}
catch (err) {
this.log.error(err);
}
finally {
callback(undefined, this.humidity);
}
});
this.informationService = new hap.Service.AccessoryInformation()
.setCharacteristic(hap.Characteristic.Manufacturer, 'adafruit')
.setCharacteristic(hap.Characteristic.Model, 'AM2320');
log.info('AM2320HumiditySensor finished initializing!');
}
/*
* This method is optional to implement. It is called when HomeKit ask to identify the accessory.
* Typical this only ever happens at the pairing process.
*/
identify() {
this.log('Identify!');
}
/*
* This method is called directly after creation of this instance.
* It should return all services which should be added to the accessory.
*/
getServices() {
return [
this.informationService,
this.humidityService,
];
}
}
class AM2320TemperatureSensor {
constructor(log, config /*, api: API*/) {
this.temperature = 0;
this.log = log;
this.name = config.name;
this.temperatureService = new hap.Service.TemperatureSensor(this.name);
this.temperatureService
.getCharacteristic(hap.Characteristic.CurrentTemperature)
.on("get" /* GET */, async (callback) => {
try {
const sensorData = await readSensorData();
this.temperature = sensorData.temperature;
}
catch (err) {
this.log.error(err);
}
finally {
callback(undefined, this.temperature);
}
});
this.informationService = new hap.Service.AccessoryInformation()
.setCharacteristic(hap.Characteristic.Manufacturer, 'adafruit')
.setCharacteristic(hap.Characteristic.Model, 'AM2320');
log.info('AM2320TemperatureSensor finished initializing!');
}
/*
* This method is optional to implement. It is called when HomeKit ask to identify the accessory.
* Typical this only ever happens at the pairing process.
*/
identify() {
this.log('Identify!');
}
/*
* This method is called directly after creation of this instance.
* It should return all services which should be added to the accessory.
*/
getServices() {
return [
this.informationService,
this.temperatureService,
];
}
}
async function readSensorData() {
const sensorScriptFile = `${__dirname}/lib/read-am2320-sensor.py`;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const spawn = require('child_process').spawn;
const process = spawn('python3', [sensorScriptFile]);
return new Promise((resolve, reject) => {
process.stdout.on('data', data => {
try {
resolve(JSON.parse(data.toString()));
}
catch (err) {
reject(err.toString());
}
});
process.stderr.on('data', err => {
reject(err.toString());
});
});
}
module.exports = (api) => {
hap = api.hap;
api.registerAccessory('AM2320TemperatureSensor', AM2320TemperatureSensor);
api.registerAccessory('AM2320HumiditySensor', AM2320HumiditySensor);
};
//# sourceMappingURL=index.js.map