import { API , DynamicPlatformPlugin , Logger , PlatformAccessory , PlatformConfig , Service , Characteristic } from 'homebridge' ; import { PLATFORM_NAME , PLUGIN_NAME } from './settings' ; import { ExamplePlatformAccessory } from './platformAccessory' ; /**
 * 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 ExampleHomebridgePlatform implements DynamicPlatformPlugin { public readonly Service : typeof Service = this . api . hap . Service ; public readonly Characteristic : typeof Characteristic = this . api . hap . Characteristic ; public readonly accessories : PlatformAccessory [ ] = [ ] ; constructor ( public readonly log , public readonly config , public readonly api ) { this . log . debug ( 'Finished initializing platform:' , this . config . name ) ; this . api . on ( 'didFinishLaunching' , ( ) => { log . debug ( 'Executed didFinishLaunching callback' ) ; this . discoverDevices ( ) ; } ) ; } /**
     * 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 ) ; this . accessories . push ( accessory ) ; } /**
     * This is an example method showing how to register discovered accessories.
     * Accessories must only be registered once, previously created accessories
     * must not be registered again to prevent "duplicate UUID" errors.
     */ discoverDevices ( ) { const exampleDevices = [ { exampleUniqueId : 'ABCD' , exampleDisplayName : 'Bedroom' , } , { exampleUniqueId : 'EFGH' , exampleDisplayName : 'Kitchen' , } , ] ; for ( const device of exampleDevices ) { const uuid = this . api . hap . uuid . generate ( device . exampleUniqueId ) ; const existingAccessory = this . accessories . find ( accessory => accessory . UUID === uuid ) ; if ( existingAccessory ) { this . log . info ( 'Restoring existing accessory from cache:' , existingAccessory . displayName ) ; new ExamplePlatformAccessory ( this , existingAccessory ) ; } else { this . log . info ( 'Adding new accessory:' , device . exampleDisplayName ) ; const accessory = new this . api . platformAccessory ( device . exampleDisplayName , uuid ) ; accessory . context . device = device ; new ExamplePlatformAccessory ( this , accessory ) ; this . api . registerPlatformAccessories ( PLUGIN_NAME , PLATFORM_NAME , [ accessory ] ) ; } } } }