UNPKG

homebridge-ikettle

Version:

Simple HomeKit control for your iKettle. Easy access without faffing with Siri Shortcuts/IFTTT.

80 lines 3.77 kB
import { iKettlePlatformAccessory } from './platformAccessory.js'; import { iKettleService } from './iKettleService.js'; import { PLUGIN_NAME, PLATFORM_NAME } from './models/constants.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 IKettlePlatform { log; config; api; Service; Characteristic; // this is used to track restored cached accessories accessories = new Map(); discoveredCacheUUIDs = []; // private app: FirebaseApp; constructor(log, config, api) { this.log = log; this.config = config; this.api = api; this.Service = api.hap.Service; this.Characteristic = api.hap.Characteristic; 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 set up 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); } discoverDevices() { const iKettle = iKettleService.getInstance(this.log); this.log.info('GETTING DEVICES'); iKettle.connect(this.config).subscribe({ next: (deviceModel) => { const uuid = this.api.hap.uuid.generate(deviceModel.id); const existingAccessory = this.accessories.get(uuid); if (existingAccessory) { this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName); new iKettlePlatformAccessory(this, existingAccessory); } else { this.log.info(`Got Device: ${deviceModel.id} - ${deviceModel.name}`); const accessory = new this.api.platformAccessory(deviceModel.name, uuid); accessory.context.device = deviceModel; new iKettlePlatformAccessory(this, accessory); this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); } this.discoveredCacheUUIDs.push(uuid); }, error: (error) => { this.log.info(error); }, complete: () => { this.log.info('Finished Kettle Discovery'); for (const [uuid, accessory] of this.accessories) { if (!this.discoveredCacheUUIDs.includes(uuid)) { this.log.info('Removing existing accessory from cache:', accessory.displayName); this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); } } } }); } } //# sourceMappingURL=platform.js.map