UNPKG

homebridge-hatch-baby-rest

Version:
51 lines (50 loc) 2.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LightAndSoundMachineAccessory = void 0; const hap_1 = require("./hap"); const operators_1 = require("rxjs/operators"); const rxjs_1 = require("rxjs"); const util_1 = require("./util"); const sound_machine_1 = require("./sound-machine"); class LightAndSoundMachineAccessory extends sound_machine_1.SoundMachineAccessory { constructor(light, accessory) { super(light, accessory); const { Service, Characteristic } = hap_1.hap, { name } = light, lightService = this.getService(Service.Lightbulb, 'Light'), onOffService = this.getService(Service.Switch), onHsbSet = new rxjs_1.Subject(), context = accessory.context, onBrightness = light.onBrightness.pipe((0, operators_1.startWith)(context.b || 0)); context.h = context.h || 0; context.s = context.s || 0; context.b = context.b || 0; light.onHue.subscribe((h) => (context.h = h)); light.onSaturation.subscribe((s) => (context.s = s)); onBrightness.subscribe((b) => (context.b = b)); onHsbSet.pipe((0, operators_1.debounceTime)(100)).subscribe(() => { light.setHsb(context); }); this.registerCharacteristic(onOffService.getCharacteristic(Characteristic.On), light.onIsPowered, (on) => { (0, util_1.logInfo)(`Turning ${on ? 'on' : 'off'} ${name}`); light.setPower(on); }); this.registerCharacteristic(lightService.getCharacteristic(Characteristic.On), onBrightness.pipe((0, operators_1.map)((brightness) => Boolean(brightness)))); this.registerCharacteristic(lightService.getCharacteristic(Characteristic.Hue), light.onHue, (hue) => { context.h = hue; onHsbSet.next(null); }); this.registerCharacteristic(lightService.getCharacteristic(Characteristic.Saturation), light.onSaturation, (saturation) => { context.s = saturation; onHsbSet.next(null); }); this.registerCharacteristic(lightService.getCharacteristic(Characteristic.Brightness), onBrightness, (brightness) => { context.b = brightness; onHsbSet.next(null); }); if (light.onBatteryLevel) { const batteryService = this.getService(Service.Battery, name); this.registerCharacteristic(batteryService.getCharacteristic(Characteristic.BatteryLevel), light.onBatteryLevel); this.registerCharacteristic(batteryService.getCharacteristic(Characteristic.StatusLowBattery), light.onBatteryLevel.pipe((0, operators_1.map)((batteryLevel) => (batteryLevel < 20 ? 1 : 0)))); batteryService .getCharacteristic(Characteristic.ChargingState) .updateValue(2); // "not chargeable". no way to detect if it is plugged in. } onOffService.setPrimaryService(true); } } exports.LightAndSoundMachineAccessory = LightAndSoundMachineAccessory;