import math import pytest from miio import Device, MiotDevice, RoborockVacuum from miio.exceptions import DeviceInfoUnavailableException, PayloadDecodeException DEVICE_CLASSES = Device.__subclasses__() + MiotDevice.__subclasses__( ) # type: ignore DEVICE_CLASSES.remove(MiotDevice) @pytest.mark.parametrize("max_properties", [None, 1, 15]) def test_get_properties_splitting(mocker, max_properties): properties = [i for i in range(20)] send = mocker.patch("miio.Device.send") d = Device("127.0.0.1", "68ffffffffffffffffffffffffffffff") d.get_properties(properties, max_properties=max_properties) if max_properties is None: max_properties = len(properties) assert send.call_count == math.ceil(len(properties) / max_properties) def test_default_timeout_and_retry(mocker): send = mocker.patch("miio.miioprotocol.MiIOProtocol.send") d = Device("127.0.0.1", "68ffffffffffffffffffffffffffffff") assert d._protocol._timeout == 5 d.send(command="fake_command", parameters=[]) send.assert_called_with("fake_command", [], 3, extra_parameters=None)
DUMMY_MODEL = "dummy.model" d = Device("127.0.0.1", "68ffffffffffffffffffffffffffffff", model=DUMMY_MODEL) d.raw_command("dummy", {}) assert d.model == DUMMY_MODEL info.assert_not_called() def test_missing_supported(mocker, caplog): """Make sure warning is logged if the device is unsupported for the class.""" _ = mocker.patch("miio.Device.send") d = Device("127.0.0.1", "68ffffffffffffffffffffffffffffff") d._fetch_info() assert "Found an unsupported model" in caplog.text assert "for class 'Device'" in caplog.text @pytest.mark.parametrize("cls", Device.__subclasses__()) def test_device_ctor_model(cls): """Make sure that every device subclass ctor accepts model kwarg.""" ignore_classes = ["GatewayDevice", "CustomDevice"] if cls.__name__ in ignore_classes: return dummy_model = "dummy" dev = cls("127.0.0.1", "68ffffffffffffffffffffffffffffff", model=dummy_model) assert dev.model == dummy_model