@@ -32,11 +32,6 @@ def _get_led():
3232 return _led_dispatcher
3333
3434
35- def _led_route (method : str , * args , ** kwargs ) -> dict :
36- """Generic: get LEDDevice, call method, return dispatch result."""
37- return dispatch_result (getattr (_get_led (), method )(* args , ** kwargs ))
38-
39-
4035# ── Global operations ──────────────────────────────────────────────────
4136
4237@router .post ("/color" )
@@ -48,17 +43,28 @@ def set_color(body: HexColorRequest) -> dict:
4843
4944 stop_led_loop ()
5045 r , g , b = parse_hex_or_400 (body .hex )
51- led = _get_led ()
52- result = TrccApp .get ().build_led_bus ( led ) .dispatch (SetLEDColorCommand (r = r , g = g , b = b ))
46+ _get_led ()
47+ result = TrccApp .get ().led_bus .dispatch (SetLEDColorCommand (r = r , g = g , b = b ))
5348 return dispatch_result (result .payload )
5449
5550
5651@router .post ("/mode" )
5752def set_mode (body : ModeRequest ) -> dict :
5853 """Set LED effect mode (static, breathing, colorful, rainbow, temp_linked, load_linked)."""
5954 from trcc .api import start_led_loop , stop_led_loop
55+ from trcc .core .app import TrccApp
56+ from trcc .core .commands .led import SetLEDModeCommand
57+ from trcc .core .models import LEDMode
6058
61- result = _led_route ("set_mode" , body .mode )
59+ _get_led ()
60+ try :
61+ mode = LEDMode [body .mode .upper ()]
62+ except KeyError :
63+ raise HTTPException (
64+ status_code = 400 ,
65+ detail = f"Unknown mode '{ body .mode } '. Choose: { ', ' .join (m .name .lower () for m in LEDMode )} " ,
66+ )
67+ result = dispatch_result (TrccApp .get ().led_bus .dispatch (SetLEDModeCommand (mode = mode )).payload )
6268 if result .get ("animated" ):
6369 start_led_loop ()
6470 else :
@@ -72,18 +78,22 @@ def set_brightness(body: LEDBrightnessRequest) -> dict:
7278 from trcc .core .app import TrccApp
7379 from trcc .core .commands .led import SetLEDBrightnessCommand
7480
75- led = _get_led ()
76- result = TrccApp .get ().build_led_bus ( led ) .dispatch (SetLEDBrightnessCommand (level = body .level ))
81+ _get_led ()
82+ result = TrccApp .get ().led_bus .dispatch (SetLEDBrightnessCommand (level = body .level ))
7783 return dispatch_result (result .payload )
7884
7985
8086@router .post ("/off" )
8187def turn_off () -> dict :
8288 """Turn LEDs off."""
8389 from trcc .api import stop_led_loop
90+ from trcc .core .app import TrccApp
91+ from trcc .core .commands .led import ToggleLEDCommand
8492
8593 stop_led_loop ()
86- return _led_route ("off" )
94+ _get_led ()
95+ result = TrccApp .get ().led_bus .dispatch (ToggleLEDCommand (on = False ))
96+ return dispatch_result (result .payload )
8797
8898
8999@router .post ("/sensor" )
@@ -92,8 +102,8 @@ def set_sensor(body: LEDSensorRequest) -> dict:
92102 from trcc .core .app import TrccApp
93103 from trcc .core .commands .led import SetLEDSensorSourceCommand
94104
95- led = _get_led ()
96- result = TrccApp .get ().build_led_bus ( led ) .dispatch (SetLEDSensorSourceCommand (source = body .source ))
105+ _get_led ()
106+ result = TrccApp .get ().led_bus .dispatch (SetLEDSensorSourceCommand (source = body .source ))
97107 return dispatch_result (result .payload )
98108
99109
@@ -106,53 +116,96 @@ def set_zone_color(zone: int, body: HexColorRequest) -> dict:
106116 from trcc .core .commands .led import SetZoneColorCommand
107117
108118 r , g , b = parse_hex_or_400 (body .hex )
109- led = _get_led ()
110- result = TrccApp .get ().build_led_bus ( led ) .dispatch (SetZoneColorCommand (zone = zone , r = r , g = g , b = b ))
119+ _get_led ()
120+ result = TrccApp .get ().led_bus .dispatch (SetZoneColorCommand (zone = zone , r = r , g = g , b = b ))
111121 return dispatch_result (result .payload )
112122
113123
114124@router .post ("/zones/{zone}/mode" )
115125def set_zone_mode (zone : int , body : ModeRequest ) -> dict :
116126 """Set effect mode for a specific LED zone."""
117- return _led_route ("set_zone_mode" , zone , body .mode )
127+ from trcc .core .app import TrccApp
128+ from trcc .core .commands .led import SetZoneModeCommand
129+ from trcc .core .models import LEDMode
130+
131+ _get_led ()
132+ try :
133+ mode = LEDMode [body .mode .upper ()]
134+ except KeyError :
135+ raise HTTPException (
136+ status_code = 400 ,
137+ detail = f"Unknown mode '{ body .mode } '. Choose: { ', ' .join (m .name .lower () for m in LEDMode )} " ,
138+ )
139+ result = TrccApp .get ().led_bus .dispatch (SetZoneModeCommand (zone = zone , mode = mode ))
140+ return dispatch_result (result .payload )
118141
119142
120143@router .post ("/zones/{zone}/brightness" )
121144def set_zone_brightness (zone : int , body : LEDBrightnessRequest ) -> dict :
122145 """Set brightness for a specific LED zone (0-100)."""
123- return _led_route ("set_zone_brightness" , zone , body .level )
146+ from trcc .core .app import TrccApp
147+ from trcc .core .commands .led import SetZoneBrightnessCommand
148+
149+ _get_led ()
150+ result = TrccApp .get ().led_bus .dispatch (SetZoneBrightnessCommand (zone = zone , level = body .level ))
151+ return dispatch_result (result .payload )
124152
125153
126154@router .post ("/zones/{zone}/toggle" )
127155def toggle_zone (zone : int , body : ToggleRequest ) -> dict :
128156 """Toggle a specific LED zone on/off."""
129- return _led_route ("toggle_zone" , zone , body .on )
157+ from trcc .core .app import TrccApp
158+ from trcc .core .commands .led import ToggleZoneCommand
159+
160+ _get_led ()
161+ result = TrccApp .get ().led_bus .dispatch (ToggleZoneCommand (zone = zone , on = body .on ))
162+ return dispatch_result (result .payload )
130163
131164
132165@router .post ("/sync" )
133166def set_sync (body : ZoneSyncRequest ) -> dict :
134167 """Enable/disable zone sync (circulate/select-all)."""
135- return _led_route ("set_zone_sync" , body .enabled , body .interval )
168+ from trcc .core .app import TrccApp
169+ from trcc .core .commands .led import SetZoneSyncCommand
170+
171+ _get_led ()
172+ result = TrccApp .get ().led_bus .dispatch (SetZoneSyncCommand (enabled = body .enabled , interval = body .interval or 0 ))
173+ return dispatch_result (result .payload )
136174
137175
138176# ── Segment operations ─────────────────────────────────────────────────
139177
140178@router .post ("/segments/{index}/toggle" )
141179def toggle_segment (index : int , body : ToggleRequest ) -> dict :
142180 """Toggle a specific LED segment on/off."""
143- return _led_route ("toggle_segment" , index , body .on )
181+ from trcc .core .app import TrccApp
182+ from trcc .core .commands .led import ToggleSegmentCommand
183+
184+ _get_led ()
185+ result = TrccApp .get ().led_bus .dispatch (ToggleSegmentCommand (index = index , on = body .on ))
186+ return dispatch_result (result .payload )
144187
145188
146189@router .post ("/clock" )
147190def set_clock (body : ClockFormatRequest ) -> dict :
148191 """Set LED segment display clock format (12h/24h)."""
149- return _led_route ("set_clock_format" , body .is_24h )
192+ from trcc .core .app import TrccApp
193+ from trcc .core .commands .led import SetClockFormatCommand
194+
195+ _get_led ()
196+ result = TrccApp .get ().led_bus .dispatch (SetClockFormatCommand (is_24h = body .is_24h ))
197+ return dispatch_result (result .payload )
150198
151199
152200@router .post ("/temp-unit" )
153201def set_temp_unit (body : TempUnitRequest ) -> dict :
154202 """Set LED segment display temperature unit (C/F)."""
155- return _led_route ("set_temp_unit" , body .unit )
203+ from trcc .core .app import TrccApp
204+ from trcc .core .commands .led import SetTempUnitLEDCommand
205+
206+ _get_led ()
207+ result = TrccApp .get ().led_bus .dispatch (SetTempUnitLEDCommand (unit = body .unit ))
208+ return dispatch_result (result .payload )
156209
157210
158211# ── Test endpoint ─────────────────────────────────────────────────────
0 commit comments