Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions miio/click_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def supported_models(cls) -> list[str]:
return list(cls._mappings.keys()) or cls._supported_models


class DeviceGroup(click.MultiCommand):
class DeviceGroup(click.Group):
class Command:
def __init__(self, name, decorators, *, default_output=None, **kwargs):
self.name = name
Expand Down Expand Up @@ -238,8 +238,8 @@ def __init__(
result_callback_pass_device=True,
**attrs,
):
self.commands = getattr(device_class, "_device_group_commands", None)
if self.commands is None:
device_commands = getattr(device_class, "_device_group_commands", None)
if device_commands is None:
raise RuntimeError(
"Class {} doesn't use DeviceGroupMeta meta class."
" It can't be used with DeviceGroup."
Expand All @@ -248,6 +248,10 @@ def __init__(
self.device_class = device_class
self.device_pass = click.make_pass_decorator(device_class)

# Default to running status instead of showing help when called with no subcommand.
if no_args_is_help is None and "status" in device_commands:
no_args_is_help = False

attrs.setdefault("params", self.DEFAULT_PARAMS)
attrs.setdefault("callback", click.pass_context(self.group_callback))
if result_callback_pass_device and callable(result_callback):
Expand All @@ -263,6 +267,9 @@ def __init__(
**attrs,
)

# Preserve our command dict, which click.Group resets in __init__
self.commands = device_commands

def group_callback(self, ctx, *args, **kwargs):
gco = ctx.find_object(GlobalContextObject)
if gco:
Expand All @@ -282,6 +289,11 @@ def _save_cache() -> None:

ctx.call_on_close(_save_cache)

def invoke(self, ctx):
if not ctx._protected_args and not ctx.args and "status" in self.commands:
ctx._protected_args = ["status"]
return super().invoke(ctx)

def command_callback(self, miio_command, miio_device, *args, **kwargs):
return miio_command.call(miio_device, *args, **kwargs)

Expand Down
Loading