Files
python-kasa/tests/cli/test_hub.py
ZeliardM 1a84242a05 tests: add type annotations to CLI, protocol, and smartcam tests (#1687)
Add type annotations (`-> None` return types and parameter types) to
test files in the `tests/cli/`, `tests/protocols/`, and
`tests/smartcam/` directories. This enables mypy to check test function
bodies, catching type errors that were previously hidden.
2026-07-05 18:46:01 +02:00

62 lines
1.8 KiB
Python

import pytest
from asyncclick.testing import CliRunner
from pytest_mock import MockerFixture
from kasa import Device, DeviceType, Module
from kasa.cli.hub import hub
from ..device_fixtures import hubs, plug_iot
@hubs
async def test_hub_pair(
dev: Device,
mocker: MockerFixture,
runner: CliRunner,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that pair calls the expected methods."""
cs = dev.modules.get(Module.ChildSetup)
# Patch if the device supports the module
if cs is not None:
mock_pair = mocker.patch.object(cs, "pair")
res = await runner.invoke(hub, ["pair"], obj=dev, catch_exceptions=False)
if cs is None:
assert "is not a hub" in res.output
return
mock_pair.assert_awaited()
assert "Finding new devices for 10 seconds" in res.output
assert res.exit_code == 0
@hubs
async def test_hub_unpair(
dev: Device, mocker: MockerFixture, runner: CliRunner
) -> None:
"""Test that unpair calls the expected method."""
if not dev.children:
pytest.skip("Cannot test without child devices")
id_ = next(iter(dev.children)).device_id
cs = dev.modules.get(Module.ChildSetup)
mock_unpair = mocker.spy(cs, "unpair")
res = await runner.invoke(hub, ["unpair", id_], obj=dev, catch_exceptions=False)
mock_unpair.assert_awaited()
assert f"Unpaired {id_}" in res.output
assert res.exit_code == 0
@plug_iot
async def test_non_hub(dev: Device, mocker: MockerFixture, runner: CliRunner) -> None:
"""Test that hub commands return an error if executed on a non-hub."""
assert dev.device_type is not DeviceType.Hub
res = await runner.invoke(
hub, ["unpair", "dummy_id"], obj=dev, catch_exceptions=False
)
assert "is not a hub" in res.output