Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add supports_miot to device class #1659

Merged
merged 1 commit into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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