From 4d514f983bbe7c3a060abc67bddcf6056df6320e Mon Sep 17 00:00:00 2001 From: Teemu R Date: Thu, 30 Mar 2023 01:53:38 +0200 Subject: [PATCH] Return result objects for cli discover and implicit 'state' (#446) This will make --json to output relevant contents even when no command is defined (i.e., when calling 'kasa --host --json' or 'kasa --target --json'. --- kasa/cli.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/kasa/cli.py b/kasa/cli.py index 35599476..48ce039c 100755 --- a/kasa/cli.py +++ b/kasa/cli.py @@ -173,8 +173,7 @@ async def cli(ctx, host, alias, target, debug, type, json): if host is None: echo("No host name given, trying discovery..") - await ctx.invoke(discover) - return + return await ctx.invoke(discover) if type is not None: dev = TYPE_TO_CLASS[type](host) @@ -186,7 +185,7 @@ async def cli(ctx, host, alias, target, debug, type, json): ctx.obj = dev if ctx.invoked_subcommand is None: - await ctx.invoke(state) + return await ctx.invoke(state) @cli.group() @@ -232,18 +231,22 @@ async def discover(ctx, timeout): target = ctx.parent.params["target"] echo(f"Discovering devices on {target} for {timeout} seconds") sem = asyncio.Semaphore() + discovered = dict() async def print_discovered(dev: SmartDevice): await dev.update() async with sem: + discovered[dev.host] = dev.internal_state ctx.obj = dev await ctx.invoke(state) echo() - return await Discover.discover( + await Discover.discover( target=target, timeout=timeout, on_discovered=print_discovered ) + return discovered + async def find_host_from_alias(alias, target="255.255.255.255", timeout=1, attempts=3): """Discover a device identified by its alias."""