@@ -17,28 +17,62 @@ def __init__(self, color_mode: ColorMode, converter: ColorConverter, channels: D
1717
1818 self .is_on = False
1919 self .brightness = 255
20- self .rgb = (255 , 255 , 255 )
2120 self .cold_white = 255
2221 self .warm_white = 255
2322 self .color_temp_kelvin = 3000
2423 self .color_temp_dmx = 255
2524
2625 self .last_brightness = 255
27- self .last_rgb = (255 , 255 , 255 )
2826 self .last_cold_white = 255
2927 self .last_warm_white = 255
3028 self .last_color_temp_kelvin = 3000
3129 self .last_color_temp_dmx = 255
32-
33- self ._channel_handlers = {
34- ChannelType .DIMMER : self ._handle_dimmer_update ,
35- ChannelType .RED : lambda v : self ._handle_rgb_component_update (0 , v ),
36- ChannelType .GREEN : lambda v : self ._handle_rgb_component_update (1 , v ),
37- ChannelType .BLUE : lambda v : self ._handle_rgb_component_update (2 , v ),
38- ChannelType .COLD_WHITE : self ._handle_cold_white_update ,
39- ChannelType .WARM_WHITE : self ._handle_warm_white_update ,
40- ChannelType .COLOR_TEMPERATURE : self .update_color_temp_dmx ,
41- }
30+
31+ # Only initialize RGB for color modes that use it
32+ if color_mode in [ColorMode .RGB , ColorMode .RGBW , ColorMode .RGBWW ]:
33+ self .rgb = (255 , 255 , 255 )
34+ self .last_rgb = (255 , 255 , 255 )
35+ else :
36+ self .rgb = None
37+ self .last_rgb = None
38+
39+ self ._channel_handlers = {}
40+
41+ if ChannelType .DIMMER in channels :
42+ self ._channel_handlers [ChannelType .DIMMER ] = self ._handle_dimmer_update
43+
44+ if ChannelType .RED in channels :
45+ self ._channel_handlers [ChannelType .RED ] = lambda v : self ._handle_rgb_component_update (0 , v )
46+ if ChannelType .GREEN in channels :
47+ self ._channel_handlers [ChannelType .GREEN ] = lambda v : self ._handle_rgb_component_update (1 , v )
48+ if ChannelType .BLUE in channels :
49+ self ._channel_handlers [ChannelType .BLUE ] = lambda v : self ._handle_rgb_component_update (2 , v )
50+
51+ if ChannelType .COLD_WHITE in channels :
52+ self ._channel_handlers [ChannelType .COLD_WHITE ] = self ._handle_cold_white_update
53+ if ChannelType .WARM_WHITE in channels :
54+ self ._channel_handlers [ChannelType .WARM_WHITE ] = self ._handle_warm_white_update
55+ if ChannelType .COLOR_TEMPERATURE in channels :
56+ self ._channel_handlers [ChannelType .COLOR_TEMPERATURE ] = self .update_color_temp_dmx
57+
58+ # Ensure state consistency after init/restoration
59+ self ._validate_and_fix_state ()
60+
61+ def _validate_and_fix_state (self ):
62+ """Ensure state fields are consistent with color mode after init/restoration"""
63+ if self .color_mode in [ColorMode .RGB , ColorMode .RGBW , ColorMode .RGBWW ]:
64+ # RGB color modes must have valid RGB values
65+ if self .rgb is None :
66+ self .rgb = (255 , 255 , 255 )
67+ log .warning (f"Fixed None RGB value for { self .color_mode } color mode" )
68+ if self .last_rgb is None :
69+ self .last_rgb = (255 , 255 , 255 )
70+ else :
71+ # Non-RGB color modes should have RGB as None
72+ if self .rgb is not None :
73+ self .rgb = None
74+ if self .last_rgb is not None :
75+ self .last_rgb = None
4276
4377 def has_channel (self , t : ChannelType ) -> bool :
4478 return t in self .channels
@@ -62,6 +96,16 @@ def _handle_dimmer_update(self, value: int):
6296 self .is_on = value > 0
6397
6498 def _handle_rgb_component_update (self , component_index : int , value : int ):
99+ # This should only be called for RGB color modes
100+ if self .color_mode not in [ColorMode .RGB , ColorMode .RGBW , ColorMode .RGBWW ]:
101+ log .warning (f"RGB component update called for non-RGB color mode { self .color_mode } " )
102+ return
103+
104+ # Safety check: ensure RGB is valid (in case of state restoration corruption)
105+ if self .rgb is None :
106+ self .rgb = (255 , 255 , 255 )
107+ log .warning (f"Fixed None RGB during RGB update for { self .color_mode } color mode" )
108+
65109 new_rgb = list (self .rgb )
66110 new_rgb [component_index ] = value
67111 self ._update_rgb_based_on_color_mode (* new_rgb )
@@ -254,7 +298,6 @@ def update_color_temp_kelvin(self, kelvin: int):
254298
255299 def reset (self ):
256300 self .brightness = 0
257- self .rgb = (0 , 0 , 0 )
258301 self .cold_white = 0
259302 self .warm_white = 0
260303 self .is_on = False
@@ -269,7 +312,7 @@ def is_all_zero(self, has_dimmer: bool) -> bool:
269312 def _update_brightness_from_channels (self ):
270313 values = []
271314
272- if self .has_rgb ():
315+ if self .has_rgb () and self . rgb is not None :
273316 values .extend (self .rgb )
274317
275318 if self .has_channel (ChannelType .COLD_WHITE ):
0 commit comments