Skip to content

Allow raw-command and wifi without update #688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
4 changes: 3 additions & 1 deletion kasa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ def _nop_echo(*args, **kwargs):

if type is not None:
dev = TYPE_TO_CLASS[type](host)
await dev.update()
elif device_family and encrypt_type:
ctype = ConnectionType(
DeviceFamilyType(device_family),
Expand All @@ -339,6 +338,9 @@ def _nop_echo(*args, **kwargs):
port=port,
credentials=credentials,
)

# Skip update for wifi & raw-command, and if factory was used to connect
if ctx.invoked_subcommand not in ["wifi", "raw-command"] and not device_family:
await dev.update()

ctx.obj = dev
Expand Down
35 changes: 33 additions & 2 deletions kasa/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@
from .conftest import device_iot, device_smart, handle_turn_on, new_discovery, turn_on


async def test_update_called_by_cli(dev, mocker):
"""Test that device update is called on main."""
runner = CliRunner()
update = mocker.patch.object(dev, "update")
mocker.patch("kasa.discover.Discover.discover_single", return_value=dev)

res = await runner.invoke(
cli,
[
"--host",
"127.0.0.1",
"--username",
"foo",
"--password",
"bar",
],
)
assert res.exit_code == 0
update.assert_called()


@device_iot
async def test_sysinfo(dev):
runner = CliRunner()
Expand Down Expand Up @@ -86,8 +107,9 @@ async def test_alias(dev):
await dev.set_alias(old_alias)


async def test_raw_command(dev):
async def test_raw_command(dev, mocker):
runner = CliRunner()
update = mocker.patch.object(dev, "update")
from kasa.tapo import TapoDevice

if isinstance(dev, TapoDevice):
Expand All @@ -96,6 +118,10 @@ async def test_raw_command(dev):
params = ["system", "get_sysinfo"]
res = await runner.invoke(raw_command, params, obj=dev)

# Make sure that update was not called for wifi
with pytest.raises(AssertionError):
update.assert_called()

assert res.exit_code == 0
assert dev.model in res.output

Expand Down Expand Up @@ -129,14 +155,19 @@ async def test_wifi_scan(dev):


@device_smart
async def test_wifi_join(dev):
async def test_wifi_join(dev, mocker):
runner = CliRunner()
update = mocker.patch.object(dev, "update")
res = await runner.invoke(
wifi,
["join", "FOOBAR", "--keytype", "wpa_psk", "--password", "foobar"],
obj=dev,
)

# Make sure that update was not called for wifi
with pytest.raises(AssertionError):
update.assert_called()

assert res.exit_code == 0
assert "Asking the device to connect to FOOBAR" in res.output

Expand Down