import type { PlatformAccessory } from 'homebridge' ; import { Characteristic , CharacteristicEventTypes , CharacteristicSetCallback , CharacteristicValue , Service } from '../config/hap' ; import TydomController from '../controller' ; import { addAccessoryService , setupAccessoryIdentifyHandler , setupAccessoryInformationService } from '../helpers/accessory' ; import type { TydomAccessoryContext } from '../typings/tydom' ; import { debugSet , debugSetResult } from '../utils/debug' ; export const setupGarageDoorOpener = ( accessory , controller ) : void => { const { context } = accessory ; const { client } = controller ; const { deviceId , endpointId } = context as TydomAccessoryContext ; setupAccessoryInformationService ( accessory , controller ) ; setupAccessoryIdentifyHandler ( accessory , controller ) ; const service = addAccessoryService ( accessory , Service . Switch , `${ accessory . displayName }` , true ) ; service . getCharacteristic ( Characteristic . On ) . on ( CharacteristicEventTypes . SET , async ( value , callback ) => { debugSet ( Characteristic . On , service , value ) ; if ( ! value ) { callback ( ) ; return ; } try { await client . put ( `/devices/${ deviceId }/endpoints/${ endpointId }/data` , [ { name : 'levelCmd' , value : 'TOGGLE' } ] ) ; debugSetResult ( Characteristic . On , service , value ) ; callback ( ) ; setTimeout ( ( ) => { service . updateCharacteristic ( Characteristic . On , false ) ; } , 1000 ) ; } catch ( err ) { callback ( err as Error ) ; } } ) . updateValue ( false ) ; } ;