Skip to content

Commit

Permalink
Add supports_miot to device class (#1659)
Browse files Browse the repository at this point in the history
This allows querying whether the device supports miot commands.
  • Loading branch information
rytilahti authored Jan 8, 2023
1 parent ae35d8a commit 771b295
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
18 changes: 17 additions & 1 deletion miio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
)
from .deviceinfo import DeviceInfo
from .devicestatus import DeviceStatus
from .exceptions import DeviceInfoUnavailableException, PayloadDecodeException
from .exceptions import (
DeviceError,
DeviceInfoUnavailableException,
PayloadDecodeException,
)
from .miioprotocol import MiIOProtocol

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -298,5 +302,17 @@ def sensors(self) -> Dict[str, SensorDescriptor]:
sensors = self.status().sensors()
return sensors

def supports_miot(self) -> bool:
"""Return True if the device supports miot commands.
This requests a single property (siid=1, piid=1) and returns True on success.
"""
try:
self.send("get_properties", [{"did": "dummy", "siid": 1, "piid": 1}])
except DeviceError as ex:
_LOGGER.debug("miot query failed, likely non-miot device: %s", repr(ex))
return False
return True

def __repr__(self):
return f"<{self.__class__.__name__ }: {self.ip} (token: {self.token})>"
13 changes: 13 additions & 0 deletions miio/tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,16 @@ def test_init_signature(cls, mocker):
# as some arguments are passed by inheriting classes using kwargs
total_args = len(parent_init.call_args.args) + len(parent_init.call_args.kwargs)
assert total_args == 8


def test_supports_miot(mocker):
from miio.exceptions import DeviceError

send = mocker.patch(
"miio.Device.send", side_effect=DeviceError({"code": 1, "message": 1})
)
d = Device("127.0.0.1", "68ffffffffffffffffffffffffffffff")
assert d.supports_miot() is False

send.side_effect = None
assert d.supports_miot() is True

0 comments on commit 771b295

Please sign in to comment.