homebridge-august-door-sense
Version:
Homebridge Plugin for August DoorSense.
80 lines • 4.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AugustSmartLockPlatform = void 0;
const settings_1 = require("./settings");
const platformAccessory_1 = require("./platformAccessory");
const august_1 = require("./august");
class AugustSmartLockPlatform {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
// this is used to track restored cached accessories
this.accessories = [];
this.log.debug('Finished initializing platform:', this.config.name);
this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback');
this.discoverDevices();
});
}
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);
}
async discoverDevices() {
var _a, _b, _c, _d;
const options = {
apiKey: (_a = this.config['securityToken']) !== null && _a !== void 0 ? _a : '7cab4bbd-2693-4fc1-b99b-dec0fb20f9d4',
uuid: this.config['installId'],
idType: (_b = this.config['idType']) !== null && _b !== void 0 ? _b : (this.config['phone'] ? 'phone' : 'email'),
identifier: (_d = (_c = this.config['id']) !== null && _c !== void 0 ? _c : this.config['phone']) !== null && _d !== void 0 ? _d : this.config['email'],
password: this.config['password'],
code: this.config['code'],
};
(0, august_1.augustStartSession)(options, this.log).then(session => {
this.Session = session;
(0, august_1.augustGetLocks)(session, this.log).then(locks => {
this.log.info(JSON.stringify(locks));
// filter out locks that are not in the config
const filteredLocks = this.config['filter']
? locks.filter(lock => !lock.id.toLowerCase().includes(this.config['filter'].toLowerCase()))
: locks;
this.registerLocks(filteredLocks);
}).catch(() => {
this.log.error('Failed to get locks');
});
}).catch((e) => {
this.log.debug(e);
this.log.error('Failed to start session, check your config and confirm your password');
});
}
registerLocks(locks) {
const usedAccessories = new Set();
for (const lock of locks) {
const uuid = this.api.hap.uuid.generate(`${lock.id}-doorSense`);
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
if (existingAccessory) {
// the accessory already exists
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
new platformAccessory_1.AugustSmartLockAccessory(this, existingAccessory);
}
else {
// the accessory does not yet exist, so we need to create it
this.log.info('Adding new accessory:', lock.name);
const accessory = new this.api.platformAccessory(lock.name, uuid);
accessory.context.device = lock;
new platformAccessory_1.AugustSmartLockAccessory(this, accessory);
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
}
usedAccessories.add(uuid);
}
const unusedAccessories = this.accessories.filter(accessory => !usedAccessories.has(accessory.UUID));
unusedAccessories.forEach(accessory => this.log.info(`Pruning unused accessory ${accessory.UUID} from cache`));
this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, unusedAccessories);
}
}
exports.AugustSmartLockPlatform = AugustSmartLockPlatform;
//# sourceMappingURL=platform.js.map