Skip to content

Commit fb3dce1

Browse files
JnyJnyclaude
andcommitted
fix: correct web API off endpoints and add blink task cancellation
Two issues fixed: 1. **Off endpoints not working**: The BusylightAPI.off() method was marked as async but called synchronous controller methods, causing await calls to fail. - Removed async from BusylightAPI.off() method - Removed await calls from off endpoints and startup/shutdown handlers 2. **Blink tasks not being canceled**: The turn_off() method only called light.off() but didn't cancel running tasks like blink effects. - Added light.cancel_tasks() before light.off() in turn_off() method - Added proper error handling for lights without task support Fixed endpoints: - GET /light/{light_id}/off - now properly turns off individual lights - GET /lights/off - now properly turns off all lights - Startup/shutdown handlers - now work without async issues The off endpoints should now work and properly cancel any running blink tasks. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 9cecb36 commit fb3dce1

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/busylight/api/busylight_api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def update(self) -> None:
9696
def release(self) -> None:
9797
self.controller.release_lights()
9898

99-
async def off(self, light_id: int = None) -> None:
99+
def off(self, light_id: int = None) -> None:
100100
if light_id is None:
101101
self.controller.all().turn_off()
102102
else:
@@ -146,13 +146,13 @@ def authenticate_user(
146146
@busylightapi.on_event("startup")
147147
async def startup():
148148
busylightapi.update()
149-
await busylightapi.off()
149+
busylightapi.off()
150150

151151

152152
@busylightapi.on_event("shutdown")
153153
async def shutdown():
154154
try:
155-
await busylightapi.off()
155+
busylightapi.off()
156156
except Exception:
157157
logger.debug("problem during shutdown: {error}")
158158

@@ -365,7 +365,7 @@ async def light_off(
365365
`color` can be a color name or a hexadecimal string e.g. "red",
366366
"#ff0000", "#f00", "0xff0000", "0xf00", "f00", "ff0000"
367367
"""
368-
await busylightapi.off(light_id)
368+
busylightapi.off(light_id)
369369

370370
return {
371371
"action": "off",
@@ -379,7 +379,7 @@ async def light_off(
379379
)
380380
async def lights_off() -> dict[str, Any]:
381381
"""Turn off all lights."""
382-
await busylightapi.off()
382+
busylightapi.off()
383383

384384
return {
385385
"action": "off",

src/busylight/controller.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ def turn_off(self) -> LightSelection:
4545
"""Turn off selected lights."""
4646
for light in self.lights:
4747
try:
48+
# Cancel any running tasks (like blink) before turning off
49+
light.cancel_tasks()
4850
light.off()
4951
except LightUnavailableError as error:
5052
logger.debug(f"Light unavailable during turn_off: {error}")
53+
except AttributeError:
54+
# Light doesn't support task cancellation, just turn off
55+
light.off()
5156
return self
5257

5358
def blink(

0 commit comments

Comments
 (0)