Get child emeters with CLI (#623)

* Get child emeters with CLI

* Avoid extra IO when not que querying the child emeter
This commit is contained in:
Nathan Wreggit
2024-01-04 17:25:24 -08:00
committed by GitHub
parent 2d8a8d9511
commit cfe694e5de
3 changed files with 40 additions and 4 deletions

View File

@@ -602,14 +602,27 @@ async def raw_command(dev: SmartDevice, module, command, parameters):
@cli.command()
@pass_dev
@click.option("--index", type=int, required=False)
@click.option("--name", type=str, required=False)
@click.option("--year", type=click.DateTime(["%Y"]), default=None, required=False)
@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):
async def emeter(dev: SmartDevice, index: int, name: str, year, month, erase):
"""Query emeter for historical consumption.
Daily and monthly data provided in CSV format.
"""
if index is not None or name is not None:
if not dev.is_strip:
echo("Index and name are only for power strips!")
return
dev = cast(SmartStrip, dev)
if index is not None:
dev = dev.get_plug_by_index(index)
elif name:
dev = dev.get_plug_by_name(name)
echo("[bold]== Emeter ==[/bold]")
if not dev.has_emeter:
echo("Device has no emeter")
@@ -629,7 +642,10 @@ async def emeter(dev: SmartDevice, year, month, erase):
usage_data = await dev.get_emeter_daily(year=month.year, month=month.month)
else:
# Call with no argument outputs summary data and returns
emeter_status = dev.emeter_realtime
if index is not None or name is not None:
emeter_status = await dev.get_emeter_realtime()
else:
emeter_status = dev.emeter_realtime
echo("Current: %s A" % emeter_status["current"])
echo("Voltage: %s V" % emeter_status["voltage"])