Skip to content

Commit 1bb2b19

Browse files
committed
Update color space definition
1 parent 38fdde5 commit 1bb2b19

File tree

5 files changed

+385
-684
lines changed

5 files changed

+385
-684
lines changed

custom_components/dmx/entity/light/light_controller.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from typing import Dict, Any
22

33
from custom_components.dmx.entity.light import ChannelType
4-
from custom_components.dmx.entity.light.light_state import LightState
4+
from custom_components.dmx.entity.light.light_state import LuvLightState
55
from custom_components.dmx.io.dmx_io import DmxUniverse
66

77

88
class LightController:
9-
def __init__(self, state: LightState, universe: DmxUniverse):
9+
def __init__(self, state: LuvLightState, universe: DmxUniverse):
1010
self.state = state
1111
self.universe = universe
1212
self.is_updating = False
@@ -44,21 +44,23 @@ async def _apply_updates(self, updates: Dict[ChannelType, int]):
4444
def _collect_updates_from_kwargs(self, kwargs: Dict[str, Any]) -> Dict[ChannelType, int]:
4545
updates = {}
4646

47-
if "brightness" in kwargs:
48-
brightness = kwargs["brightness"]
49-
updates.update(self.state.get_scaled_brightness_updates(brightness))
50-
5147
if "rgb_color" in kwargs and self.state.has_rgb():
5248
r, g, b = kwargs["rgb_color"]
49+
# Update the internal L*u*v* state
50+
self.state.update_rgb(r, g, b)
5351
updates.update({ChannelType.RED: r, ChannelType.GREEN: g, ChannelType.BLUE: b})
5452

5553
if "rgbw_color" in kwargs:
5654
r, g, b, w = kwargs["rgbw_color"]
55+
# Update the internal L*u*v* state
56+
self.state.update_rgbw(r, g, b, w)
5757
updates.update({ChannelType.RED: r, ChannelType.GREEN: g, ChannelType.BLUE: b, ChannelType.WARM_WHITE: w})
5858

5959
if "rgbww_color" in kwargs:
6060
r, g, b, cw, ww = kwargs["rgbww_color"]
61-
updates.update({ChannelType.RED: r, ChannelType.GREEN: g, ChannelType.BLUE: b,ChannelType.COLD_WHITE: cw, ChannelType.WARM_WHITE: ww})
61+
# Update the internal L*u*v* state
62+
self.state.update_rgbww(r, g, b, cw, ww)
63+
updates.update({ChannelType.RED: r, ChannelType.GREEN: g, ChannelType.BLUE: b, ChannelType.COLD_WHITE: cw, ChannelType.WARM_WHITE: ww})
6264

6365
if "color_temp_kelvin" in kwargs:
6466
kelvin = kwargs["color_temp_kelvin"]
@@ -71,6 +73,12 @@ def _collect_updates_from_kwargs(self, kwargs: Dict[str, Any]) -> Dict[ChannelTy
7173
cw, ww = self.state.converter.temp_to_cw_ww(kelvin, brightness)
7274
updates.update({ChannelType.COLD_WHITE: cw, ChannelType.WARM_WHITE: ww})
7375

76+
if "brightness" in kwargs:
77+
brightness = kwargs["brightness"]
78+
# Treat brightness as L* value and update accordingly
79+
self.state.update_brightness(brightness)
80+
updates.update(self.state.get_scaled_brightness_updates(brightness))
81+
7482
return updates
7583

7684
def _restore_previous_state(self) -> Dict[ChannelType, int]:
@@ -94,7 +102,7 @@ def _restore_previous_state(self) -> Dict[ChannelType, int]:
94102
def _capture_current_state(self) -> dict:
95103
return {
96104
'brightness': self.state.brightness,
97-
'rgb': self.state.rgb,
105+
'luv_color': self.state.luv_color,
98106
'cold_white': self.state.cold_white,
99107
'warm_white': self.state.warm_white,
100108
'color_temp_kelvin': self.state.color_temp_kelvin,
@@ -103,8 +111,8 @@ def _capture_current_state(self) -> dict:
103111

104112
def _save_last_state(self, s: dict):
105113
self.state.last_brightness = s['brightness']
106-
self.state.last_rgb = s['rgb']
114+
self.state.set_last_luv_color(s['luv_color'])
107115
self.state.last_cold_white = s['cold_white']
108116
self.state.last_warm_white = s['warm_white']
109117
self.state.last_color_temp_kelvin = s['color_temp_kelvin']
110-
self.state.last_color_temp_dmx = s['color_temp_dmx']
118+
self.state.last_color_temp_dmx = s['color_temp_dmx']

custom_components/dmx/entity/light/light_entity.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from custom_components.dmx.entity.light import ChannelMapping, ChannelType
1313
from custom_components.dmx.entity.light.color_converter import ColorConverter
1414
from custom_components.dmx.entity.light.light_controller import LightController
15-
from custom_components.dmx.entity.light.light_state import LightState
15+
from custom_components.dmx.entity.light.light_state import LuvLightState
1616
from custom_components.dmx.io.dmx_io import DmxUniverse
1717

1818
log = logging.getLogger(__name__)
@@ -53,7 +53,7 @@ def __init__(
5353
converter = ColorConverter(min_kelvin, max_kelvin)
5454

5555
self.channel_map = {ch.channel_type: ch for ch in channels}
56-
self._state = LightState(color_mode, converter, self.channel_map)
56+
self._state = LuvLightState(color_mode, converter, self.channel_map)
5757
self._controller = LightController(self._state, universe)
5858

5959
self._has_separate_dimmer = has_separate_dimmer
@@ -144,17 +144,19 @@ async def async_added_to_hass(self):
144144

145145
if "brightness" in attrs:
146146
brightness = attrs["brightness"]
147-
self._state.brightness = brightness
147+
# Treat restored brightness as L* value
148+
self._state.update_brightness(brightness)
148149
self._state.last_brightness = brightness
149150

150151
if "rgb_color" in attrs:
151152
rgb = attrs["rgb_color"]
152-
self._state.rgb = rgb
153+
# Update L*u*v* state from restored RGB
154+
self._state.update_rgb(*rgb)
153155
self._state.last_rgb = rgb
154156

155157
if "color_temp" in attrs:
156158
color_temp = attrs["color_temp"]
157-
self._state.color_temp_kelvin = color_temp
159+
self._state.update_color_temp_kelvin(color_temp)
158160
self._state.last_color_temp_kelvin = color_temp
159161

160162
# For color temp mode without dimmer, restore CW/WW values if available
@@ -165,7 +167,6 @@ async def async_added_to_hass(self):
165167
brightness = attrs["brightness"] or 100
166168
color_temp = attrs["color_temp"] or (self.min_color_temp_kelvin + self.max_color_temp_kelvin) / 2
167169
cold, warm = self._state.converter.temp_to_cw_ww(color_temp, brightness)
168-
self._state.cold_white = cold
169-
self._state.warm_white = warm
170+
self._state.update_whites(cold, warm)
170171
self._state.last_cold_white = cold
171-
self._state.last_warm_white = warm
172+
self._state.last_warm_white = warm

0 commit comments

Comments
 (0)