homebridge-pushover-notification
Version:
Homebridge plugin to send push notifications through Pushover from HomeKit
68 lines • 2.89 kB
JavaScript
import { SwitchAccessory } from './switch-accessory.js';
import { PLATFORM_NAME, PLUGIN_NAME } from './const.js';
import { PushoverClient } from './pushover-client.js';
export class PushoverNotificationPlatform {
log;
config;
api;
Service;
Characteristic;
accessories = new Map();
discoveredCacheUUIDs = [];
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = api.hap.Service;
this.Characteristic = api.hap.Characteristic;
if (!config.token) {
this.log.error('Pushover API token must be configured');
return;
}
if (!config.user) {
this.log.error('Pushover user key must be configured');
return;
}
this.log.debug('Finished initializing platform:', config.name);
this.api.on('didFinishLaunching', () => {
log.debug('Registering accessories...');
this.registerAccessories();
});
}
configureAccessory(accessory) {
this.log.debug('Loading accessory from cache:', accessory.displayName);
this.accessories.set(accessory.UUID, accessory);
}
registerAccessories() {
const config = this.config;
const pushoverClient = new PushoverClient(config.token, config.user, this.log);
if (config.messages) {
for (const message of config.messages) {
if (!message.name || !message.message) {
this.log.debug('Invalid message config, skipping setup of accessory');
continue;
}
const uuid = this.api.hap.uuid.generate(message.name);
const existingAccessory = this.accessories.get(uuid);
if (existingAccessory) {
this.log.debug('Restoring existing accessory from cache:', existingAccessory.displayName);
new SwitchAccessory(this, existingAccessory, pushoverClient, message);
}
else {
this.log.debug('Adding new accessory:', message.name);
const accessory = new this.api.platformAccessory(message.name, uuid);
new SwitchAccessory(this, accessory, pushoverClient, message);
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
this.discoveredCacheUUIDs.push(uuid);
}
}
for (const [uuid, accessory] of this.accessories) {
if (!this.discoveredCacheUUIDs.includes(uuid)) {
this.log.debug('Removing existing accessory from cache:', accessory.displayName);
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
}
}
}
//# sourceMappingURL=platform.js.map