Skip to content

Commit

Permalink
miiocli: Fix raw_command parameters (Closes: #335) (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Jun 13, 2018
1 parent 3f132b2 commit a68eba5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
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

0 comments on commit a68eba5

Please sign in to comment.