8000 Make cli interface more consistent (#232) · allandaly/python-kasa@e06b9a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit e06b9a7

Browse files
authored
Make cli interface more consistent (python-kasa#232)
1 parent c65705b commit e06b9a7

File tree

3 files changed

+32
-46
lines changed

3 files changed

+32
-46
lines changed

.gitchangelog.rc

Lines changed: 0 additions & 1 deletion
This file was deleted.

.hound.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

kasa/cli.py

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ async def cli(ctx, host, alias, target, debug, bulb, plug, lightstrip, strip):
8484
else:
8585
click.echo("Unable to detect type, use --strip or --bulb or --plug!")
8686
return
87+
88+
await dev.update()
8789
ctx.obj = dev
8890

8991
if ctx.invoked_subcommand is None:
@@ -106,6 +108,8 @@ async def scan(dev):
106108
for dev in devs:
107109
click.echo(f"\t {dev}")
108110

111+
return devs
112+
109113

110114
@wifi.command()
111115
@click.argument("ssid")
@@ -120,18 +124,7 @@ async def join(dev: SmartDevice, ssid, password, keytype):
120124
f"Response: {res} - if the device is not able to join the network, it will revert back to its previous state."
121125
)
122126

123-
124-
@cli.command()
125-
@click.option("--scrub/--no-scrub", default=True)
126-
@click.pass_context
127-
async def dump_discover(ctx, scrub):
128-
"""Dump discovery information.
129-
130-
Useful for dumping into a file to be added to the test suite.
131-
"""
132-
click.echo(
133-
"This is deprecated, use the script inside devtools to generate a devinfo file."
134-
)
127+
return res
135128

136129

137130
@cli.command()
@@ -158,35 +151,30 @@ async def discover(ctx, timeout, discover_only, dump_raw):
158151

159152
async def find_host_from_alias(alias, target="255.255.255.255", timeout=1, attempts=3):
160153
"""Discover a device identified by its alias."""
161-
click.echo(
162-
f"Trying to discover {alias} using {attempts} attempts of {timeout} seconds"
163-
)
164154
for attempt in range(1, attempts):
165-
click.echo(f"Attempt {attempt} of {attempts}")
166155
found_devs = await Discover.discover(target=target, timeout=timeout)
167-
found_devs = found_devs.items()
168-
for ip, dev in found_devs:
156+
for ip, dev in found_devs.items():
169157
if dev.alias.lower() == alias.lower():
170158
host = dev.host
171159
return host
160+
172161
return None
173162

174163

175164
@cli.command()
176165
@pass_dev
177166
async def sysinfo(dev):
178167
"""Print out full system information."""
179-
await dev.update()
180168
click.echo(click.style("== System info ==", bold=True))
181169
click.echo(pf(dev.sys_info))
170+
return dev.sys_info
182171

183172

184173
@cli.command()
185174
@pass_dev
186175
@click.pass_context
187176
async def state(ctx, dev: SmartDevice):
188177
"""Print out device state and versions."""
189-
await dev.update()
190178
click.echo(click.style(f"== {dev.alias} - {dev.model} ==", bold=True))
191179
click.echo(f"\tHost: {dev.host}")
192180
click.echo(
@@ -234,7 +222,6 @@ async def state(ctx, dev: SmartDevice):
234222
@click.option("--index", type=int)
235223
async def alias(dev, new_alias, index):
236224
"""Get or set the device (or plug) alias."""
237-
await dev.update()
238225
if index is not None:
239226
if not dev.is_strip:
240227
click.echo("Index can only used for power strips!")
@@ -244,8 +231,8 @@ async def alias(dev, new_alias, index):
244231

245232
if new_alias is not None:
246233
click.echo(f"Setting alias to {new_alias}")
247-
click.echo(await dev.set_alias(new_alias))
248-
await dev.update()
234+
res = await dev.set_alias(new_alias)
235+
return res
249236

250237
click.echo(f"Alias: {dev.alias}")
251238
if dev.is_strip:
@@ -264,9 +251,11 @@ async def raw_command(dev: SmartDevice, module, command, parameters):
264251

265252
if parameters is not None:
266253
parameters = ast.literal_eval(parameters)
254+
267255
res = await dev._query_helper(module, command, parameters)
268-
await dev.update() # TODO: is this needed?
256+
269257
click.echo(res)
258+
return res
270259

271260

272261
@cli.command()
@@ -280,7 +269,6 @@ async def emeter(dev: SmartDevice, year, month, erase):
280269
Daily and monthly data provided in CSV format.
281270
"""
282271
click.echo(click.style("== Emeter ==", bold=True))
283-
await dev.update()
284272
if not dev.has_emeter:
285273
click.echo("Device has no emeter")
286274
return
@@ -324,15 +312,15 @@ async def emeter(dev: SmartDevice, year, month, erase):
324312
@pass_dev
325313
async def brightness(dev: SmartBulb, brightness: int, transition: int):
326314
"""Get or set brightness."""
327-
await dev.update()
328315
if not dev.is_dimmable:
329316
click.echo("This device does not support brightness.")
330317
return
318+
331319
if brightness is None:
332320
click.echo(f"Brightness: {dev.brightness}")
333321
else:
334322
click.echo(f"Setting brightness to {brightness}")
335-
click.echo(await dev.set_brightness(brightness, transition=transition))
323+
return await dev.set_brightness(brightness, transition=transition)
336324

337325

338326
@cli.command()
@@ -343,10 +331,10 @@ async def brightness(dev: SmartBulb, brightness: int, transition: int):
343331
@pass_dev
344332
async def temperature(dev: SmartBulb, temperature: int, transition: int):
345333
"""Get or set color temperature."""
346-
await dev.update()
347334
if not dev.is_variable_color_temp:
348335
click.echo("Device does not support color temperature")
349336
return
337+
350338
if temperature is None:
351339
click.echo(f"Color temperature: {dev.color_temp}")
352340
valid_temperature_range = dev.valid_temperature_range
@@ -359,7 +347,7 @@ async def temperature(dev: SmartBulb, temperature: int, transition: int):
359347
)
360348
else:
361349
click.echo(f"Setting color temperature to {temperature}")
362-
await dev.set_color_temp(temperature, transition=transition)
350+
return await dev.set_color_temp(temperature, transition=transition)
363351

364352

365353
@cli.command()
@@ -370,26 +358,28 @@ async def temperature(dev: SmartBulb, temperature: int, transition: int):
370358
@click.pass_context
371359
@pass_dev
372360
async def hsv(dev, ctx, h, s, v, transition):
373-
"""Get or set color in HSV. (Bulb only)."""
374-
await dev.update()
361+
"""Get or set color in HSV."""
362+
if not dev.is_color:
363+
click.echo("Device does not support colors")
364+
return
365+
375366
if h is None or s is None or v is None:
376367
click.echo(f"Current HSV: {dev.hsv}")
377368
elif s is None or v is None:
378369
raise click.BadArgumentUsage("Setting a color requires 3 values.", ctx)
379370
else:
380371
click.echo(f"Setting HSV: {h} {s} {v}")
381-
click.echo(await dev.set_hsv(h, s, v, transition=transition))
372+
return await dev.set_hsv(h, s, v, transition=transition)
382373

383374

384375
@cli.command()
385376
@click.argument("state", type=bool, required=False)
386377
@pass_dev
387378
async def led(dev, state):
388379
"""Get or set (Plug's) led state."""
389-
await dev.update()
390380
if state is not None:
391381
click.echo(f"Turning led to {state}")
392-
click.echo(await dev.set_led(state))
382+
return await dev.set_led(state)
393383
else:
394384
click.echo(f"LED state: {dev.led}")
395385

@@ -398,7 +388,9 @@ async def led(dev, state):
398388
@pass_dev
399389
async def time(dev):
400390
"""Get the device time."""
401-
click.echo(await dev.get_time())
391+
res = await dev.get_time()
392+
click.echo(f"Current time: {res}")
393+
return res
402394

403395

404396
@cli.command()
@@ -408,19 +400,19 @@ async def time(dev):
408400
@pass_dev
409401
async def on(dev: SmartDevice, index: int, name: str, transition: int):
410402
"""Turn the device on."""
411-
await dev.update()
412403
if index is not None or name is not None:
413404
if not dev.is_strip:
414405
click.echo("Index and name are only for power strips!")
415406
return
407+
416408
dev = cast(SmartStrip, dev)
417409
if index is not None:
418410
dev = dev.get_plug_by_index(index)
419411
elif name:
420412
dev = dev.get_plug_by_name(name)
421413

422414
click.echo(f"Turning on {dev.alias}")
423-
await dev.turn_on(transition=transition)
415+
return await dev.turn_on(transition=transition)
424416

425417

426418
@cli.command()
@@ -430,19 +422,19 @@ async def on(dev: SmartDevice, index: int, name: str, transition: int):
430422
@pass_dev
431423
async def off(dev: SmartDevice, index: int, name: str, transition: int):
432424
"""Turn the device off."""
433-
await dev.update()
434425
if index is not None or name is not None:
435426
if not dev.is_strip:
436427
click.echo("Index and name are only for power strips!")
437428
return
429+
438430
dev = cast(SmartStrip, dev)
439431
if index is not None:
440432
dev = dev.get_plug_by_index(index)
441433
elif name:
442434
dev = dev.get_plug_by_name(name)
443435

444436
click.echo(f"Turning off {dev.alias}")
445-
await dev.turn_off(transition=transition)
437+
return await dev.turn_off(transition=transition)
446438

447439

448440
@cli.command()
@@ -451,7 +443,7 @@ async def off(dev: SmartDevice, index: int, name: str, transition: int):
451443 179B
async def reboot(plug, delay):
452444
"""Reboot the device."""
453445
click.echo("Rebooting the device..")
454-
click.echo(await plug.reboot(delay))
446+
return await plug.reboot(delay)
455447

456448

457449
if __name__ == "__main__":

0 commit comments

Comments
 (0)
0