matterbridge-xiaomi-roborock
Version:
Matterbridge Xiaomi Roborock Plugin
44 lines (43 loc) • 2.05 kB
JavaScript
import { MatterbridgeDynamicPlatform } from 'matterbridge';
import { VacuumDeviceAccessory } from './vacuum_device_accessory.js';
export class XiaomiRoborockVacuumPlatform extends MatterbridgeDynamicPlatform {
config;
vacuumDevices = new Set();
constructor(matterbridge, log, config) {
super(matterbridge, log, config);
this.config = config;
log.logLevel = this.config.debug ? "debug" : "info";
if (this.verifyMatterbridgeVersion === undefined || typeof this.verifyMatterbridgeVersion !== 'function' || !this.verifyMatterbridgeVersion('3.3.0')) {
throw new Error(`This plugin requires Matterbridge version >= "3.3.0". Please update Matterbridge from ${this.matterbridge.matterbridgeVersion} to the latest version."`);
}
this.log.info(`Initializing Platform...`);
}
async onStart(reason) {
this.log.info(`onStart called with reason: ${reason ?? 'none'}`);
await this.ready;
await this.clearSelect();
await this.discoverDevices();
}
async onChangeLoggerLevel(logLevel) {
this.log.info(`onChangeLoggerLevel called with: ${logLevel}`);
}
async onShutdown(reason) {
await super.onShutdown(reason);
this.log.info(`onShutdown called with reason: ${reason ?? 'none'}`);
if (this.config.unregisterOnShutdown === true)
await this.unregisterAllDevices();
this.vacuumDevices.forEach((vacuum) => vacuum.stop());
}
async discoverDevices() {
this.log.info('Discovering devices...');
const devices = this.config.devices ?? [];
this.log.info(`Found ${devices.length} devices`);
await Promise.all(devices.map(async (cfg) => {
const vacuumDevice = new VacuumDeviceAccessory(cfg, this.log);
const vacuum = await vacuumDevice.initializeMatterbridgeEndpoint();
await this.registerDevice(vacuum);
await vacuumDevice.postRegister();
this.vacuumDevices.add(vacuumDevice);
}));
}
}