homebridge-raspberry-pi-sensehat
Version:
A Homebridge plugin for Raspberry Pi SenseHAT
49 lines • 2.49 kB
JavaScript
import { debounce } from './utils.js';
import { exec } from 'child_process';
/**
* Platform Accessory "Synology Temperature"
*/
export class SynologyTemperatureAccessory {
platform;
accessory;
temperatureService;
temperature = 0;
constructor(platform, accessory) {
this.platform = platform;
this.accessory = accessory;
// set accessory information
this.accessory.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Synology')
.setCharacteristic(this.platform.Characteristic.Model, 'DiskStation')
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, '1.0')
.setCharacteristic(this.platform.Characteristic.SerialNumber, '1.0');
// create a temperature sensor service
this.temperatureService = this.accessory.getService(this.platform.Service.TemperatureSensor) ||
this.accessory.addService(this.platform.Service.TemperatureSensor, accessory.context.device.displayName, 'synology-temperature');
this.temperatureService.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.onGet(this.getCurrentTemperature.bind(this));
}
// handle get requests from HomeKit for the temperature sensor
async getCurrentTemperature() {
await this.getTemperature();
return this.temperature;
}
getTemperature = debounce(async () => {
await new Promise((resolve, reject) => {
const cmd = this.platform.config.synologyCmd || 'cat /sys/devices/platform/coretemp.0/hwmon/hwmon0/temp1_input';
exec(`ssh ${this.platform.config.synologyUser}@${this.platform.config.synologyIp} ${cmd}`, (error, stdout) => {
if (!error) {
this.temperature = parseFloat(stdout) / 1000;
this.temperatureService.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.temperature);
this.platform.log.debug(`${this.accessory.context.device.displayName} getTemperature: ${this.temperature}C`);
resolve();
}
else {
this.platform.log.error(`${this.accessory.context.device.displayName} getTemperature failed: ${error}`);
reject(error);
}
});
});
}, 1000);
}
//# sourceMappingURL=synologyTemperatureAccessory.js.map