|
| 1 | +"""Asynchronous Python client for WLED.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +from rich.console import Console |
| 6 | +from rich.live import Live |
| 7 | +from rich.table import Table |
| 8 | +from zeroconf import ServiceStateChange, Zeroconf |
| 9 | +from zeroconf.asyncio import AsyncServiceBrowser, AsyncServiceInfo, AsyncZeroconf |
| 10 | + |
| 11 | +from .async_typer import AsyncTyper |
| 12 | + |
| 13 | +cli = AsyncTyper(help="WLED CLI", no_args_is_help=True, add_completion=False) |
| 14 | +console = Console() |
| 15 | + |
| 16 | + |
| 17 | +@cli.command("scan") |
| 18 | +async def test() -> None: |
| 19 | + """Scan for WLED devices on the network.""" |
| 20 | + zeroconf = AsyncZeroconf() |
| 21 | + background_tasks = set() |
| 22 | + |
| 23 | + table = Table( |
| 24 | + title="\n\nFound WLED devices", header_style="cyan bold", show_lines=True |
| 25 | + ) |
| 26 | + table.add_column("Addresses") |
| 27 | + table.add_column("MAC Address") |
| 28 | + |
| 29 | + def async_on_service_state_change( |
| 30 | + zeroconf: Zeroconf, |
| 31 | + service_type: str, |
| 32 | + name: str, |
| 33 | + state_change: ServiceStateChange, |
| 34 | + ) -> None: |
| 35 | + """Handle service state changes.""" |
| 36 | + if state_change is not ServiceStateChange.Added: |
| 37 | + return |
| 38 | + |
| 39 | + future = asyncio.ensure_future( |
| 40 | + async_display_service_info(zeroconf, service_type, name) |
| 41 | + ) |
| 42 | + background_tasks.add(future) |
| 43 | + future.add_done_callback(background_tasks.discard) |
| 44 | + |
| 45 | + async def async_display_service_info( |
| 46 | + zeroconf: Zeroconf, service_type: str, name: str |
| 47 | + ) -> None: |
| 48 | + """Retrieve and display service info.""" |
| 49 | + info = AsyncServiceInfo(service_type, name) |
| 50 | + await info.async_request(zeroconf, 3000) |
| 51 | + if info is None: |
| 52 | + return |
| 53 | + |
| 54 | + console.print(f"[cyan bold]Found service {info.server}: is a WLED device 🎉") |
| 55 | + |
| 56 | + table.add_row( |
| 57 | + f"{str(info.server).rstrip('.')}\n" |
| 58 | + + ", ".join(info.parsed_scoped_addresses()), |
| 59 | + info.properties[b"mac"].decode(), # type: ignore[union-attr] |
| 60 | + ) |
| 61 | + |
| 62 | + console.print("[green]Scanning for WLED devices...") |
| 63 | + console.print("[red]Press Ctrl-C to exit\n") |
| 64 | + |
| 65 | + with Live(table, console=console, refresh_per_second=4): |
| 66 | + browser = AsyncServiceBrowser( |
| 67 | + zeroconf.zeroconf, |
| 68 | + "_wled._tcp.local.", |
| 69 | + handlers=[async_on_service_state_change], |
| 70 | + ) |
| 71 | + |
| 72 | + try: |
| 73 | + while True: |
| 74 | + await asyncio.sleep(0.5) |
| 75 | + except KeyboardInterrupt: |
| 76 | + pass |
| 77 | + finally: |
| 78 | + console.print("\n[green]Control-C pressed, stopping scan") |
| 79 | + await browser.async_cancel() |
| 80 | + await zeroconf.async_close() |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + cli() |
0 commit comments