homebridge-better-miot
Version:
Some xiaomi devices integration
68 lines • 3.63 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HomebridgeBetterMiotPlatform = void 0;
const settings_1 = require("./settings");
const accessories_1 = require("./accessories");
class HomebridgeBetterMiotPlatform {
constructor(log, config, api) {
this.log = log;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.accessories = [];
this.config = config;
this.log.debug('Finished initializing platform:', this.config.name);
// When this event is fired it means Homebridge has restored all cached accessories from disk.
// Dynamic Platform plugins should only register new accessories after this event was fired,
// in order to ensure they weren't added to homebridge already. This event can also be used
// to start discovery of new accessories.
this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback');
// run the method to discover / register your devices as accessories
this.discoverDevices();
});
}
/**
* This function is invoked when homebridge restores cached accessories from disk at startup.
* It should be used to setup event handlers for characteristics and update respective values.
*/
configureAccessory(accessory) {
this.log.info('Loading accessory from cache:', accessory.displayName);
// add the restored accessory to the accessories cache so we can track if it has already been registered
this.accessories.push(accessory);
}
/**
* This is an example method showing how to register discovered accessories.
* Accessories must only be registered once, previously created accessories
* must not be registered again to prevent "duplicate UUID" errors.
*/
discoverDevices() {
this.config.devices
.filter(({ isDisabled }) => !isDisabled)
.forEach((device) => {
const uuid = this.api.hap.uuid.generate(device.address);
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid); // TODO: use Map for storying accessories
if (existingAccessory) {
// the accessory already exists
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
new accessories_1.BetterMiotPlatformAccessory(this, existingAccessory);
}
else {
// the accessory does not yet exist, so we need to create it
this.log.info('Adding new accessory:', device.displayName);
// create a new accessory
const accessory = new this.api.platformAccessory(device.displayName, uuid);
// store a copy of the device object in the `accessory.context`
// the `context` property can be used to store any data about the accessory you may need
accessory.context = device;
// create the accessory handler for the newly create accessory
// this is imported from `platformAccessory.ts`
new accessories_1.BetterMiotPlatformAccessory(this, accessory);
// link the accessory to your platform
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
});
}
}
exports.HomebridgeBetterMiotPlatform = HomebridgeBetterMiotPlatform;
//# sourceMappingURL=platform.js.map