homebridge-raspberry-pi-sensehat
Version:
A Homebridge plugin for Raspberry Pi SenseHAT
73 lines • 3.92 kB
JavaScript
import { debounce } from './utils.js';
import { exec } from 'child_process';
/**
* Platform Accessory "Sensehat Sensor"
*/
export class SensehatSensorAccessory {
platform;
accessory;
temperatureService;
humidityService;
pressureService;
temperature = 0;
humidity = 0;
pressure = 700;
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, 'SenseHat')
.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, 'sensehat-temperature');
this.temperatureService.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.onGet(this.getCurrentTemperature.bind(this));
// add a humidity sensor service
this.humidityService = this.accessory.getService(this.platform.Service.HumiditySensor) ||
this.accessory.addService(this.platform.Service.HumiditySensor, accessory.context.device.displayName, 'sensehat-humidity');
this.humidityService.getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
.onGet(this.getCurrentHumidity.bind(this));
// add a pressure sensor service (Custom Service)
this.pressureService = this.accessory.getService(this.platform.CustomService.AirPressureSensor) ||
this.accessory.addService(this.platform.CustomService.AirPressureSensor, accessory.context.device.displayName, 'sensehat-pressure');
this.pressureService.getCharacteristic(this.platform.CustomCharacteristic.AirPressure)
.onGet(this.getCurrentPressure.bind(this));
}
// handle get requests from HomeKit for the temperature, humidity and pressure sensor
async getCurrentTemperature() {
await this.getSensor();
return this.temperature;
}
async getCurrentHumidity() {
return this.humidity;
}
async getCurrentPressure() {
return this.pressure;
}
getSensor = debounce(async () => {
await new Promise((resolve, reject) => {
exec(`${this.platform.pythonName} ${this.platform.scriptPath}/getSensor.py`, (error, stdout) => {
if (!error) {
const values = stdout.split(' ');
this.temperature = parseFloat(values[0]);
this.humidity = parseFloat(values[1]);
this.pressure = parseFloat(values[2]);
this.temperatureService.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, this.temperature);
this.humidityService.updateCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity, this.humidity);
this.pressureService.updateCharacteristic(this.platform.CustomCharacteristic.AirPressure, this.pressure || 700);
this.platform.log.debug(`${this.accessory.context.device.displayName} getSensor: ${this.temperature}C ${this.humidity}% ${this.pressure}hPa`);
resolve();
}
else {
this.platform.log.error(`${this.accessory.context.device.displayName} getSensor failed: ${error}`);
reject(error);
}
});
});
}, 1000);
}
//# sourceMappingURL=sensehatSensorAccessory.js.map