homebridge-hatch-baby-rest
Version:
Homebridge plugin for Hatch Rest/Restore WiFi sound machines
47 lines (46 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseAccessory = void 0;
const hap_1 = require("./hap");
const operators_1 = require("rxjs/operators");
const rxjs_1 = require("rxjs");
class BaseAccessory {
constructor(device, accessory) {
this.device = device;
this.accessory = accessory;
const { Service, Characteristic } = hap_1.hap, accessoryInfoService = this.getService(Service.AccessoryInformation);
accessoryInfoService
.getCharacteristic(Characteristic.Manufacturer)
.updateValue('Hatch Baby');
accessoryInfoService
.getCharacteristic(Characteristic.Model)
.updateValue(device.model);
accessoryInfoService
.getCharacteristic(Characteristic.SerialNumber)
.updateValue(device.macAddress);
this.registerCharacteristic(accessoryInfoService.getCharacteristic(Characteristic.FirmwareRevision), device.onFirmwareVersion || (0, rxjs_1.of)(''));
this.registerCharacteristic(accessoryInfoService.getCharacteristic(Characteristic.Name), (0, rxjs_1.of)(device.name));
}
getService(serviceType, nameSuffix, subType) {
let name = nameSuffix
? this.device.name + ' ' + nameSuffix
: this.device.name;
if (hap_1.isTestHomebridge) {
name = 'TEST ' + name;
}
const existingService = this.accessory.getService(serviceType);
return (existingService || this.accessory.addService(serviceType, name, subType));
}
registerCharacteristic(characteristic, onValue, setValue) {
if (setValue) {
characteristic.on("set" /* CharacteristicEventTypes.SET */, (value, callback) => {
callback();
setValue(value);
});
}
onValue.pipe((0, operators_1.distinctUntilChanged)()).subscribe((value) => {
characteristic.updateValue(value);
});
}
}
exports.BaseAccessory = BaseAccessory;