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
This commit is contained in:
Teemu R 2024-06-23 08:09:13 +02:00 committed by GitHub
parent fd81d073a5
commit 9f14854747
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 38 deletions

View File

@ -403,11 +403,6 @@ async def cli(
"provided or they are ignored\n" "provided or they are ignored\n"
f"discovering for {discovery_timeout} seconds.." f"discovering for {discovery_timeout} seconds.."
) )
else:
echo(
"No --type or --device-family and --encrypt-type defined, "
+ f"discovering for {discovery_timeout} seconds.."
)
dev = await Discover.discover_single( dev = await Discover.discover_single(
host, host,
port=port, port=port,
@ -613,9 +608,7 @@ def _echo_features(
id_: feat for id_, feat in features.items() if feat.category == category id_: feat for id_, feat in features.items() if feat.category == category
} }
if not features: echo(f"{indent}[bold]{title}[/bold]")
return
echo(f"[bold]{title}[/bold]")
for _, feat in features.items(): for _, feat in features.items():
try: try:
echo(f"{indent}{feat}") echo(f"{indent}{feat}")
@ -627,33 +620,40 @@ def _echo_features(
echo(f"{indent}{feat.name} ({feat.id}): [red]got exception ({ex})[/red]") echo(f"{indent}{feat.name} ({feat.id}): [red]got exception ({ex})[/red]")
def _echo_all_features(features, *, verbose=False, title_prefix=None): def _echo_all_features(features, *, verbose=False, title_prefix=None, indent=""):
"""Print out all features by category.""" """Print out all features by category."""
if title_prefix is not None: if title_prefix is not None:
echo(f"[bold]\n\t == {title_prefix} ==[/bold]") echo(f"[bold]\n{indent}== {title_prefix} ==[/bold]")
_echo_features( _echo_features(
features, features,
title="\n\t== Primary features ==", title="== Primary features ==",
category=Feature.Category.Primary, category=Feature.Category.Primary,
verbose=verbose, verbose=verbose,
indent=indent,
) )
echo()
_echo_features( _echo_features(
features, features,
title="\n\t== Information ==", title="== Information ==",
category=Feature.Category.Info, category=Feature.Category.Info,
verbose=verbose, verbose=verbose,
indent=indent,
) )
echo()
_echo_features( _echo_features(
features, features,
title="\n\t== Configuration ==", title="== Configuration ==",
category=Feature.Category.Config, category=Feature.Category.Config,
verbose=verbose, verbose=verbose,
indent=indent,
) )
echo()
_echo_features( _echo_features(
features, features,
title="\n\t== Debug ==", title="== Debug ==",
category=Feature.Category.Debug, category=Feature.Category.Debug,
verbose=verbose, verbose=verbose,
indent=indent,
) )
@ -665,38 +665,42 @@ async def state(ctx, dev: Device):
verbose = ctx.parent.params.get("verbose", False) if ctx.parent else False verbose = ctx.parent.params.get("verbose", False) if ctx.parent else False
echo(f"[bold]== {dev.alias} - {dev.model} ==[/bold]") echo(f"[bold]== {dev.alias} - {dev.model} ==[/bold]")
echo(f"\tHost: {dev.host}") echo(f"Host: {dev.host}")
echo(f"\tPort: {dev.port}") echo(f"Port: {dev.port}")
echo(f"\tDevice state: {dev.is_on}") echo(f"Device state: {dev.is_on}")
echo(f"Time: {dev.time} (tz: {dev.timezone}")
echo(f"Hardware: {dev.hw_info['hw_ver']}")
echo(f"Software: {dev.hw_info['sw_ver']}")
echo(f"MAC (rssi): {dev.mac} ({dev.rssi})")
if verbose:
echo(f"Location: {dev.location}")
_echo_all_features(dev.features, verbose=verbose)
echo()
if dev.children: if dev.children:
echo("\t== Children ==") echo("[bold]== Children ==[/bold]")
for child in dev.children: for child in dev.children:
_echo_all_features( _echo_all_features(
child.features, child.features,
title_prefix=f"{child.alias} ({child.model}, {child.device_type})", title_prefix=f"{child.alias} ({child.model})",
verbose=verbose, verbose=verbose,
indent="\t",
) )
echo() echo()
echo("\t[bold]== Generic information ==[/bold]") if verbose:
echo(f"\tTime: {dev.time} (tz: {dev.timezone}")
echo(f"\tHardware: {dev.hw_info['hw_ver']}")
echo(f"\tSoftware: {dev.hw_info['sw_ver']}")
echo(f"\tMAC (rssi): {dev.mac} ({dev.rssi})")
echo(f"\tLocation: {dev.location}")
_echo_all_features(dev.features, verbose=verbose)
echo("\n\t[bold]== Modules ==[/bold]") echo("\n\t[bold]== Modules ==[/bold]")
for module in dev.modules.values(): for module in dev.modules.values():
echo(f"\t[green]+ {module}[/green]") echo(f"\t[green]+ {module}[/green]")
if verbose:
echo("\n\t[bold]== Protocol information ==[/bold]") echo("\n\t[bold]== Protocol information ==[/bold]")
echo(f"\tCredentials hash: {dev.credentials_hash}") echo(f"\tCredentials hash: {dev.credentials_hash}")
echo() echo()
_echo_discovery_info(dev._discovery_info) _echo_discovery_info(dev._discovery_info)
return dev.internal_state return dev.internal_state
@ -1261,25 +1265,29 @@ async def shell(dev: Device):
@click.argument("value", required=False) @click.argument("value", required=False)
@click.option("--child", required=False) @click.option("--child", required=False)
@pass_dev @pass_dev
async def feature(dev: Device, child: str, name: str, value): @click.pass_context
async def feature(ctx: click.Context, dev: Device, child: str, name: str, value):
"""Access and modify features. """Access and modify features.
If no *name* is given, lists available features and their values. If no *name* is given, lists available features and their values.
If only *name* is given, the value of named feature is returned. If only *name* is given, the value of named feature is returned.
If both *name* and *value* are set, the described setting is changed. If both *name* and *value* are set, the described setting is changed.
""" """
verbose = ctx.parent.params.get("verbose", False) if ctx.parent else False
if child is not None: if child is not None:
echo(f"Targeting child device {child}") echo(f"Targeting child device {child}")
dev = dev.get_child_device(child) dev = dev.get_child_device(child)
if not name: if not name:
_echo_features(dev.features, "\n[bold]== Features ==[/bold]\n", indent="") _echo_all_features(dev.features, verbose=verbose, indent="")
if dev.children: if dev.children:
for child_dev in dev.children: for child_dev in dev.children:
_echo_features( _echo_all_features(
child_dev.features, child_dev.features,
f"\n[bold]== Child {child_dev.alias} ==\n", verbose=verbose,
indent="", title_prefix=f"Child {child_dev.alias}",
indent="\t",
) )
return return

View File

@ -839,7 +839,10 @@ async def test_features_all(discovery_mock, mocker, runner):
["--host", "127.0.0.123", "--debug", "feature"], ["--host", "127.0.0.123", "--debug", "feature"],
catch_exceptions=False, catch_exceptions=False,
) )
assert "== Features ==" in res.output assert "== Primary features ==" in res.output
assert "== Information ==" in res.output
assert "== Configuration ==" in res.output
assert "== Debug ==" in res.output
assert res.exit_code == 0 assert res.exit_code == 0