homebridge-hatch-baby-rest
Version:
Homebridge plugin for Hatch Rest/Restore WiFi sound machines
45 lines (44 loc) • 1.84 kB
JavaScript
import { hap, isTestHomebridge } from "./hap.js";
import { distinctUntilChanged } from 'rxjs/operators';
import { of } from 'rxjs';
export class BaseAccessory {
device;
accessory;
constructor(device, accessory) {
this.device = device;
this.accessory = accessory;
const { Service, Characteristic } = 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 || of(''));
this.registerCharacteristic(accessoryInfoService.getCharacteristic(Characteristic.Name), of(device.name));
}
getService(serviceType, nameSuffix, subType) {
let name = nameSuffix
? this.device.name + ' ' + nameSuffix
: this.device.name;
if (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', (value, callback) => {
callback();
setValue(value);
});
}
onValue.pipe(distinctUntilChanged()).subscribe((value) => {
characteristic.updateValue(value);
});
}
}