From 1584fff0e5f3cc9c121dc5e151d7ec400cee4ce2 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Tue, 23 Jan 2024 16:23:01 +0100 Subject: [PATCH 1/4] Allow raw-command and wifi without update --- kasa/cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kasa/cli.py b/kasa/cli.py index d1cb72765..945b9aa94 100755 --- a/kasa/cli.py +++ b/kasa/cli.py @@ -339,7 +339,8 @@ def _nop_echo(*args, **kwargs): port=port, credentials=credentials, ) - await dev.update() + if ctx.invoked_subcommand not in ["wifi", "raw-command"]: + await dev.update() ctx.obj = dev From d37d8237c45bda2e34dcafd483d5b76f7b1217d2 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Tue, 23 Jan 2024 16:56:21 +0100 Subject: [PATCH 2/4] Call update always but on wifi&raw-command --- kasa/cli.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kasa/cli.py b/kasa/cli.py index 945b9aa94..a23185c28 100755 --- a/kasa/cli.py +++ b/kasa/cli.py @@ -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), @@ -339,8 +338,9 @@ def _nop_echo(*args, **kwargs): port=port, credentials=credentials, ) - if ctx.invoked_subcommand not in ["wifi", "raw-command"]: - await dev.update() + + if ctx.invoked_subcommand not in ["wifi", "raw-command"]: + await dev.update() ctx.obj = dev From da79ea35304ccf8619836b029619c7a85a672cd2 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Tue, 23 Jan 2024 16:56:30 +0100 Subject: [PATCH 3/4] Add tests --- kasa/tests/test_cli.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/kasa/tests/test_cli.py b/kasa/tests/test_cli.py index 3aad37dda..fa2d5c69e 100644 --- a/kasa/tests/test_cli.py +++ b/kasa/tests/test_cli.py @@ -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() @@ -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): @@ -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 @@ -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 From 473370c7099ff80fcd0ba534dc7e9f678e928fd0 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Tue, 23 Jan 2024 17:06:57 +0100 Subject: [PATCH 4/4] Skip update also if device_family was defined, as device factory performs an update --- kasa/cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kasa/cli.py b/kasa/cli.py index a23185c28..1b20303d4 100755 --- a/kasa/cli.py +++ b/kasa/cli.py @@ -339,7 +339,8 @@ def _nop_echo(*args, **kwargs): credentials=credentials, ) - if ctx.invoked_subcommand not in ["wifi", "raw-command"]: + # 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