mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-04-27 09:06:24 +00:00
cli: cleanup discover, fetch update prior device access (#303)
* Use on_discovered for smoother user experience * Remove --discover-only as unnecessary
This commit is contained in:
parent
c865d3f02c
commit
9ea83388ac
20
kasa/cli.py
20
kasa/cli.py
@ -1,4 +1,5 @@
|
|||||||
"""python-kasa cli tool."""
|
"""python-kasa cli tool."""
|
||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
from pprint import pformat as pf
|
from pprint import pformat as pf
|
||||||
from typing import cast
|
from typing import cast
|
||||||
@ -145,24 +146,24 @@ async def join(dev: SmartDevice, ssid, password, keytype):
|
|||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.option("--timeout", default=3, required=False)
|
@click.option("--timeout", default=3, required=False)
|
||||||
@click.option("--discover-only", default=False)
|
|
||||||
@click.option("--dump-raw", is_flag=True)
|
@click.option("--dump-raw", is_flag=True)
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
async def discover(ctx, timeout, discover_only, dump_raw):
|
async def discover(ctx, timeout, dump_raw):
|
||||||
"""Discover devices in the network."""
|
"""Discover devices in the network."""
|
||||||
target = ctx.parent.params["target"]
|
target = ctx.parent.params["target"]
|
||||||
click.echo(f"Discovering devices on {target} for {timeout} seconds")
|
click.echo(f"Discovering devices on {target} for {timeout} seconds")
|
||||||
found_devs = await Discover.discover(target=target, timeout=timeout)
|
sem = asyncio.Semaphore()
|
||||||
if not discover_only:
|
|
||||||
for ip, dev in found_devs.items():
|
async def print_discovered(dev: SmartDevice):
|
||||||
if dump_raw:
|
await dev.update()
|
||||||
click.echo(dev.sys_info)
|
async with sem:
|
||||||
continue
|
|
||||||
ctx.obj = dev
|
ctx.obj = dev
|
||||||
await ctx.invoke(state)
|
await ctx.invoke(state)
|
||||||
click.echo()
|
click.echo()
|
||||||
|
|
||||||
return found_devs
|
await Discover.discover(
|
||||||
|
target=target, timeout=timeout, on_discovered=print_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):
|
||||||
@ -224,7 +225,6 @@ async def state(ctx, dev: SmartDevice):
|
|||||||
click.echo(click.style("\n\t== Device specific information ==", bold=True))
|
click.echo(click.style("\n\t== Device specific information ==", bold=True))
|
||||||
for k, v in dev.state_information.items():
|
for k, v in dev.state_information.items():
|
||||||
click.echo(f"\t{k}: {v}")
|
click.echo(f"\t{k}: {v}")
|
||||||
click.echo()
|
|
||||||
|
|
||||||
if dev.has_emeter:
|
if dev.has_emeter:
|
||||||
click.echo(click.style("\n\t== Current State ==", bold=True))
|
click.echo(click.style("\n\t== Current State ==", bold=True))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user