UNPKG

homebridge-raspberry-pi-sensehat

Version:
48 lines 2.35 kB
import { debounce } from './utils.js'; import { exec } from 'child_process'; /** * Platform Accessory "Raspberry Temperature" */ export class RaspberryTemperatureAccessory { 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, 'Raspberry') .setCharacteristic(this.platform.Characteristic.Model, 'Pi') .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, 'raspberry-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) => { exec(`${this.platform.pythonName} ${this.platform.scriptPath}/getTemperature.py`, (error, stdout) => { if (!error) { this.temperature = parseFloat(stdout); 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=raspberryTemperatureAccessory.js.map