import { Service , Characteristic } from 'homebridge' ; export function add ( maybeDevice , service , characteristic ) { const { ACTIVE , INACTIVE } = characteristic ; maybeDevice . then ( ( device ) => { device . on ( 'powerChanged' , ( isOn ) => { service . updateCharacteristic ( characteristic , isOn ? ACTIVE : INACTIVE ) ; } ) ; } ) ; return service . getCharacteristic ( characteristic ) . onGet ( async ( ) => { const device = await maybeDevice ; return ( await device . power ( ) ) ? ACTIVE : INACTIVE ; } ) . onSet ( async function ( this , newStatus ) { const device = await maybeDevice ; const currentStatus = await device . power ( ) ; if ( currentStatus !== newStatus ) { await device . changePower ( newStatus ) ; } } ) ; }