2023-02-18 20:41:08 +00:00
|
|
|
import json
|
2024-04-20 15:14:53 +00:00
|
|
|
import os
|
2024-01-10 19:37:43 +00:00
|
|
|
import re
|
2022-10-03 18:28:05 +00:00
|
|
|
|
2023-09-13 13:46:38 +00:00
|
|
|
import asyncclick as click
|
2021-12-13 19:17:54 +00:00
|
|
|
import pytest
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
from asyncclick.testing import CliRunner
|
2024-07-02 13:11:19 +00:00
|
|
|
from pytest_mock import MockerFixture
|
2020-05-12 10:11:47 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
from kasa import (
|
2024-02-21 15:52:55 +00:00
|
|
|
AuthenticationError,
|
2024-02-08 19:03:06 +00:00
|
|
|
Credentials,
|
2024-02-04 15:20:08 +00:00
|
|
|
Device,
|
2024-02-21 15:52:55 +00:00
|
|
|
DeviceError,
|
2024-01-05 01:25:24 +00:00
|
|
|
EmeterStatus,
|
2024-02-21 15:52:55 +00:00
|
|
|
KasaException,
|
2024-05-14 07:38:21 +00:00
|
|
|
Module,
|
2024-02-21 15:52:55 +00:00
|
|
|
UnsupportedDeviceError,
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
2024-07-04 12:52:01 +00:00
|
|
|
from kasa.cli.main import (
|
2023-12-29 19:17:15 +00:00
|
|
|
TYPE_TO_CLASS,
|
|
|
|
alias,
|
|
|
|
brightness,
|
|
|
|
cli,
|
2024-02-22 19:46:19 +00:00
|
|
|
cmd_command,
|
2024-05-14 07:38:21 +00:00
|
|
|
effect,
|
2023-12-29 19:17:15 +00:00
|
|
|
emeter,
|
2024-07-02 13:11:19 +00:00
|
|
|
energy,
|
2024-05-14 07:38:21 +00:00
|
|
|
hsv,
|
|
|
|
led,
|
2023-12-29 19:17:15 +00:00
|
|
|
raw_command,
|
2024-01-23 13:26:47 +00:00
|
|
|
reboot,
|
2023-12-29 19:17:15 +00:00
|
|
|
state,
|
|
|
|
sysinfo,
|
2024-05-14 07:38:21 +00:00
|
|
|
temperature,
|
2024-06-17 08:37:08 +00:00
|
|
|
time,
|
2023-12-29 19:17:15 +00:00
|
|
|
toggle,
|
2024-01-10 19:37:43 +00:00
|
|
|
update_credentials,
|
|
|
|
wifi,
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
|
|
|
from kasa.discover import Discover, DiscoveryResult
|
2024-02-04 15:20:08 +00:00
|
|
|
from kasa.iot import IotDevice
|
2020-04-20 17:26:20 +00:00
|
|
|
|
2024-02-23 22:32:17 +00:00
|
|
|
from .conftest import (
|
|
|
|
device_smart,
|
2024-02-27 17:39:04 +00:00
|
|
|
get_device_for_fixture_protocol,
|
2024-02-23 22:32:17 +00:00
|
|
|
handle_turn_on,
|
|
|
|
new_discovery,
|
|
|
|
turn_on,
|
|
|
|
)
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
|
2020-04-20 17:26:20 +00:00
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def runner():
|
|
|
|
"""Runner fixture that unsets the KASA_ environment variables for tests."""
|
|
|
|
KASA_VARS = {k: None for k, v in os.environ.items() if k.startswith("KASA_")}
|
|
|
|
runner = CliRunner(env=KASA_VARS)
|
|
|
|
|
|
|
|
return runner
|
|
|
|
|
|
|
|
|
2024-06-19 10:01:35 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("device_family", "encrypt_type"),
|
|
|
|
[
|
|
|
|
pytest.param(None, None, id="No connect params"),
|
|
|
|
pytest.param("SMART.TAPOPLUG", None, id="Only device_family"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_update_called_by_cli(dev, mocker, runner, device_family, encrypt_type):
|
2024-01-23 21:58:57 +00:00
|
|
|
"""Test that device update is called on main."""
|
|
|
|
update = mocker.patch.object(dev, "update")
|
2024-02-15 15:25:08 +00:00
|
|
|
|
|
|
|
# These will mock the features to avoid accessing non-existing
|
|
|
|
mocker.patch("kasa.device.Device.features", return_value={})
|
|
|
|
mocker.patch("kasa.iot.iotdevice.IotDevice.features", return_value={})
|
|
|
|
|
2024-01-23 21:58:57 +00:00
|
|
|
mocker.patch("kasa.discover.Discover.discover_single", return_value=dev)
|
|
|
|
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--host",
|
|
|
|
"127.0.0.1",
|
|
|
|
"--username",
|
|
|
|
"foo",
|
|
|
|
"--password",
|
|
|
|
"bar",
|
2024-06-19 10:01:35 +00:00
|
|
|
"--device-family",
|
|
|
|
device_family,
|
|
|
|
"--encrypt-type",
|
|
|
|
encrypt_type,
|
2024-01-23 21:58:57 +00:00
|
|
|
],
|
2024-02-15 15:25:08 +00:00
|
|
|
catch_exceptions=False,
|
2024-01-23 21:58:57 +00:00
|
|
|
)
|
|
|
|
assert res.exit_code == 0
|
|
|
|
update.assert_called()
|
|
|
|
|
|
|
|
|
2024-05-14 07:38:21 +00:00
|
|
|
async def test_sysinfo(dev: Device, runner):
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(sysinfo, obj=dev)
|
2020-04-20 17:26:20 +00:00
|
|
|
assert "System info" in res.output
|
2024-05-14 07:38:21 +00:00
|
|
|
assert dev.model in res.output
|
2020-04-20 17:26:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
@turn_on
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_state(dev, turn_on, runner):
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
await handle_turn_on(dev, turn_on)
|
2021-09-19 21:45:48 +00:00
|
|
|
await dev.update()
|
2024-04-24 11:25:16 +00:00
|
|
|
res = await runner.invoke(state, obj=dev)
|
2020-04-20 17:26:20 +00:00
|
|
|
|
|
|
|
if dev.is_on:
|
2023-02-18 16:31:06 +00:00
|
|
|
assert "Device state: True" in res.output
|
2020-04-20 17:26:20 +00:00
|
|
|
else:
|
2023-02-18 16:31:06 +00:00
|
|
|
assert "Device state: False" in res.output
|
2020-04-20 17:26:20 +00:00
|
|
|
|
|
|
|
|
2023-08-26 12:21:38 +00:00
|
|
|
@turn_on
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_toggle(dev, turn_on, runner):
|
2023-08-26 12:21:38 +00:00
|
|
|
await handle_turn_on(dev, turn_on)
|
2024-04-24 11:25:16 +00:00
|
|
|
await dev.update()
|
|
|
|
assert dev.is_on == turn_on
|
2023-08-26 12:21:38 +00:00
|
|
|
|
2024-04-24 11:25:16 +00:00
|
|
|
await runner.invoke(toggle, obj=dev)
|
|
|
|
await dev.update()
|
|
|
|
assert dev.is_on != turn_on
|
2023-08-26 12:21:38 +00:00
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_alias(dev, runner):
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(alias, obj=dev)
|
2020-04-20 17:26:20 +00:00
|
|
|
assert f"Alias: {dev.alias}" in res.output
|
|
|
|
|
2021-09-19 21:45:48 +00:00
|
|
|
old_alias = dev.alias
|
|
|
|
|
2020-04-20 17:26:20 +00:00
|
|
|
new_alias = "new alias"
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(alias, [new_alias], obj=dev)
|
2020-04-20 17:26:20 +00:00
|
|
|
assert f"Setting alias to {new_alias}" in res.output
|
2024-04-24 11:25:16 +00:00
|
|
|
await dev.update()
|
2020-04-20 17:26:20 +00:00
|
|
|
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(alias, obj=dev)
|
2020-04-20 17:26:20 +00:00
|
|
|
assert f"Alias: {new_alias}" in res.output
|
|
|
|
|
2021-09-19 21:45:48 +00:00
|
|
|
await dev.set_alias(old_alias)
|
|
|
|
|
2020-04-20 17:26:20 +00:00
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_raw_command(dev, mocker, runner):
|
2024-01-23 21:58:57 +00:00
|
|
|
update = mocker.patch.object(dev, "update")
|
2024-02-04 15:20:08 +00:00
|
|
|
from kasa.smart import SmartDevice
|
2024-01-10 19:37:43 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
if isinstance(dev, SmartDevice):
|
2024-01-10 19:37:43 +00:00
|
|
|
params = ["na", "get_device_info"]
|
|
|
|
else:
|
|
|
|
params = ["system", "get_sysinfo"]
|
|
|
|
res = await runner.invoke(raw_command, params, obj=dev)
|
2020-04-20 17:26:20 +00:00
|
|
|
|
2024-01-23 21:58:57 +00:00
|
|
|
# Make sure that update was not called for wifi
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
update.assert_called()
|
|
|
|
|
2020-04-20 17:26:20 +00:00
|
|
|
assert res.exit_code == 0
|
2024-01-10 19:37:43 +00:00
|
|
|
assert dev.model in res.output
|
2020-04-20 17:26:20 +00:00
|
|
|
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(raw_command, obj=dev)
|
2020-04-20 17:26:20 +00:00
|
|
|
assert res.exit_code != 0
|
|
|
|
assert "Usage" in res.output
|
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_command_with_child(dev, mocker, runner):
|
2024-02-22 19:46:19 +00:00
|
|
|
"""Test 'command' command with --child."""
|
|
|
|
update_mock = mocker.patch.object(dev, "update")
|
|
|
|
|
2024-03-11 10:17:12 +00:00
|
|
|
# create_autospec for device slows tests way too much, so we use a dummy here
|
|
|
|
class DummyDevice(dev.__class__):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__("127.0.0.1")
|
2024-07-02 13:11:19 +00:00
|
|
|
# device_type and _info initialised for repr
|
|
|
|
self._device_type = Device.Type.StripSocket
|
|
|
|
self._info = {}
|
2024-03-11 10:17:12 +00:00
|
|
|
|
|
|
|
async def _query_helper(*_, **__):
|
|
|
|
return {"dummy": "response"}
|
|
|
|
|
|
|
|
dummy_child = DummyDevice()
|
2024-02-22 19:46:19 +00:00
|
|
|
|
2024-07-02 13:11:19 +00:00
|
|
|
mocker.patch.object(dev, "_children", {"XYZ": [dummy_child]})
|
2024-02-22 19:46:19 +00:00
|
|
|
mocker.patch.object(dev, "get_child_device", return_value=dummy_child)
|
|
|
|
|
|
|
|
res = await runner.invoke(
|
|
|
|
cmd_command,
|
|
|
|
["--child", "XYZ", "command", "'params'"],
|
|
|
|
obj=dev,
|
|
|
|
catch_exceptions=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
update_mock.assert_called()
|
|
|
|
assert '{"dummy": "response"}' in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
|
2024-01-23 13:26:47 +00:00
|
|
|
@device_smart
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_reboot(dev, mocker, runner):
|
2024-01-23 13:26:47 +00:00
|
|
|
"""Test that reboot works on SMART devices."""
|
|
|
|
query_mock = mocker.patch.object(dev.protocol, "query")
|
|
|
|
|
|
|
|
res = await runner.invoke(
|
|
|
|
reboot,
|
|
|
|
obj=dev,
|
|
|
|
)
|
|
|
|
|
|
|
|
query_mock.assert_called()
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
|
2024-01-10 19:37:43 +00:00
|
|
|
@device_smart
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_wifi_scan(dev, runner):
|
2024-01-10 19:37:43 +00:00
|
|
|
res = await runner.invoke(wifi, ["scan"], obj=dev)
|
|
|
|
|
|
|
|
assert res.exit_code == 0
|
2024-05-01 15:05:37 +00:00
|
|
|
assert re.search(r"Found [\d]+ wifi networks!", res.output)
|
2024-01-10 19:37:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@device_smart
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_wifi_join(dev, mocker, runner):
|
2024-01-23 21:58:57 +00:00
|
|
|
update = mocker.patch.object(dev, "update")
|
2024-01-10 19:37:43 +00:00
|
|
|
res = await runner.invoke(
|
|
|
|
wifi,
|
|
|
|
["join", "FOOBAR", "--keytype", "wpa_psk", "--password", "foobar"],
|
|
|
|
obj=dev,
|
|
|
|
)
|
|
|
|
|
2024-01-23 21:58:57 +00:00
|
|
|
# Make sure that update was not called for wifi
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
update.assert_called()
|
|
|
|
|
2024-01-10 19:37:43 +00:00
|
|
|
assert res.exit_code == 0
|
|
|
|
assert "Asking the device to connect to FOOBAR" in res.output
|
|
|
|
|
|
|
|
|
|
|
|
@device_smart
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_wifi_join_no_creds(dev, runner):
|
2024-01-10 19:37:43 +00:00
|
|
|
dev.protocol._transport._credentials = None
|
|
|
|
res = await runner.invoke(
|
|
|
|
wifi,
|
|
|
|
["join", "FOOBAR", "--keytype", "wpa_psk", "--password", "foobar"],
|
|
|
|
obj=dev,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res.exit_code != 0
|
2024-02-21 15:52:55 +00:00
|
|
|
assert isinstance(res.exception, AuthenticationError)
|
2024-01-10 19:37:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@device_smart
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_wifi_join_exception(dev, mocker, runner):
|
2024-02-21 15:52:55 +00:00
|
|
|
mocker.patch.object(dev.protocol, "query", side_effect=DeviceError(error_code=9999))
|
2024-01-10 19:37:43 +00:00
|
|
|
res = await runner.invoke(
|
|
|
|
wifi,
|
|
|
|
["join", "FOOBAR", "--keytype", "wpa_psk", "--password", "foobar"],
|
|
|
|
obj=dev,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res.exit_code != 0
|
2024-02-21 15:52:55 +00:00
|
|
|
assert isinstance(res.exception, KasaException)
|
2024-01-10 19:37:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@device_smart
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_update_credentials(dev, runner):
|
2024-01-10 19:37:43 +00:00
|
|
|
res = await runner.invoke(
|
|
|
|
update_credentials,
|
|
|
|
["--username", "foo", "--password", "bar"],
|
|
|
|
input="y\n",
|
|
|
|
obj=dev,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res.exit_code == 0
|
|
|
|
assert (
|
|
|
|
"Do you really want to replace the existing credentials? [y/N]: y\n"
|
|
|
|
in res.output
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-06-17 08:37:08 +00:00
|
|
|
async def test_time_get(dev, runner):
|
|
|
|
"""Test time get command."""
|
|
|
|
res = await runner.invoke(
|
|
|
|
time,
|
|
|
|
obj=dev,
|
|
|
|
)
|
|
|
|
assert res.exit_code == 0
|
|
|
|
assert "Current time: " in res.output
|
|
|
|
|
|
|
|
|
|
|
|
@device_smart
|
|
|
|
async def test_time_sync(dev, mocker, runner):
|
|
|
|
"""Test time sync command.
|
|
|
|
|
|
|
|
Currently implemented only for SMART.
|
|
|
|
"""
|
|
|
|
update = mocker.patch.object(dev, "update")
|
|
|
|
set_time_mock = mocker.spy(dev.modules[Module.Time], "set_time")
|
|
|
|
res = await runner.invoke(
|
|
|
|
time,
|
|
|
|
["sync"],
|
|
|
|
obj=dev,
|
|
|
|
)
|
|
|
|
set_time_mock.assert_called()
|
|
|
|
update.assert_called()
|
|
|
|
|
|
|
|
assert res.exit_code == 0
|
|
|
|
assert "Old time: " in res.output
|
|
|
|
assert "New time: " in res.output
|
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_emeter(dev: Device, mocker, runner):
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(emeter, obj=dev)
|
2020-04-20 17:26:20 +00:00
|
|
|
if not dev.has_emeter:
|
|
|
|
assert "Device has no emeter" in res.output
|
|
|
|
return
|
|
|
|
|
2020-05-24 15:57:54 +00:00
|
|
|
assert "== Emeter ==" in res.output
|
2020-04-20 17:26:20 +00:00
|
|
|
|
2024-01-05 01:25:24 +00:00
|
|
|
if not dev.is_strip:
|
|
|
|
res = await runner.invoke(emeter, ["--index", "0"], obj=dev)
|
2024-07-02 13:11:19 +00:00
|
|
|
assert f"Device: {dev.host} does not have children" in res.output
|
2024-01-05 01:25:24 +00:00
|
|
|
res = await runner.invoke(emeter, ["--name", "mock"], obj=dev)
|
2024-07-02 13:11:19 +00:00
|
|
|
assert f"Device: {dev.host} does not have children" in res.output
|
2024-01-05 01:25:24 +00:00
|
|
|
|
|
|
|
if dev.is_strip and len(dev.children) > 0:
|
|
|
|
realtime_emeter = mocker.patch.object(dev.children[0], "get_emeter_realtime")
|
|
|
|
realtime_emeter.return_value = EmeterStatus({"voltage_mv": 122066})
|
|
|
|
|
|
|
|
res = await runner.invoke(emeter, ["--index", "0"], obj=dev)
|
|
|
|
assert "Voltage: 122.066 V" in res.output
|
|
|
|
realtime_emeter.assert_called()
|
|
|
|
assert realtime_emeter.call_count == 1
|
|
|
|
|
|
|
|
res = await runner.invoke(emeter, ["--name", dev.children[0].alias], obj=dev)
|
|
|
|
assert "Voltage: 122.066 V" in res.output
|
|
|
|
assert realtime_emeter.call_count == 2
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
if isinstance(dev, IotDevice):
|
|
|
|
monthly = mocker.patch.object(dev, "get_emeter_monthly")
|
|
|
|
monthly.return_value = {1: 1234}
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(emeter, ["--year", "1900"], obj=dev)
|
2024-02-04 15:20:08 +00:00
|
|
|
if not isinstance(dev, IotDevice):
|
|
|
|
assert "Device has no historical statistics" in res.output
|
|
|
|
return
|
2020-04-20 17:26:20 +00:00
|
|
|
assert "For year" in res.output
|
2022-10-03 18:28:05 +00:00
|
|
|
assert "1, 1234" in res.output
|
|
|
|
monthly.assert_called_with(year=1900)
|
2020-04-20 17:26:20 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
if isinstance(dev, IotDevice):
|
|
|
|
daily = mocker.patch.object(dev, "get_emeter_daily")
|
|
|
|
daily.return_value = {1: 1234}
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(emeter, ["--month", "1900-12"], obj=dev)
|
2024-02-04 15:20:08 +00:00
|
|
|
if not isinstance(dev, IotDevice):
|
|
|
|
assert "Device has no historical statistics" in res.output
|
|
|
|
return
|
2020-04-20 17:26:20 +00:00
|
|
|
assert "For month" in res.output
|
2022-10-03 18:28:05 +00:00
|
|
|
assert "1, 1234" in res.output
|
|
|
|
daily.assert_called_with(year=1900, month=12)
|
2020-04-20 17:26:20 +00:00
|
|
|
|
|
|
|
|
2024-05-14 07:38:21 +00:00
|
|
|
async def test_brightness(dev: Device, runner):
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(brightness, obj=dev)
|
2024-05-14 07:38:21 +00:00
|
|
|
if not (light := dev.modules.get(Module.Light)) or not light.is_dimmable:
|
2020-04-20 17:26:20 +00:00
|
|
|
assert "This device does not support brightness." in res.output
|
|
|
|
return
|
|
|
|
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(brightness, obj=dev)
|
2024-05-14 07:38:21 +00:00
|
|
|
assert f"Brightness: {light.brightness}" in res.output
|
2020-04-20 17:26:20 +00:00
|
|
|
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(brightness, ["12"], obj=dev)
|
2020-04-20 17:26:20 +00:00
|
|
|
assert "Setting brightness" in res.output
|
2024-04-24 11:25:16 +00:00
|
|
|
await dev.update()
|
2020-04-20 17:26:20 +00:00
|
|
|
|
async++, small powerstrip improvements (#46)
* async++, small powerstrip improvements
* use asyncclick instead of click, allows defining the commands with async def to avoid manual eventloop/asyncio.run handling
* improve powerstrip support:
* new powerstrip api: turn_{on,off}_by_{name,index} methods
* cli: fix on/off for powerstrip using the new apis
* add missing update()s for cli's hsv, led, temperature (fixes #43)
* prettyprint the received payloads when debug mode in use
* cli: debug mode can be activated now with '-d'
* update requirements_test.txt
* remove outdated click-datetime, replace click with asyncclick
* debug is a flag
* make smartstripplug to inherit the sysinfo from its parent, allows for simple access of general plug properties
* proper bound checking for index accesses, allow controlling the plug at index 0
* remove the mess of turn_{on,off}_by_{name,index}, get_plug_by_{name,index} are enough.
* adapt cli to use that
* allow changing the alias per index
* use f-strings consistently everywhere in the cli
* add tests for get_plug_by_{index,name}
2020-04-21 18:46:13 +00:00
|
|
|
res = await runner.invoke(brightness, obj=dev)
|
2020-05-13 12:50:45 +00:00
|
|
|
assert "Brightness: 12" in res.output
|
2020-04-20 17:26:20 +00:00
|
|
|
|
|
|
|
|
2024-05-14 07:38:21 +00:00
|
|
|
async def test_color_temperature(dev: Device, runner):
|
|
|
|
res = await runner.invoke(temperature, obj=dev)
|
|
|
|
if not (light := dev.modules.get(Module.Light)) or not light.is_variable_color_temp:
|
|
|
|
assert "Device does not support color temperature" in res.output
|
|
|
|
return
|
|
|
|
|
|
|
|
res = await runner.invoke(temperature, obj=dev)
|
|
|
|
assert f"Color temperature: {light.color_temp}" in res.output
|
|
|
|
valid_range = light.valid_temperature_range
|
|
|
|
assert f"(min: {valid_range.min}, max: {valid_range.max})" in res.output
|
|
|
|
|
|
|
|
val = int((valid_range.min + valid_range.max) / 2)
|
|
|
|
res = await runner.invoke(temperature, [str(val)], obj=dev)
|
|
|
|
assert "Setting color temperature to " in res.output
|
|
|
|
await dev.update()
|
|
|
|
|
|
|
|
res = await runner.invoke(temperature, obj=dev)
|
|
|
|
assert f"Color temperature: {val}" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
invalid_max = valid_range.max + 100
|
|
|
|
# Lights that support the maximum range will not get past the click cli range check
|
|
|
|
# So can't be tested for the internal range check.
|
|
|
|
if invalid_max < 9000:
|
|
|
|
res = await runner.invoke(temperature, [str(invalid_max)], obj=dev)
|
|
|
|
assert res.exit_code == 1
|
|
|
|
assert isinstance(res.exception, ValueError)
|
|
|
|
|
|
|
|
res = await runner.invoke(temperature, [str(9100)], obj=dev)
|
|
|
|
assert res.exit_code == 2
|
|
|
|
|
|
|
|
|
|
|
|
async def test_color_hsv(dev: Device, runner: CliRunner):
|
|
|
|
res = await runner.invoke(hsv, obj=dev)
|
|
|
|
if not (light := dev.modules.get(Module.Light)) or not light.is_color:
|
|
|
|
assert "Device does not support colors" in res.output
|
|
|
|
return
|
|
|
|
|
|
|
|
res = await runner.invoke(hsv, obj=dev)
|
|
|
|
assert f"Current HSV: {light.hsv}" in res.output
|
|
|
|
|
|
|
|
res = await runner.invoke(hsv, ["180", "50", "50"], obj=dev)
|
|
|
|
assert "Setting HSV: 180 50 50" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
await dev.update()
|
|
|
|
|
|
|
|
res = await runner.invoke(hsv, ["180", "50"], obj=dev)
|
|
|
|
assert "Setting a color requires 3 values." in res.output
|
|
|
|
assert res.exit_code == 2
|
|
|
|
|
|
|
|
|
|
|
|
async def test_light_effect(dev: Device, runner: CliRunner):
|
|
|
|
res = await runner.invoke(effect, obj=dev)
|
|
|
|
if not (light_effect := dev.modules.get(Module.LightEffect)):
|
|
|
|
assert "Device does not support effects" in res.output
|
|
|
|
return
|
|
|
|
|
|
|
|
# Start off with a known state of off
|
|
|
|
await light_effect.set_effect(light_effect.LIGHT_EFFECTS_OFF)
|
|
|
|
await dev.update()
|
|
|
|
assert light_effect.effect == light_effect.LIGHT_EFFECTS_OFF
|
|
|
|
|
|
|
|
res = await runner.invoke(effect, obj=dev)
|
2024-05-15 05:16:57 +00:00
|
|
|
assert f"Light effect: {light_effect.effect}" in res.output
|
|
|
|
assert res.exit_code == 0
|
2024-05-14 07:38:21 +00:00
|
|
|
|
|
|
|
res = await runner.invoke(effect, [light_effect.effect_list[1]], obj=dev)
|
|
|
|
assert f"Setting Effect: {light_effect.effect_list[1]}" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
await dev.update()
|
|
|
|
assert light_effect.effect == light_effect.effect_list[1]
|
|
|
|
|
|
|
|
res = await runner.invoke(effect, ["foobar"], obj=dev)
|
|
|
|
assert f"Effect must be one of: {light_effect.effect_list}" in res.output
|
|
|
|
assert res.exit_code == 2
|
|
|
|
|
|
|
|
|
|
|
|
async def test_led(dev: Device, runner: CliRunner):
|
|
|
|
res = await runner.invoke(led, obj=dev)
|
|
|
|
if not (led_module := dev.modules.get(Module.Led)):
|
|
|
|
assert "Device does not support led" in res.output
|
|
|
|
return
|
|
|
|
|
|
|
|
res = await runner.invoke(led, obj=dev)
|
|
|
|
assert f"LED state: {led_module.led}" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
res = await runner.invoke(led, ["on"], obj=dev)
|
|
|
|
assert "Turning led to True" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
await dev.update()
|
|
|
|
assert led_module.led is True
|
|
|
|
|
|
|
|
res = await runner.invoke(led, ["off"], obj=dev)
|
|
|
|
assert "Turning led to False" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
await dev.update()
|
|
|
|
assert led_module.led is False
|
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_json_output(dev: Device, mocker, runner):
|
2023-02-18 20:41:08 +00:00
|
|
|
"""Test that the json output produces correct output."""
|
2024-06-17 09:04:46 +00:00
|
|
|
mocker.patch("kasa.Discover.discover_single", return_value=dev)
|
|
|
|
# These will mock the features to avoid accessing non-existing ones
|
2024-02-15 15:25:08 +00:00
|
|
|
mocker.patch("kasa.device.Device.features", return_value={})
|
|
|
|
mocker.patch("kasa.iot.iotdevice.IotDevice.features", return_value={})
|
|
|
|
|
2024-06-17 09:04:46 +00:00
|
|
|
res = await runner.invoke(cli, ["--host", "127.0.0.1", "--json", "state"], obj=dev)
|
2023-02-18 20:41:08 +00:00
|
|
|
assert res.exit_code == 0
|
|
|
|
assert json.loads(res.output) == dev.internal_state
|
2023-09-13 13:46:38 +00:00
|
|
|
|
|
|
|
|
2023-12-04 18:50:05 +00:00
|
|
|
@new_discovery
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_credentials(discovery_mock, mocker, runner):
|
2023-09-13 13:46:38 +00:00
|
|
|
"""Test credentials are passed correctly from cli to device."""
|
|
|
|
# Patch state to echo username and password
|
2024-02-04 15:20:08 +00:00
|
|
|
pass_dev = click.make_pass_decorator(Device)
|
2023-09-13 13:46:38 +00:00
|
|
|
|
|
|
|
@pass_dev
|
2024-02-04 15:20:08 +00:00
|
|
|
async def _state(dev: Device):
|
2023-09-13 13:46:38 +00:00
|
|
|
if dev.credentials:
|
|
|
|
click.echo(
|
|
|
|
f"Username:{dev.credentials.username} Password:{dev.credentials.password}"
|
|
|
|
)
|
|
|
|
|
2024-07-04 12:52:01 +00:00
|
|
|
mocker.patch("kasa.cli.main.state", new=_state)
|
2023-09-13 13:46:38 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
dr = DiscoveryResult(**discovery_mock.discovery_data["result"])
|
2023-09-13 13:46:38 +00:00
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--host",
|
2023-12-04 18:50:05 +00:00
|
|
|
"127.0.0.123",
|
2023-09-13 13:46:38 +00:00
|
|
|
"--username",
|
|
|
|
"foo",
|
|
|
|
"--password",
|
|
|
|
"bar",
|
2023-12-29 19:17:15 +00:00
|
|
|
"--device-family",
|
|
|
|
dr.device_type,
|
|
|
|
"--encrypt-type",
|
|
|
|
dr.mgt_encrypt_schm.encrypt_type,
|
2023-09-13 13:46:38 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
assert res.exit_code == 0
|
2023-12-04 18:50:05 +00:00
|
|
|
|
|
|
|
assert "Username:foo Password:bar\n" in res.output
|
2023-09-13 13:46:38 +00:00
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_without_device_type(dev, mocker, runner):
|
2023-11-21 22:48:53 +00:00
|
|
|
"""Test connecting without the device type."""
|
2024-02-08 19:03:06 +00:00
|
|
|
discovery_mock = mocker.patch(
|
|
|
|
"kasa.discover.Discover.discover_single", return_value=dev
|
|
|
|
)
|
2024-02-15 15:25:08 +00:00
|
|
|
# These will mock the features to avoid accessing non-existing
|
|
|
|
mocker.patch("kasa.device.Device.features", return_value={})
|
|
|
|
mocker.patch("kasa.iot.iotdevice.IotDevice.features", return_value={})
|
|
|
|
|
2023-11-21 22:48:53 +00:00
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--host",
|
|
|
|
"127.0.0.1",
|
|
|
|
"--username",
|
|
|
|
"foo",
|
|
|
|
"--password",
|
|
|
|
"bar",
|
2024-02-08 19:03:06 +00:00
|
|
|
"--discovery-timeout",
|
|
|
|
"7",
|
2023-11-21 22:48:53 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
assert res.exit_code == 0
|
2024-02-08 19:03:06 +00:00
|
|
|
discovery_mock.assert_called_once_with(
|
|
|
|
"127.0.0.1",
|
|
|
|
port=None,
|
|
|
|
credentials=Credentials("foo", "bar"),
|
|
|
|
timeout=5,
|
|
|
|
discovery_timeout=7,
|
|
|
|
)
|
2023-11-21 22:48:53 +00:00
|
|
|
|
|
|
|
|
2023-09-13 13:46:38 +00:00
|
|
|
@pytest.mark.parametrize("auth_param", ["--username", "--password"])
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_invalid_credential_params(auth_param, runner):
|
2023-10-04 21:35:26 +00:00
|
|
|
"""Test for handling only one of username or password supplied."""
|
2023-09-13 13:46:38 +00:00
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--host",
|
|
|
|
"127.0.0.1",
|
|
|
|
"--type",
|
|
|
|
"plug",
|
|
|
|
auth_param,
|
|
|
|
"foo",
|
|
|
|
],
|
|
|
|
)
|
2023-10-04 21:35:26 +00:00
|
|
|
assert res.exit_code == 2
|
2023-09-13 13:46:38 +00:00
|
|
|
assert (
|
2023-10-04 21:35:26 +00:00
|
|
|
"Error: Using authentication requires both --username and --password"
|
|
|
|
in res.output
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_duplicate_target_device(runner):
|
2023-10-04 21:35:26 +00:00
|
|
|
"""Test that defining both --host or --alias gives an error."""
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--host",
|
|
|
|
"127.0.0.1",
|
|
|
|
"--alias",
|
|
|
|
"foo",
|
|
|
|
],
|
2023-09-13 13:46:38 +00:00
|
|
|
)
|
2023-10-04 21:35:26 +00:00
|
|
|
assert res.exit_code == 2
|
|
|
|
assert "Error: Use either --alias or --host, not both." in res.output
|
2023-12-19 12:50:33 +00:00
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_discover(discovery_mock, mocker, runner):
|
2023-12-19 12:50:33 +00:00
|
|
|
"""Test discovery output."""
|
2024-02-15 15:25:08 +00:00
|
|
|
# These will mock the features to avoid accessing non-existing
|
|
|
|
mocker.patch("kasa.device.Device.features", return_value={})
|
|
|
|
mocker.patch("kasa.iot.iotdevice.IotDevice.features", return_value={})
|
|
|
|
|
2023-12-19 12:50:33 +00:00
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--discovery-timeout",
|
|
|
|
0,
|
|
|
|
"--username",
|
|
|
|
"foo",
|
|
|
|
"--password",
|
|
|
|
"bar",
|
|
|
|
"--verbose",
|
2023-12-29 15:04:41 +00:00
|
|
|
"discover",
|
2023-12-19 12:50:33 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_discover_host(discovery_mock, mocker, runner):
|
2024-02-05 17:53:09 +00:00
|
|
|
"""Test discovery output."""
|
2024-02-15 15:25:08 +00:00
|
|
|
# These will mock the features to avoid accessing non-existing
|
|
|
|
mocker.patch("kasa.device.Device.features", return_value={})
|
|
|
|
mocker.patch("kasa.iot.iotdevice.IotDevice.features", return_value={})
|
|
|
|
|
2024-02-05 17:53:09 +00:00
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--discovery-timeout",
|
|
|
|
0,
|
|
|
|
"--host",
|
|
|
|
"127.0.0.123",
|
|
|
|
"--username",
|
|
|
|
"foo",
|
|
|
|
"--password",
|
|
|
|
"bar",
|
|
|
|
"--verbose",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_discover_unsupported(unsupported_device_info, runner):
|
2023-12-19 12:50:33 +00:00
|
|
|
"""Test discovery output."""
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--discovery-timeout",
|
|
|
|
0,
|
|
|
|
"--username",
|
|
|
|
"foo",
|
|
|
|
"--password",
|
|
|
|
"bar",
|
|
|
|
"--verbose",
|
2023-12-29 15:04:41 +00:00
|
|
|
"discover",
|
2023-12-19 12:50:33 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
assert res.exit_code == 0
|
|
|
|
assert "== Unsupported device ==" in res.output
|
|
|
|
assert "== Discovery Result ==" in res.output
|
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_host_unsupported(unsupported_device_info, runner):
|
2023-12-19 12:50:33 +00:00
|
|
|
"""Test discovery output."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--host",
|
|
|
|
host,
|
|
|
|
"--username",
|
|
|
|
"foo",
|
|
|
|
"--password",
|
|
|
|
"bar",
|
2024-02-20 11:21:04 +00:00
|
|
|
"--debug",
|
2023-12-19 12:50:33 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res.exit_code != 0
|
2024-02-21 15:52:55 +00:00
|
|
|
assert isinstance(res.exception, UnsupportedDeviceError)
|
2023-12-19 12:50:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@new_discovery
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_discover_auth_failed(discovery_mock, mocker, runner):
|
2023-12-19 12:50:33 +00:00
|
|
|
"""Test discovery output."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
discovery_mock.ip = host
|
|
|
|
device_class = Discover._get_device_class(discovery_mock.discovery_data)
|
|
|
|
mocker.patch.object(
|
|
|
|
device_class,
|
|
|
|
"update",
|
2024-02-21 15:52:55 +00:00
|
|
|
side_effect=AuthenticationError("Failed to authenticate"),
|
2023-12-19 12:50:33 +00:00
|
|
|
)
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--discovery-timeout",
|
|
|
|
0,
|
|
|
|
"--username",
|
|
|
|
"foo",
|
|
|
|
"--password",
|
|
|
|
"bar",
|
|
|
|
"--verbose",
|
2023-12-29 15:04:41 +00:00
|
|
|
"discover",
|
2023-12-19 12:50:33 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res.exit_code == 0
|
|
|
|
assert "== Authentication failed for device ==" in res.output
|
|
|
|
assert "== Discovery Result ==" in res.output
|
|
|
|
|
|
|
|
|
|
|
|
@new_discovery
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_host_auth_failed(discovery_mock, mocker, runner):
|
2023-12-19 12:50:33 +00:00
|
|
|
"""Test discovery output."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
discovery_mock.ip = host
|
|
|
|
device_class = Discover._get_device_class(discovery_mock.discovery_data)
|
|
|
|
mocker.patch.object(
|
|
|
|
device_class,
|
|
|
|
"update",
|
2024-02-21 15:52:55 +00:00
|
|
|
side_effect=AuthenticationError("Failed to authenticate"),
|
2023-12-19 12:50:33 +00:00
|
|
|
)
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--host",
|
|
|
|
host,
|
|
|
|
"--username",
|
|
|
|
"foo",
|
|
|
|
"--password",
|
|
|
|
"bar",
|
2024-02-20 11:21:04 +00:00
|
|
|
"--debug",
|
2023-12-19 12:50:33 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
assert res.exit_code != 0
|
2024-02-21 15:52:55 +00:00
|
|
|
assert isinstance(res.exception, AuthenticationError)
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("device_type", list(TYPE_TO_CLASS))
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_type_param(device_type, mocker, runner):
|
2023-12-29 19:17:15 +00:00
|
|
|
"""Test for handling only one of username or password supplied."""
|
|
|
|
result_device = FileNotFoundError
|
2024-02-04 15:20:08 +00:00
|
|
|
pass_dev = click.make_pass_decorator(Device)
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
@pass_dev
|
2024-02-04 15:20:08 +00:00
|
|
|
async def _state(dev: Device):
|
2023-12-29 19:17:15 +00:00
|
|
|
nonlocal result_device
|
|
|
|
result_device = dev
|
|
|
|
|
2024-07-04 12:52:01 +00:00
|
|
|
mocker.patch("kasa.cli.main.state", new=_state)
|
2023-12-29 19:17:15 +00:00
|
|
|
expected_type = TYPE_TO_CLASS[device_type]
|
|
|
|
mocker.patch.object(expected_type, "update")
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
["--type", device_type, "--host", "127.0.0.1"],
|
|
|
|
)
|
|
|
|
assert res.exit_code == 0
|
|
|
|
assert isinstance(result_device, expected_type)
|
2024-02-06 13:48:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip(
|
|
|
|
"Skip until pytest-asyncio supports pytest 8.0, https://github.com/pytest-dev/pytest-asyncio/issues/737"
|
|
|
|
)
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_shell(dev: Device, mocker, runner):
|
2024-02-06 13:48:19 +00:00
|
|
|
"""Test that the shell commands tries to embed a shell."""
|
|
|
|
mocker.patch("kasa.Discover.discover", return_value=[dev])
|
|
|
|
# repl = mocker.patch("ptpython.repl")
|
|
|
|
mocker.patch.dict(
|
|
|
|
"sys.modules",
|
|
|
|
{"ptpython": mocker.MagicMock(), "ptpython.repl": mocker.MagicMock()},
|
|
|
|
)
|
|
|
|
embed = mocker.patch("ptpython.repl.embed")
|
|
|
|
res = await runner.invoke(cli, ["shell"], obj=dev)
|
|
|
|
assert res.exit_code == 0
|
|
|
|
embed.assert_called()
|
2024-02-20 11:21:04 +00:00
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_errors(mocker, runner):
|
2024-02-21 15:52:55 +00:00
|
|
|
err = KasaException("Foobar")
|
2024-02-20 11:21:04 +00:00
|
|
|
|
|
|
|
# Test masking
|
|
|
|
mocker.patch("kasa.Discover.discover", side_effect=err)
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
["--username", "foo", "--password", "bar"],
|
|
|
|
)
|
|
|
|
assert res.exit_code == 1
|
|
|
|
assert "Raised error: Foobar" in res.output
|
|
|
|
assert "Run with --debug enabled to see stacktrace" in res.output
|
|
|
|
assert isinstance(res.exception, SystemExit)
|
|
|
|
|
|
|
|
# Test --debug
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
["--debug"],
|
|
|
|
)
|
|
|
|
assert res.exit_code == 1
|
|
|
|
assert "Raised error: Foobar" in res.output
|
|
|
|
assert res.exception == err
|
|
|
|
|
|
|
|
# Test no device passed to subcommand
|
|
|
|
mocker.patch("kasa.Discover.discover", return_value={})
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
["sysinfo"],
|
|
|
|
)
|
|
|
|
assert res.exit_code == 1
|
|
|
|
assert (
|
2024-06-17 09:04:46 +00:00
|
|
|
"Only discover is available without --host or --alias"
|
2024-02-20 18:40:28 +00:00
|
|
|
in res.output.replace("\n", "") # Remove newlines from rich formatting
|
2024-02-20 11:21:04 +00:00
|
|
|
)
|
|
|
|
assert isinstance(res.exception, SystemExit)
|
|
|
|
|
|
|
|
# Test click error
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
["--foobar"],
|
|
|
|
)
|
|
|
|
assert res.exit_code == 2
|
|
|
|
assert "Raised error:" not in res.output
|
2024-02-23 22:32:17 +00:00
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_feature(mocker, runner):
|
2024-02-23 22:32:17 +00:00
|
|
|
"""Test feature command."""
|
2024-02-27 17:39:04 +00:00
|
|
|
dummy_device = await get_device_for_fixture_protocol(
|
|
|
|
"P300(EU)_1.0_1.0.13.json", "SMART"
|
|
|
|
)
|
2024-02-23 22:32:17 +00:00
|
|
|
mocker.patch("kasa.discover.Discover.discover_single", return_value=dummy_device)
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
["--host", "127.0.0.123", "--debug", "feature"],
|
|
|
|
catch_exceptions=False,
|
|
|
|
)
|
|
|
|
assert "LED" in res.output
|
|
|
|
assert "== Child " in res.output # child listing
|
|
|
|
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
|
2024-05-02 14:31:12 +00:00
|
|
|
async def test_features_all(discovery_mock, mocker, runner):
|
|
|
|
"""Test feature command on all fixtures."""
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
["--host", "127.0.0.123", "--debug", "feature"],
|
|
|
|
catch_exceptions=False,
|
|
|
|
)
|
2024-06-23 06:09:13 +00:00
|
|
|
assert "== Primary features ==" in res.output
|
|
|
|
assert "== Information ==" in res.output
|
|
|
|
assert "== Configuration ==" in res.output
|
|
|
|
assert "== Debug ==" in res.output
|
2024-05-02 14:31:12 +00:00
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_feature_single(mocker, runner):
|
2024-02-23 22:32:17 +00:00
|
|
|
"""Test feature command returning single value."""
|
2024-02-27 17:39:04 +00:00
|
|
|
dummy_device = await get_device_for_fixture_protocol(
|
|
|
|
"P300(EU)_1.0_1.0.13.json", "SMART"
|
|
|
|
)
|
2024-02-23 22:32:17 +00:00
|
|
|
mocker.patch("kasa.discover.Discover.discover_single", return_value=dummy_device)
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
["--host", "127.0.0.123", "--debug", "feature", "led"],
|
|
|
|
catch_exceptions=False,
|
|
|
|
)
|
|
|
|
assert "LED" in res.output
|
|
|
|
assert "== Features ==" not in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
2024-02-27 17:39:04 +00:00
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_feature_missing(mocker, runner):
|
2024-02-23 22:32:17 +00:00
|
|
|
"""Test feature command returning single value."""
|
2024-02-27 17:39:04 +00:00
|
|
|
dummy_device = await get_device_for_fixture_protocol(
|
|
|
|
"P300(EU)_1.0_1.0.13.json", "SMART"
|
|
|
|
)
|
2024-02-23 22:32:17 +00:00
|
|
|
mocker.patch("kasa.discover.Discover.discover_single", return_value=dummy_device)
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
["--host", "127.0.0.123", "--debug", "feature", "missing"],
|
|
|
|
catch_exceptions=False,
|
|
|
|
)
|
|
|
|
assert "No feature by name 'missing'" in res.output
|
|
|
|
assert "== Features ==" not in res.output
|
2024-06-17 09:04:46 +00:00
|
|
|
assert res.exit_code == 1
|
2024-02-23 22:32:17 +00:00
|
|
|
|
2024-02-27 17:39:04 +00:00
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_feature_set(mocker, runner):
|
2024-02-23 22:32:17 +00:00
|
|
|
"""Test feature command's set value."""
|
2024-02-27 17:39:04 +00:00
|
|
|
dummy_device = await get_device_for_fixture_protocol(
|
|
|
|
"P300(EU)_1.0_1.0.13.json", "SMART"
|
|
|
|
)
|
2024-05-11 18:28:18 +00:00
|
|
|
led_setter = mocker.patch("kasa.smart.modules.led.Led.set_led")
|
2024-02-23 22:32:17 +00:00
|
|
|
mocker.patch("kasa.discover.Discover.discover_single", return_value=dummy_device)
|
|
|
|
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
["--host", "127.0.0.123", "--debug", "feature", "led", "True"],
|
|
|
|
catch_exceptions=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
led_setter.assert_called_with(True)
|
2024-06-23 06:22:29 +00:00
|
|
|
assert "Changing led from True to True" in res.output
|
2024-02-23 22:32:17 +00:00
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
|
2024-04-20 15:14:53 +00:00
|
|
|
async def test_feature_set_child(mocker, runner):
|
2024-02-23 22:32:17 +00:00
|
|
|
"""Test feature command's set value."""
|
2024-02-27 17:39:04 +00:00
|
|
|
dummy_device = await get_device_for_fixture_protocol(
|
|
|
|
"P300(EU)_1.0_1.0.13.json", "SMART"
|
|
|
|
)
|
2024-02-23 22:32:17 +00:00
|
|
|
setter = mocker.patch("kasa.smart.smartdevice.SmartDevice.set_state")
|
|
|
|
|
|
|
|
mocker.patch("kasa.discover.Discover.discover_single", return_value=dummy_device)
|
|
|
|
get_child_device = mocker.spy(dummy_device, "get_child_device")
|
|
|
|
|
|
|
|
child_id = "000000000000000000000000000000000000000001"
|
|
|
|
|
|
|
|
res = await runner.invoke(
|
|
|
|
cli,
|
|
|
|
[
|
|
|
|
"--host",
|
|
|
|
"127.0.0.123",
|
|
|
|
"--debug",
|
|
|
|
"feature",
|
|
|
|
"--child",
|
|
|
|
child_id,
|
|
|
|
"state",
|
2024-05-02 14:31:12 +00:00
|
|
|
"True",
|
2024-02-23 22:32:17 +00:00
|
|
|
],
|
|
|
|
catch_exceptions=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
get_child_device.assert_called()
|
2024-05-02 14:31:12 +00:00
|
|
|
setter.assert_called_with(True)
|
2024-02-23 22:32:17 +00:00
|
|
|
|
|
|
|
assert f"Targeting child device {child_id}"
|
2024-05-02 14:31:12 +00:00
|
|
|
assert "Changing state from False to True" in res.output
|
2024-02-23 22:32:17 +00:00
|
|
|
assert res.exit_code == 0
|
2024-07-02 13:11:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_cli_child_commands(
|
|
|
|
dev: Device, runner: CliRunner, mocker: MockerFixture
|
|
|
|
):
|
|
|
|
if not dev.children:
|
|
|
|
res = await runner.invoke(alias, ["--child-index", "0"], obj=dev)
|
|
|
|
assert f"Device: {dev.host} does not have children" in res.output
|
|
|
|
assert res.exit_code == 1
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["--index", "0"], obj=dev)
|
|
|
|
assert f"Device: {dev.host} does not have children" in res.output
|
|
|
|
assert res.exit_code == 1
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["--child", "Plug 2"], obj=dev)
|
|
|
|
assert f"Device: {dev.host} does not have children" in res.output
|
|
|
|
assert res.exit_code == 1
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["--name", "Plug 2"], obj=dev)
|
|
|
|
assert f"Device: {dev.host} does not have children" in res.output
|
|
|
|
assert res.exit_code == 1
|
|
|
|
|
|
|
|
if dev.children:
|
|
|
|
child_alias = dev.children[0].alias
|
|
|
|
assert child_alias
|
|
|
|
child_device_id = dev.children[0].device_id
|
|
|
|
child_count = len(dev.children)
|
|
|
|
child_update_method = dev.children[0].update
|
|
|
|
|
|
|
|
# Test child retrieval
|
|
|
|
res = await runner.invoke(alias, ["--child-index", "0"], obj=dev)
|
|
|
|
assert f"Targeting child device {child_alias}" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["--index", "0"], obj=dev)
|
|
|
|
assert f"Targeting child device {child_alias}" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["--child", child_alias], obj=dev)
|
|
|
|
assert f"Targeting child device {child_alias}" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["--name", child_alias], obj=dev)
|
|
|
|
assert f"Targeting child device {child_alias}" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["--child", child_device_id], obj=dev)
|
|
|
|
assert f"Targeting child device {child_alias}" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["--name", child_device_id], obj=dev)
|
|
|
|
assert f"Targeting child device {child_alias}" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
# Test invalid name and index
|
|
|
|
res = await runner.invoke(alias, ["--child-index", "-1"], obj=dev)
|
|
|
|
assert f"Invalid index -1, device has {child_count} children" in res.output
|
|
|
|
assert res.exit_code == 1
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["--child-index", str(child_count)], obj=dev)
|
|
|
|
assert (
|
|
|
|
f"Invalid index {child_count}, device has {child_count} children"
|
|
|
|
in res.output
|
|
|
|
)
|
|
|
|
assert res.exit_code == 1
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["--child", "foobar"], obj=dev)
|
|
|
|
assert "No child device found with device_id or name: foobar" in res.output
|
|
|
|
assert res.exit_code == 1
|
|
|
|
|
|
|
|
# Test using both options:
|
|
|
|
|
|
|
|
res = await runner.invoke(
|
|
|
|
alias, ["--child", child_alias, "--child-index", "0"], obj=dev
|
|
|
|
)
|
|
|
|
assert "Use either --child or --child-index, not both." in res.output
|
|
|
|
assert res.exit_code == 2
|
|
|
|
|
|
|
|
# Test child with no parameter interactive prompt
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["--child"], obj=dev, input="0\n")
|
|
|
|
assert "Enter the index number of the child device:" in res.output
|
|
|
|
assert f"Alias: {child_alias}" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
# Test values and updates
|
|
|
|
|
|
|
|
res = await runner.invoke(alias, ["foo", "--child", child_device_id], obj=dev)
|
|
|
|
assert "Alias set to: foo" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
# Test help has command options plus child options
|
|
|
|
|
|
|
|
res = await runner.invoke(energy, ["--help"], obj=dev)
|
|
|
|
assert "--year" in res.output
|
|
|
|
assert "--child" in res.output
|
|
|
|
assert "--child-index" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
|
|
|
|
# Test child update patching calls parent and is undone on exit
|
|
|
|
|
|
|
|
parent_update_spy = mocker.spy(dev, "update")
|
|
|
|
res = await runner.invoke(alias, ["bar", "--child", child_device_id], obj=dev)
|
|
|
|
assert "Alias set to: bar" in res.output
|
|
|
|
assert res.exit_code == 0
|
|
|
|
parent_update_spy.assert_called_once()
|
|
|
|
assert dev.children[0].update == child_update_method
|