3
3
from json import loads as json_loads
4
4
from os import environ
5
5
from secrets import compare_digest
6
- from typing import Any , Callable , Dict , List
6
+ from typing import Any , Callable
7
7
8
8
from busylight_core import Light , LightUnavailableError , NoLightsFoundError
9
9
from fastapi import Depends , FastAPI , HTTPException , Path , Request , status
@@ -82,10 +82,10 @@ def __init__(self):
82
82
dependencies = dependencies ,
83
83
)
84
84
self .controller = LightController ()
85
- self .endpoints : List [str ] = []
85
+ self .endpoints : list [str ] = []
86
86
87
87
@property
88
- def lights (self ) -> List [Light ]:
88
+ def lights (self ) -> list [Light ]:
89
89
"""Get all lights for compatibility."""
90
90
return self .controller .lights
91
91
@@ -219,11 +219,11 @@ async def light_manager_update(request: Request, call_next):
219
219
220
220
## GET API Routes
221
221
##
222
- @busylightapi .get ("/" , response_model = List [EndPoint ])
223
- async def available_endpoints () -> List [ Dict [str , str ]]:
222
+ @busylightapi .get ("/" , response_model = list [EndPoint ])
223
+ async def available_endpoints () -> list [ dict [str , str ]]:
224
224
"""API endpoint listing.
225
225
226
- List of valid endpoints recognized by this API.
226
+ list of valid endpoints recognized by this API.
227
227
"""
228
228
return [{"path" : endpoint } for endpoint in busylightapi .endpoints ]
229
229
@@ -238,37 +238,56 @@ async def available_endpoints() -> List[Dict[str, str]]:
238
238
)
239
239
async def light_status (
240
240
light_id : int = Path (..., title = "Numeric light identifier" , ge = 0 ),
241
- ) -> Dict [str , Any ]:
241
+ ) -> dict [str , Any ]:
242
242
"""Information about the light selected by `light_id`."""
243
243
light = busylightapi .lights [light_id ]
244
+
244
245
return {
245
246
"light_id" : light_id ,
246
247
"name" : light .name ,
247
- "info" : light .hardware ,
248
- "is_on" : light .is_on ,
248
+ "info" : {
249
+ "path" : light .hardware .path ,
250
+ "vendor_id" : light .hardware .vendor_id ,
251
+ "product_id" : light .hardware .product_id ,
252
+ "serial_number" : light .hardware .serial_number ,
253
+ "manufacturer_string" : light .hardware .manufacturer_string ,
254
+ "product_string" : light .hardware .product_string ,
255
+ "release_number" : light .hardware .release_number ,
256
+ "is_acquired" : light .hardware .is_acquired ,
257
+ },
258
+ "is_on" : light .is_lit ,
249
259
"color" : colortuple_to_name (light .color ),
250
260
"rgb" : light .color ,
251
261
}
252
262
253
263
254
264
@busylightapi .get (
255
265
"/lights/status" ,
256
- response_model = List [LightDescription ],
266
+ response_model = list [LightDescription ],
257
267
)
258
268
@busylightapi .get (
259
269
"/lights" ,
260
- response_model = List [LightDescription ],
270
+ response_model = list [LightDescription ],
261
271
)
262
- async def lights_status () -> List [ Dict [str , Any ]]:
272
+ async def lights_status () -> list [ dict [str , Any ]]:
263
273
"""Information about all available lights."""
264
274
result = []
265
275
for light_id , light in enumerate (busylightapi .lights ):
266
276
result .append (
267
277
{
268
278
"light_id" : light_id ,
269
279
"name" : light .name ,
270
- "info" : light .hardware ,
271
- "is_on" : light .is_on ,
280
+ "info" : {
281
+ "path" : light .hardware .path ,
282
+ "vendor_id" : light .hardware .vendor_id ,
283
+ "product_id" : light .hardware .product_id ,
284
+ "serial_number" : light .hardware .serial_number ,
285
+ "manufacturer_string" : light .hardware .manufacturer_string ,
286
+ "product_string" : light .hardware .product_string ,
287
+ "release_number" : light .hardware .release_number ,
288
+ "is_acquired" : light .hardware .is_acquired ,
289
+ },
290
+ "is_on" : light .is_lit ,
272
291
"color" : colortuple_to_name (light .color ),
273
292
"rgb" : light .color ,
274
293
},
@@ -284,7 +303,7 @@ async def light_on(
284
303
light_id : int = Path (..., title = "Numeric light identifier" , ge = 0 ),
285
304
color : str = "green" ,
286
305
dim : float = 1.0 ,
287
- ) -> Dict [str , Any ]:
306
+ ) -> dict [str , Any ]:
288
307
"""Turn on the specified light with the given `color`.
289
308
290
309
`light_id` is an integer value identifying a light and ranges
@@ -313,7 +332,7 @@ async def light_on(
313
332
async def lights_on (
314
333
color : str = "green" ,
315
334
dim : float = 1.0 ,
316
- ) -> Dict [str , Any ]:
335
+ ) -> dict [str , Any ]:
317
336
"""Turn on all lights with the given `color`.
318
337
319
338
`color` can be a color name or a hexadecimal string e.g. "red",
@@ -338,7 +357,7 @@ async def lights_on(
338
357
)
339
358
async def light_off (
340
359
light_id : int = Path (..., title = "Numeric light identifier" , ge = 0 ),
341
- ) -> Dict [str , Any ]:
360
+ ) -> dict [str , Any ]:
342
361
"""Turn off the specified light.
343
362
`light_id` is an integer value identifying a light and ranges
344
363
between zero and number_of_lights-1.
@@ -358,7 +377,7 @@ async def light_off(
358
377
"/lights/off" ,
359
378
response_model = LightOperation ,
360
379
)
361
- async def lights_off () -> Dict [str , Any ]:
380
+ async def lights_off () -> dict [str , Any ]:
362
381
"""Turn off all lights."""
363
382
await busylightapi .off ()
364
383
@@ -378,7 +397,7 @@ async def blink_light(
378
397
speed : Speed = Speed .Slow ,
379
398
dim : float = 1.0 ,
380
399
count : int = 0 ,
381
- ) -> Dict [str , Any ]:
400
+ ) -> dict [str , Any ]:
382
401
"""Start blinking the specified light: color and off.
383
402
384
403
`light_id` is an integer value identifying a light and ranges
@@ -391,9 +410,9 @@ async def blink_light(
391
410
"""
392
411
rgb = parse_color_string (color , dim )
393
412
394
- effect = Effects . for_name ( "blink" )( rgb , speed . duty_cycle , count = count )
395
-
396
- await busylightapi . apply_effect ( effect , light_id )
413
+ # Use controller's fluent API
414
+ selection = busylightapi . controller . by_index ( light_id )
415
+ selection . blink ( rgb , count = count , speed = speed . name . lower () )
397
416
398
417
return {
399
418
"action" : "blink" ,
@@ -415,20 +434,20 @@ async def blink_lights(
415
434
speed : Speed = Speed .Slow ,
416
435
dim : float = 1.0 ,
417
436
count : int = 0 ,
418
- ) -> Dict [str , Any ]:
437
+ ) -> dict [str , Any ]:
419
438
"""Start blinking all the lights: red and off
420
439
<p>Note: lights will not be synchronized.</p>
421
440
"""
422
441
rgb = parse_color_string (color , dim )
423
442
424
- blink = Effects . for_name ( "blink" )( rgb , speed . duty_cycle , count = count )
425
-
426
- await busylightapi . apply_effect ( blink )
443
+ # Use controller's fluent API
444
+ selection = busylightapi . controller . all ()
445
+ selection . blink ( rgb , count = count , speed = speed . name . lower () )
427
446
428
447
return {
429
448
"action" : "blink" ,
430
449
"light_id" : "all" ,
431
- "color" : "red" ,
450
+ "color" : color ,
432
451
"rgb" : rgb ,
433
452
"speed" : speed ,
434
453
"dim" : dim ,
@@ -444,7 +463,7 @@ async def rainbow_light(
444
463
light_id : int = Path (..., title = "Numeric light identifier" , ge = 0 ),
445
464
speed : Speed = Speed .Slow ,
446
465
dim : float = 1.0 ,
447
- ) -> Dict [str , Any ]:
466
+ ) -> dict [str , Any ]:
448
467
"""Start a rainbow animation on the specified light.
449
468
450
469
`light_id` is an integer value identifying a light and ranges
@@ -470,7 +489,7 @@ async def rainbow_light(
470
489
async def rainbow_lights (
471
490
speed : Speed = Speed .Slow ,
472
491
dim : float = 1.0 ,
473
- ) -> Dict [str , Any ]:
492
+ ) -> dict [str , Any ]:
474
493
"""Start a rainbow animation on all lights.
475
494
<p><em>Note:</em> lights will not be synchronized.</p>
476
495
"""
@@ -497,7 +516,7 @@ async def flash_light_impressively(
497
516
speed : Speed = Speed .Slow ,
498
517
dim : float = 1.0 ,
499
518
count : int = 0 ,
500
- ) -> Dict [str , Any ]:
519
+ ) -> dict [str , Any ]:
501
520
"""Flash the specified light impressively [default: red/blue].
502
521
503
522
`light_id` is an integer value identifying a light and ranges
@@ -573,7 +592,7 @@ async def pulse_light(
573
592
speed : Speed = Speed .Slow ,
574
593
dim : float = 1.0 ,
575
594
count : int = 0 ,
576
- ) -> Dict [str , Any ]:
595
+ ) -> dict [str , Any ]:
577
596
"""Pulse a light with a specified color [default: red].
578
597
579
598
`light_id` is an integer value identifying a light and ranges
0 commit comments