Skip to content

Commit

Permalink
Use __cli_output__ for info()
Browse files Browse the repository at this point in the history
  • Loading branch information
rytilahti committed Oct 20, 2023
1 parent 72880ae commit f8ce619
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
20 changes: 1 addition & 19 deletions miio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import click

from .click_common import DeviceGroupMeta, LiteralParamType, command, format_output
from .click_common import DeviceGroupMeta, LiteralParamType, command
from .descriptorcollection import DescriptorCollection
from .descriptors import AccessFlags, ActionDescriptor, Descriptor, PropertyDescriptor
from .deviceinfo import DeviceInfo
Expand All @@ -26,23 +26,6 @@ class UpdateState(Enum):
Idle = "idle"


def _info_output(result):
"""Format the output for info command."""
s = f"Model: {result.model}\n"
s += f"Hardware version: {result.hardware_version}\n"
s += f"Firmware version: {result.firmware_version}\n"

from .devicefactory import DeviceFactory

cls = DeviceFactory.class_for_model(result.model)
dev = DeviceFactory.create(result.ip_address, result.token, force_generic_miot=True)
s += f"Supported using: {cls.__name__}\n"
s += f"Command: miiocli {cls.__name__.lower()} --ip {result.ip_address} --token {result.token}\n"
s += f"Supported by genericmiot: {dev.supports_miot()}"

return s


class Device(metaclass=DeviceGroupMeta):
"""Base class for all device implementations.
Expand Down Expand Up @@ -134,7 +117,6 @@ def raw_command(self, command, parameters):
return self.send(command, parameters)

@command(
default_output=format_output(result_msg_fmt=_info_output),
skip_autodetect=True,
)
def info(self, *, skip_cache=False) -> DeviceInfo:
Expand Down
17 changes: 17 additions & 0 deletions miio/deviceinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,20 @@ def token(self) -> Optional[str]:
def raw(self):
"""Raw data as returned by the device."""
return self.data

@property
def __cli_output__(self):
"""Format the output for info command."""
s = f"Model: {self.model}\n"
s += f"Hardware version: {self.hardware_version}\n"
s += f"Firmware version: {self.firmware_version}\n"

from .devicefactory import DeviceFactory

cls = DeviceFactory.class_for_model(self.model)
dev = DeviceFactory.create(self.ip_address, self.token, force_generic_miot=True)
s += f"Supported using: {cls.__name__}\n"
s += f"Command: miiocli {cls.__name__.lower()} --ip {self.ip_address} --token {self.token}\n"
s += f"Supported by genericmiot: {dev.supports_miot()}"

return s
13 changes: 13 additions & 0 deletions miio/tests/test_deviceinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,16 @@ def test_missing_fields(info):
assert info.hardware_version is None
assert info.mac_address is None
assert info.token is None


def test_cli_output(info, mocker):
mocker.patch("miio.Device.send")
mocker.patch("miio.Device.supports_miot", return_value=False)

output = info.__cli_output__
assert "Model: chuangmi.plug.m1" in output
assert "Hardware version: MW300" in output
assert "Firmware version: 1.2.4_16" in output
assert "Supported using: ChuangmiPlug" in output
assert "Command: miiocli chuangmiplug" in output
assert "Supported by genericmiot: False" in output

0 comments on commit f8ce619

Please sign in to comment.