UNPKG

homebridge-raspberry-pi-sensehat

Version:
113 lines 4.66 kB
import { debounce } from './utils.js'; import { exec } from 'child_process'; /** * Platform Accessory "Sensehat Light" */ export class SensehatLightAccessory { platform; accessory; lightService; lightState = { On: false, Brightness: 100, Hue: 0, Saturation: 100, }; prevLightState = { ...this.lightState }; 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 lightbulb service this.lightService = this.accessory.getService(this.platform.Service.Lightbulb) || this.accessory.addService(this.platform.Service.Lightbulb, accessory.context.device.displayName, 'sensehat-switch'); // register handlers for Characteristics this.lightService.getCharacteristic(this.platform.Characteristic.On) .onGet(this.getOn.bind(this)) .onSet(this.setOn.bind(this)); this.lightService.getCharacteristic(this.platform.Characteristic.Brightness) .onGet(this.getBrightness.bind(this)) .onSet(this.setBrightness.bind(this)); this.lightService.getCharacteristic(this.platform.Characteristic.Hue) .onGet(this.getHue.bind(this)) .onSet(this.setHue.bind(this)); this.lightService.getCharacteristic(this.platform.Characteristic.Saturation) .onGet(this.getSaturation.bind(this)) .onSet(this.setSaturation.bind(this)); } // Handle get requests from HomeKit for the lightbulb service async getOn() { await this.getLight(); return this.lightState.On; } async getBrightness() { return this.lightState.Brightness; } async getHue() { return this.lightState.Hue; } async getSaturation() { return this.lightState.Saturation; } // handle set requests from HomeKit for the lightbulb service async setOn(value) { this.lightState.On = value; this.setLight(); } async setBrightness(value) { this.lightState.Brightness = value; this.setLight(); } async setHue(value) { this.lightState.Hue = value; this.setLight(); } async setSaturation(value) { this.lightState.Saturation = value; this.setLight(); } getLight = debounce(async () => { await new Promise((resolve, reject) => { exec(`${this.platform.pythonName} ${this.platform.scriptPath}/getLight.py`, (error, stdout) => { if (!error) { this.lightState.On = (/true/i).test(stdout); this.lightService.updateCharacteristic(this.platform.Characteristic.On, this.lightState.On); this.platform.log.debug(`${this.accessory.context.device.displayName} getLight: ${this.lightState.On}`); resolve(); } else { this.platform.log.error(`${this.accessory.context.device.displayName} getLight failed: ${error}`); reject(error); } }); }); }, 1000); setLight = debounce(async () => { if (this.isStateEqual(this.lightState, this.prevLightState)) { return; } this.prevLightState = { ...this.lightState }; await new Promise((resolve, reject) => { const param = `${this.lightState.On ? 1 : 0} ${this.lightState.Hue} ${this.lightState.Saturation} ${this.lightState.Brightness}`; exec(`${this.platform.pythonName} ${this.platform.scriptPath}/setLight.py ${param}`, (error) => { if (!error) { this.platform.log.debug(`${this.accessory.context.device.displayName} setLight: ${param}`); resolve(); } else { this.platform.log.error(`${this.accessory.context.device.displayName} setLight failed: ${error}`); reject(error); } }); }); }, 100); isStateEqual(a, b) { return Object.keys(a).every(key => a[key] === b[key]); } } //# sourceMappingURL=sensehatLightAccessory.js.map