Skip to content

Commit

Permalink
Update cli.py to addresss crash on year/month calls and improve outpu…
Browse files Browse the repository at this point in the history
…t formatting (#103)

* Update cli.py

Fix crash when kasa cli is called with --month or --year arguments.

* Update cli.py

* Fix crash on emeter monthly, yearly cli calls

* Formatting fix.
  • Loading branch information
BuongiornoTexas committed Oct 3, 2020
1 parent c59b748 commit 70061cb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ __pycache__/
# IntelliJ
.idea

# vscode
.vscode

# VirtualEnvs
venv
.venv
Expand Down
24 changes: 16 additions & 8 deletions kasa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,10 @@ async def raw_command(dev: SmartDevice, module, command, parameters):
@click.option("--month", type=click.DateTime(["%Y-%m"]), default=None, required=False)
@click.option("--erase", is_flag=True)
async def emeter(dev: SmartDevice, year, month, erase):
"""Query emeter for historical consumption."""
"""Query emeter for historical consumption.
Daily and monthly data provided in CSV format.
"""
click.echo(click.style("== Emeter ==", bold=True))
await dev.update()
if not dev.has_emeter:
Expand All @@ -323,18 +326,17 @@ async def emeter(dev: SmartDevice, year, month, erase):

if year:
click.echo(f"== For year {year.year} ==")
emeter_status = await dev.get_emeter_monthly(year.year)
click.echo("Month, usage (kWh)")
usage_data = await dev.get_emeter_monthly(year.year)
elif month:
click.echo(f"== For month {month.month} of {month.year} ==")
emeter_status = await dev.get_emeter_daily(year=month.year, month=month.month)
click.echo("Day, usage (kWh)")
usage_data = await dev.get_emeter_daily(year=month.year, month=month.month)
else:
# Call with no argument outputs summary data and returns
usage_data = {}
emeter_status = dev.emeter_realtime

if isinstance(emeter_status, list):
for plug in emeter_status:
index = emeter_status.index(plug) + 1
click.echo(f"Plug {index}: {plug}")
else:
click.echo("Current: %s A" % emeter_status["current"])
click.echo("Voltage: %s V" % emeter_status["voltage"])
click.echo("Power: %s W" % emeter_status["power"])
Expand All @@ -343,6 +345,12 @@ async def emeter(dev: SmartDevice, year, month, erase):
click.echo("Today: %s kWh" % dev.emeter_today)
click.echo("This month: %s kWh" % dev.emeter_this_month)

return

# output any detailed usage data
for index, usage in usage_data.items():
click.echo(f"{index}, {usage}")


@cli.command()
@click.argument("brightness", type=click.IntRange(0, 100), default=None, required=False)
Expand Down

0 comments on commit 70061cb

Please sign in to comment.