Skip to content

Commit ffef7fb

Browse files
committed
Add tests for DeviceFactory.create
1 parent 96fa246 commit ffef7fb

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

miio/devicefactory.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ def register(cls, integration_cls: Type[Device]):
3333
for model in integration_cls.supported_models: # type: ignore
3434
if model in cls._supported_models:
3535
_LOGGER.debug(
36-
"Got duplicate of %s for %s, previously registered by %s",
36+
"Ignoring duplicate of %s for %s, previously registered by %s",
3737
model,
3838
integration_cls,
3939
cls._supported_models[model],
4040
)
41+
continue
4142

4243
_LOGGER.debug(" * %s => %s", model, integration_cls)
4344
cls._supported_models[model] = integration_cls
@@ -97,7 +98,7 @@ def create(
9798
from .integrations.genericmiot import GenericMiot
9899

99100
dev = GenericMiot(host, token, model=model)
100-
dev.info() # HACK: we have to force update to load the miot schema
101+
dev.info()
101102
return dev
102103
if model is None:
103104
dev = Device(host, token)

miio/tests/test_devicefactory.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from miio import Device, DeviceFactory, Gateway, GenericMiot, MiotDevice
3+
from miio import Device, DeviceFactory, DeviceInfo, Gateway, GenericMiot, MiotDevice
44

55
DEVICE_CLASSES = Device.__subclasses__() + MiotDevice.__subclasses__() # type: ignore
66
DEVICE_CLASSES.remove(MiotDevice)
@@ -39,3 +39,42 @@ class _DummyDevice(Device):
3939
def test_device_class_for_model_unknown():
4040
"""Test that unknown model returns genericmiot."""
4141
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

Comments
 (0)