@@ -118,50 +118,42 @@ def brightness(self):
118118
119119 async def async_turn_on (self , ** kwargs ):
120120 """Turn the light on with optional parameters."""
121- _LOGGER .debug ("Turning on Juwel Helialux light with kwargs: %s" , kwargs )
122-
121+ _LOGGER .debug ("Turn on called with: %s" , kwargs )
122+
123+ # Get target values with defaults
123124 brightness = kwargs .get ("brightness" , 255 )
124125 rgbw_color = kwargs .get ("rgbw_color" , (255 , 255 , 255 , 255 ))
125-
126- # Convert HA brightness (0-255) to Juwel's (0-100)
127- brightness_juwel = brightness * 2.55
128-
129- # Calculate the scaling factor for RGBW values based on brightness
130- scale_factor = brightness_juwel / 100.0
131-
132- # Convert RGBW values (0-255) to Juwel's (0-100)
133- red , green , blue , white = (
134- int (rgbw_color [0 ] * 2.55 ),
135- int (rgbw_color [1 ] * 2.55 ),
136- int (rgbw_color [2 ] * 2.55 ),
137- int (rgbw_color [3 ] * 2.55 ),
138- )
139-
140- # Debugging output
141- _LOGGER .debug ("Converted RGBW values (scaled): Red: %d, Green: %d, Blue: %d, White: %d" ,
142- red , green , blue , white )
143-
144- # Ensure all values are within the range of 0-100
145- red = max (0 , min (100 , red ))
146- green = max (0 , min (100 , green ))
147- blue = max (0 , min (100 , blue ))
148- white = max (0 , min (100 , white ))
149-
150- _LOGGER .debug (
151- "Final RGBW values after clamping: Red: %d, Green: %d, Blue: %d, White: %d" ,
152- red , green , blue , white ,
153- )
154-
155- # Now set the manual color with the adjusted values
156- await self ._controller .start_manual_color_simulation (1439 )
157- await self ._controller .set_manual_color (white , blue , green , red )
158-
159- # Update the state
160- self ._attr_is_on = True
161- self ._attr_brightness = brightness
162- self ._attr_rgbw_color = rgbw_color
163-
164- self .async_write_ha_state ()
126+
127+ # Convert to device scale (0-100) - only do this once!
128+ white = min (100 , max (0 , round (rgbw_color [3 ] / 2.55 )))
129+ blue = min (100 , max (0 , round (rgbw_color [2 ] / 2.55 )))
130+ green = min (100 , max (0 , round (rgbw_color [1 ] / 2.55 )))
131+ red = min (100 , max (0 , round (rgbw_color [0 ] / 2.55 )))
132+
133+ # Apply brightness scaling if needed
134+ if brightness < 255 :
135+ scale = brightness / 255.0
136+ white = min (100 , round (white * scale ))
137+ blue = min (100 , round (blue * scale ))
138+ green = min (100 , round (green * scale ))
139+ red = min (100 , round (red * scale ))
140+
141+ _LOGGER .debug ("Setting light to W:%d B:%d G:%d R:%d" , white , blue , green , red )
142+
143+ try :
144+ # Set the light state - values are already in 0-100 range
145+ await self ._controller .start_manual_color_simulation (1439 )
146+ await self ._controller .set_manual_color (white , blue , green , red )
147+
148+ # Update local state immediately
149+ self ._attr_is_on = True
150+ self ._attr_brightness = brightness
151+ self ._attr_rgbw_color = rgbw_color
152+ self .async_write_ha_state ()
153+
154+ except Exception as e :
155+ _LOGGER .error ("Error setting light state: %s" , e )
156+ raise
165157
166158 async def async_turn_off (self , ** kwargs ):
167159 """Turn the light off."""
@@ -172,4 +164,4 @@ async def async_turn_off(self, **kwargs):
172164 self ._attr_brightness = 0
173165 self ._attr_rgbw_color = (0 , 0 , 0 , 0 )
174166
175- self .async_write_ha_state ()
167+ self .async_write_ha_state ()
0 commit comments