import { Service , Characteristic , CharacteristicEventTypes } from 'homebridge' ; import { MODE } from '../../miio-consts' ; const RATIO = 30 ; const toPercentage = ( speed ) => Math . round ( ( speed / RATIO ) * 100 ) / 100 ; export function add ( maybeDevice , service , characteristic ) { maybeDevice . then ( ( device ) => { device . on ( 'fanSpeedChanged' , ( speed ) => { service . updateCharacteristic ( characteristic , toPercentage ( speed ) ) ; } ) ; } ) ; return service . getCharacteristic ( characteristic ) . onGet ( async ( ) => { const device = await maybeDevice ; return toPercentage ( await device . fanSpeed ( ) ) ; } ) . onSet ( async ( speed ) => { const device = await maybeDevice ; console . log ( device ) ; if ( ( await device . mode ( ) ) !== MODE . NONE ) { await device . changeMode ( MODE . NONE ) ; } await device . changeFanSpeed ( + speed * RATIO ) ; } ) ; }