Update cli.py to addresss crash on year/month calls and improve output 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.
This commit is contained in:
BuongiornoTexas 2020-10-04 05:32:38 +11:00 committed by GitHub
parent c59b748a06
commit 70061cbe67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

3
.gitignore vendored
View File

@ -19,6 +19,9 @@ __pycache__/
# IntelliJ # IntelliJ
.idea .idea
# vscode
.vscode
# VirtualEnvs # VirtualEnvs
venv venv
.venv .venv

View File

@ -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("--month", type=click.DateTime(["%Y-%m"]), default=None, required=False)
@click.option("--erase", is_flag=True) @click.option("--erase", is_flag=True)
async def emeter(dev: SmartDevice, year, month, erase): 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)) click.echo(click.style("== Emeter ==", bold=True))
await dev.update() await dev.update()
if not dev.has_emeter: if not dev.has_emeter:
@ -323,18 +326,17 @@ async def emeter(dev: SmartDevice, year, month, erase):
if year: if year:
click.echo(f"== For year {year.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: elif month:
click.echo(f"== For month {month.month} of {month.year} ==") 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: else:
# Call with no argument outputs summary data and returns
usage_data = {}
emeter_status = dev.emeter_realtime 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("Current: %s A" % emeter_status["current"])
click.echo("Voltage: %s V" % emeter_status["voltage"]) click.echo("Voltage: %s V" % emeter_status["voltage"])
click.echo("Power: %s W" % emeter_status["power"]) click.echo("Power: %s W" % emeter_status["power"])
@ -343,6 +345,12 @@ async def emeter(dev: SmartDevice, year, month, erase):
click.echo("Today: %s kWh" % dev.emeter_today) click.echo("Today: %s kWh" % dev.emeter_today)
click.echo("This month: %s kWh" % dev.emeter_this_month) 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() @cli.command()
@click.argument("brightness", type=click.IntRange(0, 100), default=None, required=False) @click.argument("brightness", type=click.IntRange(0, 100), default=None, required=False)