mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-04-26 16:46:23 +00:00
Add toggle command to cli (#498)
This commit is contained in:
parent
24da24efad
commit
4b99351dd6
26
kasa/cli.py
26
kasa/cli.py
@ -613,6 +613,32 @@ async def off(dev: SmartDevice, index: int, name: str, transition: int):
|
|||||||
return await dev.turn_off(transition=transition)
|
return await dev.turn_off(transition=transition)
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command()
|
||||||
|
@click.option("--index", type=int, required=False)
|
||||||
|
@click.option("--name", type=str, required=False)
|
||||||
|
@click.option("--transition", type=int, required=False)
|
||||||
|
@pass_dev
|
||||||
|
async def toggle(dev: SmartDevice, index: int, name: str, transition: int):
|
||||||
|
"""Toggle the device on/off."""
|
||||||
|
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)
|
||||||
|
|
||||||
|
if dev.is_on:
|
||||||
|
echo(f"Turning off {dev.alias}")
|
||||||
|
return await dev.turn_off(transition=transition)
|
||||||
|
|
||||||
|
echo(f"Turning on {dev.alias}")
|
||||||
|
return await dev.turn_on(transition=transition)
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
@click.option("--delay", default=1)
|
@click.option("--delay", default=1)
|
||||||
@pass_dev
|
@pass_dev
|
||||||
|
@ -5,7 +5,7 @@ import pytest
|
|||||||
from asyncclick.testing import CliRunner
|
from asyncclick.testing import CliRunner
|
||||||
|
|
||||||
from kasa import SmartDevice
|
from kasa import SmartDevice
|
||||||
from kasa.cli import alias, brightness, cli, emeter, raw_command, state, sysinfo
|
from kasa.cli import alias, brightness, cli, emeter, raw_command, state, sysinfo, toggle
|
||||||
|
|
||||||
from .conftest import handle_turn_on, turn_on
|
from .conftest import handle_turn_on, turn_on
|
||||||
|
|
||||||
@ -30,6 +30,18 @@ async def test_state(dev, turn_on):
|
|||||||
assert "Device state: False" in res.output
|
assert "Device state: False" in res.output
|
||||||
|
|
||||||
|
|
||||||
|
@turn_on
|
||||||
|
async def test_toggle(dev, turn_on, mocker):
|
||||||
|
await handle_turn_on(dev, turn_on)
|
||||||
|
runner = CliRunner()
|
||||||
|
await runner.invoke(toggle, obj=dev)
|
||||||
|
|
||||||
|
if turn_on:
|
||||||
|
assert not dev.is_on
|
||||||
|
else:
|
||||||
|
assert dev.is_on
|
||||||
|
|
||||||
|
|
||||||
async def test_alias(dev):
|
async def test_alias(dev):
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user