Skip to content

Commit 16bb6e4

Browse files
committed
Introduce new lick parameter type for hexadecimal strings
1 parent 8d9e584 commit 16bb6e4

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

miio/airconditioningcompanion.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import click
66

7-
from .click_common import command, format_output, EnumType
7+
from .click_common import command, format_output, EnumType, HexStringParamType
88
from .device import Device, DeviceException
99

1010
_LOGGER = logging.getLogger(__name__)
@@ -310,8 +310,8 @@ def learn_stop(self, slot: int=STORAGE_SLOT_ID):
310310
return self.send("end_ir_learn", [slot])
311311

312312
@command(
313-
click.argument("model", type=bytes),
314-
click.argument("code", type=bytes),
313+
click.argument("model", type=HexStringParamType),
314+
click.argument("code", type=HexStringParamType),
315315
default_output=format_output("Sending the supplied infrared command")
316316
)
317317
def send_ir_code(self, model: bytes, code: bytes, slot: int=0):
@@ -346,7 +346,7 @@ def send_command(self, command: str):
346346
return self.send("send_cmd", [str(command)])
347347

348348
@command(
349-
click.argument("model", type=str),
349+
click.argument("model", type=HexStringParamType),
350350
click.argument("power", type=EnumType(Power, False)),
351351
click.argument("operation_mode", type=EnumType(OperationMode, False)),
352352
click.argument("target_temperature", type=int),

miio/click_common.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ def get_metavar(self, param):
9393
return ("_".join(word)).upper()
9494

9595

96+
class HexStringParamType(click.ParamType):
97+
def convert(self, value, param, ctx):
98+
try:
99+
return bytes.fromhex(value)
100+
except ValueError:
101+
self.fail('%s is not a valid hexadecimal string' % value, param, ctx)
102+
103+
96104
class GlobalContextObject:
97105
def __init__(self, debug: int=0, output: callable=None):
98106
self.debug = debug

0 commit comments

Comments
 (0)