Increase rate limit for lighting state requests and enhance error handling for Cync device operations. Improve lyric search processing by splitting lyrics based on line breaks and cleaning special characters (bugfix for subsearch/seek).
This commit is contained in:
@@ -179,7 +179,7 @@ class Lighting(FastAPI):
|
||||
methods=["GET"],
|
||||
include_in_schema=True,
|
||||
dependencies=[
|
||||
Depends(RateLimiter(times=10, seconds=2)),
|
||||
Depends(RateLimiter(times=25, seconds=2)),
|
||||
Depends(get_current_user),
|
||||
],
|
||||
)
|
||||
@@ -190,7 +190,7 @@ class Lighting(FastAPI):
|
||||
methods=["POST"],
|
||||
include_in_schema=True,
|
||||
dependencies=[
|
||||
Depends(RateLimiter(times=10, seconds=2)),
|
||||
Depends(RateLimiter(times=25, seconds=2)),
|
||||
Depends(get_current_user),
|
||||
],
|
||||
)
|
||||
@@ -266,7 +266,7 @@ class Lighting(FastAPI):
|
||||
|
||||
await self.ensure_cync_connection()
|
||||
|
||||
# Apply to Cync device
|
||||
# Validate and extract state values
|
||||
power = state.get("power", "off")
|
||||
if power not in ["on", "off"]:
|
||||
raise HTTPException(
|
||||
@@ -296,41 +296,58 @@ class Lighting(FastAPI):
|
||||
else:
|
||||
rgb = None
|
||||
|
||||
# Use persistent Cync API object
|
||||
if not self.cync_api:
|
||||
raise HTTPException(status_code=500, detail="Cync API not initialized.")
|
||||
devices = self.cync_api.get_devices()
|
||||
if not devices or not isinstance(devices, (list, tuple)):
|
||||
raise HTTPException(
|
||||
status_code=500, detail="No devices returned from Cync API."
|
||||
)
|
||||
light = next(
|
||||
(
|
||||
d
|
||||
for d in devices
|
||||
if hasattr(d, "name") and d.name == self.cync_device_name
|
||||
),
|
||||
None,
|
||||
)
|
||||
if not light:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Device '{self.cync_device_name}' not found",
|
||||
)
|
||||
# Apply to Cync device with retry on connection issues
|
||||
max_retries = 2
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
# Use persistent Cync API object
|
||||
if not self.cync_api:
|
||||
raise Exception("Cync API not initialized.")
|
||||
devices = self.cync_api.get_devices()
|
||||
if not devices or not isinstance(devices, (list, tuple)):
|
||||
raise Exception("No devices returned from Cync API.")
|
||||
light = next(
|
||||
(
|
||||
d
|
||||
for d in devices
|
||||
if hasattr(d, "name") and d.name == self.cync_device_name
|
||||
),
|
||||
None,
|
||||
)
|
||||
if not light:
|
||||
raise Exception(f"Device '{self.cync_device_name}' not found")
|
||||
|
||||
# Set power
|
||||
if power == "on":
|
||||
await light.turn_on()
|
||||
else:
|
||||
await light.turn_off()
|
||||
# Set power
|
||||
if power == "on":
|
||||
await light.turn_on()
|
||||
else:
|
||||
await light.turn_off()
|
||||
|
||||
# Set brightness
|
||||
if "brightness" in state:
|
||||
await light.set_brightness(brightness)
|
||||
# Set brightness
|
||||
if "brightness" in state:
|
||||
await light.set_brightness(brightness)
|
||||
|
||||
# Set color
|
||||
if rgb:
|
||||
await light.set_rgb(rgb)
|
||||
# Set color
|
||||
if rgb:
|
||||
await light.set_rgb(rgb)
|
||||
|
||||
break # Success, exit retry loop
|
||||
except Exception as e:
|
||||
if attempt < max_retries - 1:
|
||||
logging.warning(
|
||||
"Device operation failed (attempt %d/%d): %s. Retrying with reconnection.",
|
||||
attempt + 1,
|
||||
max_retries,
|
||||
e,
|
||||
)
|
||||
await self.ensure_cync_connection()
|
||||
else:
|
||||
logging.error(
|
||||
"Device operation failed after %d attempts: %s",
|
||||
max_retries,
|
||||
e,
|
||||
)
|
||||
raise
|
||||
|
||||
logging.info(
|
||||
"Successfully applied state to device '%s': %s",
|
||||
|
Reference in New Issue
Block a user