Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions kasa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@
Discover,
SmartBulb,
SmartDevice,
SmartDimmer,
SmartLightStrip,
SmartPlug,
SmartStrip,
)

TYPE_TO_CLASS = {
"plug": SmartPlug,
"bulb": SmartBulb,
"dimmer": SmartDimmer,
"strip": SmartStrip,
"lightstrip": SmartLightStrip,
}

click.anyio_backend = "asyncio"


Expand Down Expand Up @@ -44,9 +53,12 @@
@click.option("--plug", default=False, is_flag=True)
@click.option("--lightstrip", default=False, is_flag=True)
@click.option("--strip", default=False, is_flag=True)
@click.option(
"--type", default=None, type=click.Choice(TYPE_TO_CLASS, case_sensitive=False)
)
@click.version_option()
@click.pass_context
async def cli(ctx, host, alias, target, debug, bulb, plug, lightstrip, strip):
async def cli(ctx, host, alias, target, debug, bulb, plug, lightstrip, strip, type):
"""A tool for controlling TP-Link smart home devices.""" # noqa
if debug:
logging.basicConfig(level=logging.DEBUG)
Expand All @@ -69,24 +81,28 @@ async def cli(ctx, host, alias, target, debug, bulb, plug, lightstrip, strip):
click.echo("No host name given, trying discovery..")
await ctx.invoke(discover)
return
else:
if not bulb and not plug and not strip and not lightstrip:
click.echo("No --strip nor --bulb nor --plug given, discovering..")
dev = await Discover.discover_single(host)
elif bulb:
dev = SmartBulb(host)

if bulb or plug or strip or lightstrip:
click.echo(
"Using --bulb, --plug, --strip, and --lightstrip is deprecated. Use --type instead to define the type"
)
if bulb:
type = "bulb"
elif plug:
dev = SmartPlug(host)
type = "plug"
elif strip:
dev = SmartStrip(host)
type = "strip"
elif lightstrip:
dev = SmartLightStrip(host)
else:
click.echo("Unable to detect type, use --strip or --bulb or --plug!")
return
type = "lightstrip"

if type is not None:
dev = TYPE_TO_CLASS[type](host)
else:
click.echo("No --type defined, discovering..")
dev = await Discover.discover_single(host)

await dev.update()
ctx.obj = dev
await dev.update()
ctx.obj = dev

if ctx.invoked_subcommand is None:
await ctx.invoke(state)
Expand Down
27 changes: 26 additions & 1 deletion kasa/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import pytest
from asyncclick.testing import CliRunner

from kasa import SmartDevice
from kasa.cli import alias, brightness, emeter, raw_command, state, sysinfo
from kasa.cli import (
TYPE_TO_CLASS,
alias,
brightness,
cli,
emeter,
raw_command,
state,
sysinfo,
)

from .conftest import handle_turn_on, pytestmark, turn_on

Expand Down Expand Up @@ -96,6 +106,21 @@ async def test_brightness(dev):
assert "Brightness: 12" in res.output


def _generate_type_class_pairs():
yield from TYPE_TO_CLASS.items()


@pytest.mark.parametrize("type_class", _generate_type_class_pairs())
async def test_deprecated_type(dev, type_class):
"""Make sure that using deprecated types yields a warning."""
type, cls = type_class
if type == "dimmer":
return
runner = CliRunner()
res = await runner.invoke(cli, ["--host", "127.0.0.2", f"--{type}"])
assert "Using --bulb, --plug, --strip, and --lightstrip is deprecated" in res.output


async def test_temperature(dev):
pass

Expand Down