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 <host> --json' or 'kasa --target <bcast> --json'.
This commit is contained in:
Teemu R 2023-03-30 01:53:38 +02:00 committed by GitHub
parent 6be2f387f0
commit 4d514f983b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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."""