homebridge-xiaomi-roborock-vacuum
Version:
Xiaomi Vacuum Cleaner - 1st (Mi Robot), 2nd (Roborock S50 + S55), 3rd Generation (Roborock S6) and S5 Max - plugin for Homebridge.
76 lines • 3.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.XiaomiRoborockVacuumPlatformAccessoryInitializer = void 0;
const rxjs_1 = require("rxjs");
const services_1 = require("../services");
const find_speed_modes_1 = require("../utils/find_speed_modes");
const logger_1 = require("../utils/logger");
const accessory_1 = require("./accessory");
/**
* Dynamic Device Accessory:
*
* It should:
* 1. Connect first
* 2. Figure out the model
* 3. Reuse cached accessory or create a new one
* 4. Assign the listeners and services to the accessory
*/
class XiaomiRoborockVacuumPlatformAccessoryInitializer {
accessory;
initialized$ = new rxjs_1.Subject();
log;
constructor(log, config, api, accessory) {
this.accessory = accessory;
this.log = (0, logger_1.getLogger)(log, config);
const deviceManager = new services_1.DeviceManager(api.hap, this.log, config);
deviceManager.deviceConnected$.pipe((0, rxjs_1.first)()).subscribe(() => {
// For now, let's simply reuse the accessory class to bootstrap the services.
// In the future, we can move to smarter logic: calculate the rooms first, then create the services.
const vacuumService = new accessory_1.XiaomiRoborockVacuumAccessory(this.log, this.applyDefaultsBasedOnModel(config, deviceManager.model), api, deviceManager);
// Loop through the services in `vacuumService.getServices()`
// and attach them to the accessory with
// https://developers.homebridge.io/#/api/platform-plugins#platformaccessoryaddservice
vacuumService.getServices().forEach((service) => {
let svc;
if (service.subtype) {
svc = this.accessory.getServiceById(service.getServiceId(), service.subtype);
}
if (!svc) {
// Hack to cover those services that do not declare a subtype
svc = this.accessory.services.find((cachedSvc) => cachedSvc.UUID === service.UUID &&
cachedSvc.subtype === service.subtype);
}
if (svc) {
// Cached service, we need to copy the listeners
this.log.info("Copying services...");
if (svc instanceof api.hap.Service.AccessoryInformation) {
svc.replaceCharacteristicsFromService(service);
}
else {
svc.characteristics = service.characteristics;
svc.optionalCharacteristics = service.optionalCharacteristics;
svc.setPrimaryService(service.isPrimaryService);
}
}
else {
// New service to add to the accessory
this.log.info("Adding new service...");
svc = this.accessory.addService(service);
}
});
this.initialized$.next();
});
}
applyDefaultsBasedOnModel(config, model) {
// At this point, we can apply any defaults depending on the model...
return {
// like enabling the waterbox mode if the model supports it
waterBox: !!(0, find_speed_modes_1.findSpeedModes)(model).waterspeed,
// or enabling auto-room if the model supports room mapping
// autoroom: TODO,
...config,
};
}
}
exports.XiaomiRoborockVacuumPlatformAccessoryInitializer = XiaomiRoborockVacuumPlatformAccessoryInitializer;
//# sourceMappingURL=platform_accessory.js.map