@smj0x/homebridge-shortcuts-buttons
Version:
Run any Apple Shortcut with just the tap of a button, and execute a custom unix command (or another shortcut!) after completion to handle its success/failure, using x-callback-url.
84 lines • 3.56 kB
JavaScript
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js';
import { HSBAccessory, HSBDevice } from './accessory.js';
import { HSBXCallbackUrlServer } from './server/index.js';
import { HSBUtils } from './utils.js';
import { ShortcutsRunner } from './shortcutsRunner';
export class HSBPlatform {
log;
api;
config;
utils;
device;
shortcutsRunner;
// Expose homebridge Service and Characteristic for accessories
Service;
Characteristic;
accessory = null;
server = null;
constructor(log, _config, api) {
this.log = log;
this.api = api;
if (!log.success) {
log.success = log.info;
}
// Assign homebridge Service and Characteristic from the API
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
this.config = _config;
this.device = new HSBDevice(this.config);
this.utils = new HSBUtils(log);
// Configure SSH if enabled and all required fields are present
let sshConfig = undefined;
if (this.config.sshEnabled &&
this.config.sshHost &&
this.config.sshUsername &&
this.config.sshPrivateKeyPath) {
sshConfig = {
enabled: true,
host: this.config.sshHost,
port: this.config.sshPort || 22,
username: this.config.sshUsername,
privateKeyPath: this.config.sshPrivateKeyPath,
passphrase: this.config.sshPassphrase
};
}
this.shortcutsRunner = new ShortcutsRunner(this.log, sshConfig);
this.log.info('Platform initialized:', this.config.name);
api.on('didFinishLaunching', () => {
log.debug('Platform::api.on(didFinishLaunching) callback');
if (this.config.callbackServerEnabled === true) {
this.server = new HSBXCallbackUrlServer(this.config, log, this.utils, api);
}
this.discoverDevices();
});
}
configureAccessory(accessory) {
this.log.debug('Platform::configureAccessory', `DisplayName=${accessory.displayName}`);
this.accessory = accessory;
}
discoverDevices() {
const uuid = this.api.hap.uuid.generate(this.device.serialNumber);
if (this.accessory?.UUID === uuid) {
if (this.accessory.displayName !== this.device.displayName) {
this.log.debug('Platform::discoverDevices', 'Update existing accessory display name to:', this.device.displayName);
this.accessory.displayName = this.device.displayName;
this.accessory.context.device = this.device;
this.api.updatePlatformAccessories([this.accessory]);
}
this.log.info('Platform::discoverDevices', 'Restored existing accessory from cache:', this.accessory.displayName);
}
else {
const accessory = new this.api.platformAccessory(this.device.displayName, uuid);
accessory.context.device = this.device;
this.accessory = accessory;
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [this.accessory]);
this.log.info('Platform::discoverDevices', 'Registered new accessory:', this.accessory.displayName);
}
new HSBAccessory(this, this.accessory);
}
// Make the shortcuts runner available to accessories
getShortcutsRunner() {
return this.shortcutsRunner;
}
}
//# sourceMappingURL=platform.js.map