|
1 | 1 | import pytest |
2 | 2 |
|
3 | | -from miio import Device, DeviceFactory, Gateway, GenericMiot, MiotDevice |
| 3 | +from miio import Device, DeviceFactory, DeviceInfo, Gateway, GenericMiot, MiotDevice |
4 | 4 |
|
5 | 5 | DEVICE_CLASSES = Device.__subclasses__() + MiotDevice.__subclasses__() # type: ignore |
6 | 6 | DEVICE_CLASSES.remove(MiotDevice) |
@@ -39,3 +39,42 @@ class _DummyDevice(Device): |
39 | 39 | def test_device_class_for_model_unknown(): |
40 | 40 | """Test that unknown model returns genericmiot.""" |
41 | 41 | assert DeviceFactory.class_for_model("foo.foo.xyz.invalid") == GenericMiot |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize("cls", DEVICE_CLASSES) |
| 45 | +@pytest.mark.parametrize("force_model", [True, False]) |
| 46 | +def test_create(cls, force_model, mocker): |
| 47 | + """Test create for both forced and autodetected models.""" |
| 48 | + mocker.patch("miio.Device.send") |
| 49 | + |
| 50 | + model = None |
| 51 | + first_supported_model = next(iter(cls.supported_models)) |
| 52 | + if force_model: |
| 53 | + model = first_supported_model |
| 54 | + |
| 55 | + dummy_info = DeviceInfo({"model": first_supported_model}) |
| 56 | + info = mocker.patch("miio.Device.info", return_value=dummy_info) |
| 57 | + |
| 58 | + device = DeviceFactory.create("127.0.0.1", 32 * "0", model=model) |
| 59 | + device_class = DeviceFactory.class_for_model(device.model) |
| 60 | + assert isinstance(device, device_class) |
| 61 | + |
| 62 | + if force_model: |
| 63 | + info.assert_not_called() |
| 64 | + else: |
| 65 | + info.assert_called() |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("cls", DEVICE_CLASSES) |
| 69 | +def test_create_force_miot(cls, mocker): |
| 70 | + """Test that force_generic_miot works.""" |
| 71 | + mocker.patch("miio.Device.send") |
| 72 | + mocker.patch("miio.Device.info") |
| 73 | + class_for_model = mocker.patch("miio.DeviceFactory.class_for_model") |
| 74 | + |
| 75 | + assert isinstance( |
| 76 | + DeviceFactory.create("127.0.0.1", 32 * "0", force_generic_miot=True), |
| 77 | + GenericMiot, |
| 78 | + ) |
| 79 | + |
| 80 | + class_for_model.assert_not_called() |
0 commit comments