Skip to content

Commit

Permalink
Skip warning if the unknown model is reported on a base class (#1243)
Browse files Browse the repository at this point in the history
* Skip warning if the unknown model is reported on a base class

* Add tests + fix test_model_autodetection
  • Loading branch information
rytilahti authored Dec 13, 2021
1 parent 1772ba2 commit 4315253
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
6 changes: 4 additions & 2 deletions miio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ def _fetch_info(self) -> DeviceInfo:
devinfo = DeviceInfo(self.send("miIO.info"))
self._info = devinfo
_LOGGER.debug("Detected model %s", devinfo.model)
if devinfo.model not in self.supported_models:
cls = self.__class__.__name__
bases = ["Device", "MiotDevice"]
if devinfo.model not in self.supported_models and cls not in bases:
_LOGGER.warning(
"Found an unsupported model '%s' for class '%s'. If this is working for you, please open an issue at https://github.com/rytilahti/python-miio/",
self.model,
self.__class__.__name__,
cls,
)

return devinfo
Expand Down
19 changes: 13 additions & 6 deletions miio/tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from miio import Device
from miio import Device, MiotDevice, Vacuum
from miio.exceptions import DeviceInfoUnavailableException, PayloadDecodeException


Expand Down Expand Up @@ -59,7 +59,7 @@ def test_unavailable_device_info_raises(mocker):

def test_model_autodetection(mocker):
"""Make sure info() gets called if the model is unknown."""
info = mocker.patch("miio.Device.info")
info = mocker.patch("miio.Device._fetch_info")
_ = mocker.patch("miio.Device.send")

d = Device("127.0.0.1", "68ffffffffffffffffffffffffffffff")
Expand All @@ -83,15 +83,22 @@ def test_forced_model(mocker):
info.assert_not_called()


def test_missing_supported(mocker, caplog):
@pytest.mark.parametrize(
"cls,hidden", [(Device, True), (MiotDevice, True), (Vacuum, False)]
)
def test_missing_supported(mocker, caplog, cls, hidden):
"""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 = cls("127.0.0.1", "68ffffffffffffffffffffffffffffff")
d._fetch_info()

assert "Found an unsupported model" in caplog.text
assert "for class 'Device'" in caplog.text
if hidden:
assert "Found an unsupported model" not in caplog.text
assert f"for class '{cls.__name__}'" not in caplog.text
else:
assert "Found an unsupported model" in caplog.text
assert f"for class '{cls.__name__}'" in caplog.text


@pytest.mark.parametrize("cls", Device.__subclasses__())
Expand Down

0 comments on commit 4315253

Please sign in to comment.