Skip to content

Add new cli command 'command' to execute arbitrary commands #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
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
23 changes: 19 additions & 4 deletions kasa/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""python-kasa cli tool."""
import ast
import asyncio
import json
import logging
Expand Down Expand Up @@ -69,6 +70,9 @@ def wrapper(message=None, *args, **kwargs):
device_family_type.value for device_family_type in DeviceFamilyType
]

# Block list of commands which require no update
SKIP_UPDATE_COMMANDS = ["wifi", "raw-command", "command"]

click.anyio_backend = "asyncio"

pass_dev = click.make_pass_decorator(SmartDevice)
Expand Down Expand Up @@ -339,8 +343,9 @@ def _nop_echo(*args, **kwargs):
credentials=credentials,
)

# Skip update for wifi & raw-command, and if factory was used to connect
if ctx.invoked_subcommand not in ["wifi", "raw-command"] and not device_family:
# Skip update on specific commands, or if device factory,
# that performs an update was used for the device.
if ctx.invoked_subcommand not in SKIP_UPDATE_COMMANDS and not device_family:
await dev.update()

ctx.obj = dev
Expand Down Expand Up @@ -592,13 +597,23 @@ async def alias(dev, new_alias, index):

@cli.command()
@pass_dev
@click.pass_context
@click.argument("module")
@click.argument("command")
@click.argument("parameters", default=None, required=False)
async def raw_command(dev: SmartDevice, module, command, parameters):
async def raw_command(ctx, dev: SmartDevice, module, command, parameters):
"""Run a raw command on the device."""
import ast
logging.warning("Deprecated, use 'kasa command --module %s %s'", module, command)
return await ctx.forward(cmd_command)


@cli.command(name="command")
@pass_dev
@click.option("--module", required=False, help="Module for IOT protocol.")
@click.argument("command")
@click.argument("parameters", default=None, required=False)
async def cmd_command(dev: SmartDevice, module, command, parameters):
"""Run a raw command on the device."""
if parameters is not None:
parameters = ast.literal_eval(parameters)

Expand Down
2 changes: 1 addition & 1 deletion kasa/modules/emeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _convert_stat_data(
self,
data: List[Dict[str, Union[int, float]]],
entry_key: str,
kwh: bool=True,
kwh: bool = True,
key: Optional[int] = None,
) -> Dict[Union[int, float], Union[int, float]]:
"""Return emeter information keyed with the day/month.
Expand Down