From b7fa0d2040f91d41f04a41e589b0f2fa3e898af7 Mon Sep 17 00:00:00 2001 From: "Teemu R." Date: Sat, 21 Sep 2024 16:52:52 +0200 Subject: [PATCH] Add factory-reset command to cli (#1108) Allow reseting devices to factory settings using the cli: `kasa device factory-reset`. --- kasa/cli/device.py | 11 +++++++++++ kasa/tests/test_cli.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/kasa/cli/device.py b/kasa/cli/device.py index 60438035..400bc473 100644 --- a/kasa/cli/device.py +++ b/kasa/cli/device.py @@ -166,6 +166,17 @@ async def reboot(plug, delay): return await plug.reboot(delay) +@device.command() +@pass_dev +async def factory_reset(plug): + """Reset device to factory settings.""" + click.confirm( + "Do you really want to reset the device to factory settings?", abort=True + ) + + return await plug.factory_reset() + + @device.command() @pass_dev @click.option( diff --git a/kasa/tests/test_cli.py b/kasa/tests/test_cli.py index e55f4d01..289dcd23 100644 --- a/kasa/tests/test_cli.py +++ b/kasa/tests/test_cli.py @@ -19,6 +19,7 @@ from kasa import ( ) from kasa.cli.device import ( alias, + factory_reset, led, reboot, state, @@ -215,6 +216,21 @@ async def test_reboot(dev, mocker, runner): assert res.exit_code == 0 +@device_smart +async def test_factory_reset(dev, mocker, runner): + """Test that factory reset works on SMART devices.""" + query_mock = mocker.patch.object(dev.protocol, "query") + + res = await runner.invoke( + factory_reset, + obj=dev, + input="y\n", + ) + + query_mock.assert_called() + assert res.exit_code == 0 + + @device_smart async def test_wifi_scan(dev, runner): res = await runner.invoke(wifi, ["scan"], obj=dev)