homebridge-risco-lite
Version:
A simple homebridge plugin to arm and disarm Risco alarm systems.
100 lines • 5.04 kB
JavaScript
import { RiscoSecuritySystemAccessory } from './platformAccessory.js';
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
import { RiscoClient } from './lib/riscoClient.js';
/**
* HomebridgePlatform
* This class is the main constructor for your plugin, this is where you should
* parse the user config and discover/register accessories with Homebridge.
*/
export class RiscoAlarmPlatform {
log;
config;
api;
authenticationInterval = 1000 * 60 * 30;
Service;
Characteristic;
// this is used to track restored cached accessories
accessories = new Map();
riscoClient;
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.riscoClient = new RiscoClient(this.config, this.log);
// 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();
// Re-authenticate every <authenticationInterval> to keep us logged in.
setInterval(() => {
this.riscoClient.reAuthenticate();
}, this.authenticationInterval);
});
}
/**
* 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.set(accessory.UUID, accessory);
}
/**
* Register discovered accessories.
* Accessories must only be registered once, previously created accessories
* must not be registered again to prevent "duplicate UUID" errors.
*/
discoverDevices() {
// Get devices from the config
const devices = this.config.devices || [{
name: 'Risco Alarm',
id: 'main',
}];
// loop over the discovered devices and register each one if it has not already been registered
for (const device of devices) {
// generate a unique id for the accessory
const uuid = this.api.hap.uuid.generate(device.id);
// see if an accessory with the same uuid has already been registered and restored from
// the cached devices we stored in the `configureAccessory` method above
const existingAccessory = this.accessories.get(uuid);
if (existingAccessory) {
// the accessory already exists
this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
// Update the accessory context
existingAccessory.context.device = device;
this.api.updatePlatformAccessories([existingAccessory]);
// create the accessory handler for the restored accessory
new RiscoSecuritySystemAccessory(this, existingAccessory);
}
else {
// the accessory does not yet exist, so we need to create it
this.log.info('Adding new accessory:', device.name);
// create a new accessory
const accessory = new this.api.platformAccessory(device.name, 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 = device;
// create the accessory handler for the newly create accessory
new RiscoSecuritySystemAccessory(this, accessory);
// link the accessory to your platform
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
// Remove any accessories that are no longer configured
for (const [uuid, accessory] of this.accessories) {
const isConfigured = devices.some(device => this.api.hap.uuid.generate(device.id) === uuid);
if (!isConfigured) {
this.log.info('Removing existing accessory from cache:', accessory.displayName);
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
}
//# sourceMappingURL=platform.js.map