8000 Cleanup cli output (#1000) · python-kasa/python-kasa@9f14854 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f14854

Browse files
authored
Cleanup cli output (#1000)
Avoid unnecessary indentation of elements, now only the child device information is indented Use _echo_all_features consistently for both state and feature Avoid discovery log message which brings no extra value Hide location by default
1 parent fd81d07 commit 9f14854

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

kasa/cli.py

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,6 @@ def _nop_echo(*args, **kwargs):
403403
"provided or they are ignored\n"
404404
f"discovering for {discovery_timeout} seconds.."
405405
)
406-
else:
407-
echo(
408-
"No --type or --device-family and --encrypt-type defined, "
409-
+ f"discovering for {discovery_timeout} seconds.."
410-
)
411406
dev = await Discover.discover_single(
412407
host,
413408
port=port,
@@ -613,9 +608,7 @@ def _echo_features(
613608
id_: feat for id_, feat in features.items() if feat.category == category
614609
}
615610

616-
if not features:
617-
return
618-
echo(f"[bold]{title}[/bold]")
611+
echo(f"{indent}[bold]{title}[/bold]")
619612
for _, feat in features.items():
620613
try:
621614
echo(f"{indent}{feat}")
@@ -627,33 +620,40 @@ def _echo_features(
627620
echo(f"{indent}{feat.name} ({feat.id}): [red]got exception ({ex})[/red]")
628621

629622

630-
def _echo_all_features(features, *, verbose=False, title_prefix=None):
623+
def _echo_all_features(features, *, verbose=False, title_prefix=None, indent=""):
631624
"""Print out all features by category."""
632625
if title_prefix is not None:
633-
echo(f"[bold]\n\t == {title_prefix} ==[/bold]")
626+
echo(f"[bold]\n{indent}== {title_prefix} ==[/bold]")
634627
_echo_features(
635628
features,
636-
title="\n\t== Primary features ==",
629+
title="== Primary features ==",
637630
category=Feature.Category.Primary,
638631
verbose=verbose,
632+
indent=indent,
639633
)
634+
echo()
640635
_echo_features(
641636
features,
642-
title="\n\t== Information ==",
637+
title="== Information ==",
643638
category=Feature.Category.Info,
644639
verbose=verbose,
640+
indent=indent,
645641
)
642+
echo()
646643
_echo_features(
647644
features,
648-
title="\n\t== Configuration ==",
645+
title="== Configuration ==",
649646
category=Feature.Category.Config,
650647
verbose=verbose,
648+
indent=indent,
651649
)
650+
echo()
652651
_echo_features(
653652
features,
654-
title="\n\t== Debug ==",
653+
title="== Debug ==",
655654
category=Feature.Category.Debug,
656655
verbose=verbose,
656+
indent=indent,
657657
)
658658

659659

@@ -665,38 +665,42 @@ async def state(ctx, dev: Device):
665665
verbose = ctx.parent.params.get("verbose", False) if ctx.parent else False
666666

667667
echo(f"[bold]== {dev.alias} - {dev.model} ==[/bold]")
668-
echo(f"\tHost: {dev.host}")
669-
echo(f"\tPort: {dev.port}")
670-
echo(f"\tDevice state: {dev.is_on}")
668+
echo(f"Host: {dev.host}")
669+
echo(f"Port: {dev.port}")
670+
echo(f"Device state: {dev.is_on}")
671+
672+
echo(f"Time: {dev.time} (tz: {dev.timezone}")
673+
echo(f"Hardware: {dev.hw_info['hw_ver']}")
674+
echo(f"Software: {dev.hw_info['sw_ver']}")
675+
echo(f"MAC (rssi): {dev.mac} ({dev.rssi})")
676+
if verbose:
677+
echo(f"Location: {dev.location}")
678+
679+
_echo_all_features(dev.features, verbose=verbose)
680+
echo()
681+
671682
if dev.children:
672-
echo("\t== Children ==")
683+
echo("[bold]== Children ==[/bold]")
673684
for child in dev.children:
674685
_echo_all_features(
675686
child.features,
676-
title_prefix=f"{child.alias} ({child.model}, {child.device_type})",
687+
title_prefix=f"{child.alias} ({child.model})",
677688
verbose=verbose,
689+
indent="\t",
678690
)
679691

680692
echo()
681693

682-
echo("\t[bold]== Generic information ==[/bold]")
683-
echo(f"\tTime: {dev.time} (tz: {dev.timezone}")
684-
echo(f"\tHardware: {dev.hw_info['hw_ver']}")
685-
echo(f"\tSoftware: {dev.hw_info['sw_ver']}")
686-
echo(f"\tMAC (rssi): {dev.mac} ({dev.rssi})")
687-
echo(f"\tLocation: {dev.location}")
688-
689-
_echo_all_features(dev.features, verbose=verbose)
690-
691-
echo("\n\t[bold]== Modules ==[/bold]")
692-
for module in dev.modules.values():
693-
echo(f"\t[green]+ {module}[/green]")
694-
695694
if verbose:
695+
echo("\n\t[bold]== Modules ==[/bold]")
696+
for module in dev.modules.values():
697+
echo(f"\t[green]+ {module}[/green]")
698+
696699
echo("\n\t[bold]== Protocol information ==[/bold]")
697700
echo(f"\tCredentials hash: {dev.credentials_hash}")
698701
echo()
699702
_echo_discovery_info(dev._discovery_info)
703+
700704
return dev.internal_state
701705

702706

@@ -1261,25 +1265,29 @@ async def shell(dev: Device):
12611265
@click.argument("value", required=False)
12621266
@click.option("--child", required=False)
12631267
@pass_dev
1264-
async def feature(dev: Device, child: str, name: str, value):
1268+
@click.pass_context
1269+
async def feature(ctx: click.Context, dev: Device, child: str, name: str, value):
12651270
"""Access and modify features.
12661271
12671272
If no *name* is given, lists available features and their values.
12681273
If only *name* is given, the value of named feature is returned.
12691274
If both *name* and *value* are set, the described setting is changed.
12701275
"""
1276+
verbose = ctx.parent.params.get("verbose", False) if ctx.parent else False
1277+
12711278
if child is not None:
12721279
echo(f"Targeting child device {child}")
12731280
dev = dev.get_child_device(child)
12741281
if not name:
1275-
_echo_features(dev.features, "\n[bold]== Features ==[/bold]\n", indent="")
1282+
_echo_all_features(dev.features, verbose=verbose, indent="")
12761283

12771284
if dev.children:
12781285
for child_dev in dev.children:
1279-
_echo_features(
1286+
_echo_all_features(
12801287
child_dev.features,
1281-
f"\n[bold]== Child {child_dev.alias} ==\n",
1282-
indent="",
1288+
verbose=verbose,
1289+
title_prefix=f"Child {child_dev.alias}",
1290+
indent="\t",
12831291
)
12841292

12851293
return

kasa/tests/test_cli.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,10 @@ async def test_features_all(discovery_mock, mocker, runner):
839839
["--host", "127.0.0.123", "--debug", "feature"],
840840
catch_exceptions=False,
841841
)
842-
assert "== Features ==" in res.output
842+
assert "== Primary features ==" in res.output
843+
assert "== Information ==" in res.output
844+
assert "== Configuration ==" in res.output
845+
assert "== Debug ==" in res.output
843846
assert res.exit_code == 0
844847

845848

0 commit comments

Comments
 (0)
0