import { Service , PlatformAccessory , CharacteristicValue } from 'homebridge' ; import { ExampleHomebridgePlatform } from './platform' ; /**
 * Platform Accessory
 * An instance of this class is created for each accessory your platform registers
 * Each accessory may expose multiple services of different service types.
 */ export class ExamplePlatformAccessory { private service : Service ; /**
     * These are just used to create a working example
     * You should implement your own code to track the state of your accessory
     */ private exampleStates = { On : false , Brightness : 100 , } ; constructor ( private readonly platform , private readonly accessory ) { this . accessory . getService ( this . platform . Service . AccessoryInformation ) ! . setCharacteristic ( this . platform . Characteristic . Manufacturer , 'Default-Manufacturer' ) . setCharacteristic ( this . platform . Characteristic . Model , 'Default-Model' ) . setCharacteristic ( this . platform . Characteristic . SerialNumber , 'Default-Serial' ) ; this . service = this . accessory . getService ( this . platform . Service . Lightbulb ) || this . accessory . addService ( this . platform . Service . Lightbulb ) ; this . service . setCharacteristic ( this . platform . Characteristic . Name , accessory . context . device . exampleDisplayName ) ; this . service . getCharacteristic ( this . platform . Characteristic . On ) . onSet ( this . setOn . bind ( this ) ) . onGet ( this . getOn . bind ( this ) ) ; this . service . getCharacteristic ( this . platform . Characteristic . Brightness ) . onSet ( this . setBrightness . bind ( this ) ) ; /**
         * Creating multiple services of the same type.
         *
         * To avoid "Cannot add a Service with the same UUID another Service without also defining a unique 'subtype' property." error,
         * when creating multiple services of the same type, you need to use the following syntax to specify a name and subtype id:
         * this.accessory.getService('NAME') || this.accessory.addService(this.platform.Service.Lightbulb, 'NAME', 'USER_DEFINED_SUBTYPE_ID');
         *
         * The USER_DEFINED_SUBTYPE must be unique to the platform accessory (if you platform exposes multiple accessories, each accessory
         * can use the same sub type id.)
         */ const motionSensorOneService = this . accessory . getService ( 'Motion Sensor One Name' ) || this . accessory . addService ( this . platform . Service . MotionSensor , 'Motion Sensor One Name' , 'YourUniqueIdentifier-1' ) ; const motionSensorTwoService = this . accessory . getService ( 'Motion Sensor Two Name' ) || this . accessory . addService ( this . platform . Service . MotionSensor , 'Motion Sensor Two Name' , 'YourUniqueIdentifier-2' ) ; /**
         * Updating characteristics values asynchronously.
         *
         * Example showing how to update the state of a Characteristic asynchronously instead
         * of using the `on('get')` handlers.
         * Here we change update the motion sensor trigger states on and off every 10 seconds
         * the `updateCharacteristic` method.
         *
         */ let motionDetected = false ; setInterval ( ( ) => { motionDetected = ! motionDetected ; motionSensorOneService . updateCharacteristic ( this . platform . Characteristic . MotionDetected , motionDetected ) ; motionSensorTwoService . updateCharacteristic ( this . platform . Characteristic . MotionDetected , ! motionDetected ) ; this . platform . log . debug ( 'Triggering motionSensorOneService:' , motionDetected ) ; this . platform . log . debug ( 'Triggering motionSensorTwoService:' , ! motionDetected ) ; } , 10000 ) ; } /**
     * Handle "SET" requests from HomeKit
     * These are sent when the user changes the state of an accessory, for example, turning on a Light bulb.
     */ async setOn ( value ) { this . exampleStates . On = value as boolean ; this . platform . log . debug ( 'Set Characteristic On ->' , value ) ; } example async getOn ( ) { const isOn = this . exampleStates . On ; this . platform . log . debug ( 'Get Characteristic On ->' , isOn ) ; return isOn ; } /**
     * Handle "SET" requests from HomeKit
     * These are sent when the user changes the state of an accessory, for example, changing the Brightness
     */ async setBrightness ( value ) { this . exampleStates . Brightness = value as number ; this . platform . log . debug ( 'Set Characteristic Brightness -> ' , value ) ; } }