homebridge-hatch-baby-rest-volume
Version:
Homebridge plugin for Hatch Baby Rest bluetooth night light volume
65 lines (64 loc) • 3.58 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HatchBabyRestPlusVolumeAccessory = void 0;
const hap_1 = require("../hap");
const operators_1 = require("rxjs/operators");
const rxjs_1 = require("rxjs");
class HatchBabyRestPlusVolumeAccessory {
constructor(light, accessory, log, audioTrack) {
this.light = light;
this.accessory = accessory;
this.log = log;
this.audioTrack = audioTrack;
const { Service, Characteristic } = hap_1.hap, lightService = this.getService(Service.Lightbulb), batteryService = this.getService(Service.BatteryService), accessoryInfoService = this.getService(Service.AccessoryInformation);
this.registerCharacteristic(lightService.getCharacteristic(Characteristic.On), light.onAudioOn, (on) => {
log.info('ON: ' + on);
light.setAudioTrack(on ? audioTrack !== null && audioTrack !== void 0 ? audioTrack : 3 /* PinkNoise */ : 0 /* None */);
});
this.registerCharacteristic(lightService.getCharacteristic(Characteristic.Brightness), light.onVolume, (volume) => {
log.info('VOLUME: ' + volume);
light.setVolume(volume);
});
this.registerCharacteristic(batteryService.getCharacteristic(Characteristic.BatteryLevel), light.onBatteryLevel);
accessoryInfoService
.getCharacteristic(Characteristic.Manufacturer)
.updateValue('Hatch Baby');
accessoryInfoService
.getCharacteristic(Characteristic.Model)
.updateValue('Rest+');
accessoryInfoService
.getCharacteristic(Characteristic.SerialNumber)
.updateValue('Unknown');
this.registerCharacteristic(accessoryInfoService.getCharacteristic(Characteristic.FirmwareRevision), light.onState.pipe(operators_1.map((state) => state.deviceInfo.f)));
this.registerCharacteristic(accessoryInfoService.getCharacteristic(Characteristic.Name), rxjs_1.of(light.name));
}
getService(serviceType) {
const existingService = this.accessory.getService(serviceType);
return existingService || this.accessory.addService(serviceType);
}
registerCharacteristic(characteristic, onValue, setValue) {
const getValue = () => onValue.pipe(operators_1.take(1)).toPromise();
if (setValue) {
characteristic.on("set" /* SET */, (value, callback) => __awaiter(this, void 0, void 0, function* () {
callback();
const currentValue = yield getValue();
if (value !== currentValue) {
setValue(value);
}
}));
}
onValue.pipe(operators_1.distinctUntilChanged()).subscribe((value) => {
characteristic.updateValue(value);
});
}
}
exports.HatchBabyRestPlusVolumeAccessory = HatchBabyRestPlusVolumeAccessory;