From 59f6b1551039035dde9821f5f58efb4a803fb5b7 Mon Sep 17 00:00:00 2001 From: Teemu R Date: Sat, 21 Oct 2023 02:09:27 +0200 Subject: [PATCH] Use __cli_output__ for info() (#1847) Move output formatting into DeviceInfo class. --- miio/device.py | 20 +------------------- miio/deviceinfo.py | 17 +++++++++++++++++ miio/tests/test_deviceinfo.py | 13 +++++++++++++ 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/miio/device.py b/miio/device.py index 6c29d053d..5a130b7ac 100644 --- a/miio/device.py +++ b/miio/device.py @@ -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 @@ -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. @@ -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: diff --git a/miio/deviceinfo.py b/miio/deviceinfo.py index fdff5f91c..4f2e44901 100644 --- a/miio/deviceinfo.py +++ b/miio/deviceinfo.py @@ -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 diff --git a/miio/tests/test_deviceinfo.py b/miio/tests/test_deviceinfo.py index d577078d7..bd0f3362d 100644 --- a/miio/tests/test_deviceinfo.py +++ b/miio/tests/test_deviceinfo.py @@ -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