mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-04-26 16:46:23 +00:00
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:
parent
6be2f387f0
commit
4d514f983b
11
kasa/cli.py
11
kasa/cli.py
@ -173,8 +173,7 @@ async def cli(ctx, host, alias, target, debug, type, json):
|
|||||||
|
|
||||||
if host is None:
|
if host is None:
|
||||||
echo("No host name given, trying discovery..")
|
echo("No host name given, trying discovery..")
|
||||||
await ctx.invoke(discover)
|
return await ctx.invoke(discover)
|
||||||
return
|
|
||||||
|
|
||||||
if type is not None:
|
if type is not None:
|
||||||
dev = TYPE_TO_CLASS[type](host)
|
dev = TYPE_TO_CLASS[type](host)
|
||||||
@ -186,7 +185,7 @@ async def cli(ctx, host, alias, target, debug, type, json):
|
|||||||
ctx.obj = dev
|
ctx.obj = dev
|
||||||
|
|
||||||
if ctx.invoked_subcommand is None:
|
if ctx.invoked_subcommand is None:
|
||||||
await ctx.invoke(state)
|
return await ctx.invoke(state)
|
||||||
|
|
||||||
|
|
||||||
@cli.group()
|
@cli.group()
|
||||||
@ -232,18 +231,22 @@ async def discover(ctx, timeout):
|
|||||||
target = ctx.parent.params["target"]
|
target = ctx.parent.params["target"]
|
||||||
echo(f"Discovering devices on {target} for {timeout} seconds")
|
echo(f"Discovering devices on {target} for {timeout} seconds")
|
||||||
sem = asyncio.Semaphore()
|
sem = asyncio.Semaphore()
|
||||||
|
discovered = dict()
|
||||||
|
|
||||||
async def print_discovered(dev: SmartDevice):
|
async def print_discovered(dev: SmartDevice):
|
||||||
await dev.update()
|
await dev.update()
|
||||||
async with sem:
|
async with sem:
|
||||||
|
discovered[dev.host] = dev.internal_state
|
||||||
ctx.obj = dev
|
ctx.obj = dev
|
||||||
await ctx.invoke(state)
|
await ctx.invoke(state)
|
||||||
echo()
|
echo()
|
||||||
|
|
||||||
return await Discover.discover(
|
await Discover.discover(
|
||||||
target=target, timeout=timeout, on_discovered=print_discovered
|
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):
|
async def find_host_from_alias(alias, target="255.255.255.255", timeout=1, attempts=3):
|
||||||
"""Discover a device identified by its alias."""
|
"""Discover a device identified by its alias."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user