Skip to content
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

miiocli: Fix raw_command parameters (Closes: #335) #336

Merged
merged 1 commit into from
Jun 13, 2018
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
11 changes: 11 additions & 0 deletions miio/click_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This file contains common functions for cli tools.
"""
import ast
import sys
if sys.version_info < (3, 5):
print("To use this script you need python 3.5 or newer, got %s" %
Expand Down Expand Up @@ -93,6 +94,16 @@ def get_metavar(self, param):
return ("_".join(word)).upper()


class LiteralParamType(click.ParamType):
name = 'literal'

def convert(self, value, param, ctx):
try:
return ast.literal_eval(value)
except ValueError:
self.fail('%s is not a valid literal' % value, param, ctx)


class GlobalContextObject:
def __init__(self, debug: int=0, output: callable=None):
self.debug = debug
Expand Down
14 changes: 7 additions & 7 deletions miio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import construct

from .click_common import (
DeviceGroupMeta, command, format_output,
DeviceGroupMeta, command, format_output, LiteralParamType
)
from .exceptions import DeviceException, DeviceError
from .protocol import Message
Expand Down Expand Up @@ -289,17 +289,17 @@ def send(self, command: str, parameters: Any=None, retry_count=3) -> Any:
raise DeviceException("No response from the device") from ex

@command(
click.argument('cmd', required=True),
click.argument('parameters', required=False),
click.argument('command', type=str, required=True),
click.argument('parameters', type=LiteralParamType(), required=False),
)
def raw_command(self, cmd, params):
def raw_command(self, command, parameters):
"""Send a raw command to the device.
This is mostly useful when trying out commands which are not
implemented by a given device instance.
:param str cmd: Command to send
:param dict params: Parameters to send"""
return self.send(cmd, params)
:param str command: Command to send
:param dict parameters: Parameters to send"""
return self.send(command, parameters)

@command(
default_output=format_output(
Expand Down