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.
79 lines • 3.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.XiaomiRoborockVacuumPlatform = void 0;
const services_1 = require("../services");
const constants_1 = require("../constants");
const platform_accessory_1 = require("./platform_accessory");
const rxjs_1 = require("rxjs");
class XiaomiRoborockVacuumPlatform {
log;
api;
config;
// List of the cached accessories
accessories = new Set();
constructor(log, config, api) {
this.log = log;
this.api = api;
this.config = {
devices: [], // Make sure it exists
...config,
};
// This event indicates all cached accessories have been loaded
this.api.on("didFinishLaunching", () => {
// 1. Unregister all the accessories that have been removed from the config
this.unregisterRemovedAccessories();
// 2. Initialize the connections and new accessories
this.initializeDevices();
});
}
/**
* Required by Homebridge - Called for each cached accessory.
* @param accessory
*/
configureAccessory(accessory) {
this.accessories.add(accessory);
}
/**
* Removes all cached accessories that no longer exist in the configuration
*/
unregisterRemovedAccessories() {
const accessoriesToDelete = [...this.accessories].filter((accessory) => !this.config.devices.find(({ name }) => name === accessory.context.name));
if (accessoriesToDelete.length > 0) {
const names = accessoriesToDelete.map((accessory) => {
this.accessories.delete(accessory);
return accessory.context.name;
});
this.log.info(`Unregistering the devices [${names.join(", ")}]`);
this.api.unregisterPlatformAccessories(constants_1.PLUGIN_NAME, constants_1.ACCESSORY_NAME, accessoriesToDelete);
}
}
/**
* Initializes the connections of all the devices
* and appends new accessories if not previously cached.
*/
initializeDevices() {
this.config.devices.forEach((config) => {
let isCached = true;
const uuid = this.api.hap.uuid.generate(`${constants_1.PLUGIN_NAME}-accessory-${config.name}`);
let accessory = [...this.accessories].find(({ UUID }) => UUID === uuid);
if (!accessory) {
isCached = false;
const name = (0, services_1.applyConfigDefaults)(config).name;
accessory = new this.api.platformAccessory(name, uuid);
accessory.context.name = name;
this.accessories.add(accessory);
}
const platformInitializer = new platform_accessory_1.XiaomiRoborockVacuumPlatformAccessoryInitializer(this.log, config, this.api, accessory);
// Finally, register the accessory if not cached
if (!isCached) {
platformInitializer.initialized$.pipe((0, rxjs_1.first)()).subscribe(() => {
this.api.registerPlatformAccessories(constants_1.PLUGIN_NAME, constants_1.ACCESSORY_NAME, [
accessory,
]);
});
}
});
}
}
exports.XiaomiRoborockVacuumPlatform = XiaomiRoborockVacuumPlatform;
//# sourceMappingURL=platform.js.map