import asyncio import re async def set_led(device: str, state: str) -> dict: """Set the locate LED on a drive via ledctl. Args: device: Block device name (e.g. "sda"). Must be alphanumeric. state: "locate" to turn on the locate LED, "off" to turn it off. """ if not re.fullmatch(r"[a-zA-Z0-9]+", device): raise ValueError(f"Invalid device name: {device}") if state not in ("locate", "off"): raise ValueError(f"Invalid state: {state}") proc = await asyncio.create_subprocess_exec( "ledctl", f"{state}=/dev/{device}", stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ) stdout, stderr = await proc.communicate() if proc.returncode != 0: err = stderr.decode().strip() or stdout.decode().strip() raise RuntimeError(f"ledctl failed (rc={proc.returncode}): {err}") return {"device": device, "state": state}