import { clamp , convertHSLtoRGB , convertRGBtoHSL } from '../magichome-interface/utils' ; import { HomebridgeMagichomeDynamicPlatformAccessory } from '../platformAccessory' ; export class RGBWWBulb extends HomebridgeMagichomeDynamicPlatformAccessory { async updateDeviceState ( _timeout = 200 ) { const hsl = this . lightState . HSL ; const [ red , green , blue ] = convertHSLtoRGB ( hsl ) ; const whites = this . hueToWhiteTemperature ( ) ; const brightness = this . lightState . brightness ; let mask = 0xF0 ; let r = Math . round ( ( ( clamp ( red , 0 , 255 ) / 100 ) * brightness ) ) ; let g = Math . round ( ( ( clamp ( green , 0 , 255 ) / 100 ) * brightness ) ) ; let b = Math . round ( ( ( clamp ( blue , 0 , 255 ) / 100 ) * brightness ) ) ; let ww = Math . round ( ( ( clamp ( whites . warmWhite , 0 , 255 ) / 100 ) * brightness ) ) ; let cw = Math . round ( ( ( clamp ( whites . coldWhite , 0 , 255 ) / 100 ) * brightness ) ) ; if ( hsl . hue == 31 && ( hsl . saturation == 33 ) ) { r = 0 ; g = 0 ; b = 0 ; ww = Math . round ( ( 255 / 100 ) * brightness ) ; cw = 0 ; mask = 0x0F ; } else if ( ( hsl . hue == 208 && ( hsl . saturation == 17 ) ) ) { r = 0 ; g = 0 ; b = 0 ; ww = 0 ; cw = Math . round ( ( 255 / 100 ) * brightness ) ; mask = 0x0F ; } else if ( ( hsl . saturation < this . colorWhiteThreshold ) ) { r = 0 ; g = 0 ; b = 0 ; mask = 0x0F ; } else { ww = 0 ; cw = 0 ; } await this . send ( [ 0x31 , r , g , b , ww , cw , mask , 0x0F ] , true , _timeout ) ; } async updateHomekitState ( ) { this . service . updateCharacteristic ( this . platform . Characteristic . On , this . lightState . isOn ) ; this . service . updateCharacteristic ( this . platform . Characteristic . Hue , this . lightState . HSL . hue ) ; this . service . updateCharacteristic ( this . platform . Characteristic . Saturation , this . lightState . HSL . saturation ) ; if ( this . lightState . HSL . luminance > 0 && this . lightState . isOn ) { this . service . updateCharacteristic ( this . platform . Characteristic . Brightness , this . lightState . HSL . luminance * 2 ) ; } else if ( this . lightState . isOn ) { this . service . updateCharacteristic ( this . platform . Characteristic . Brightness , clamp ( ( ( this . lightState . whiteValues . coldWhite / 2.55 ) + ( this . lightState . whiteValues . warmWhite / 2.55 ) ) , 0 , 100 ) ) ; if ( this . lightState . whiteValues . warmWhite > this . lightState . whiteValues . coldWhite ) { this . service . updateCharacteristic ( this . platform . Characteristic . Saturation , this . colorWhiteThreshold - ( this . colorWhiteThreshold * ( this . lightState . whiteValues . coldWhite / 255 ) ) ) ; this . service . updateCharacteristic ( this . platform . Characteristic . Hue , 0 ) ; } else { this . service . updateCharacteristic ( this . platform . Characteristic . Saturation , this . colorWhiteThreshold - ( this . colorWhiteThreshold * ( this . lightState . whiteValues . warmWhite / 255 ) ) ) ; this . service . updateCharacteristic ( this . platform . Characteristic . Hue , 180 ) ; } } this . cacheCurrentLightState ( ) ; } }