From 902b43d82e587ac68bc072465b6b82ee6fa9b1e4 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Sat, 31 Mar 2018 20:29:04 +0200 Subject: [PATCH] Port cli code to the new plug implementation --- miio/chuangmi_plug.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/miio/chuangmi_plug.py b/miio/chuangmi_plug.py index 5da4a30a8..be7d06abe 100644 --- a/miio/chuangmi_plug.py +++ b/miio/chuangmi_plug.py @@ -1,8 +1,12 @@ import logging +import click from typing import Dict, Any, Optional from collections import defaultdict from .device import Device from .utils import deprecated +from .click_common import ( + DeviceGroupMeta, device_command, echo_return_status +) _LOGGER = logging.getLogger(__name__) @@ -85,7 +89,7 @@ def __repr__(self) -> str: return s -class ChuangmiPlug(Device): +class ChuangmiPlug(Device, metaclass=DeviceGroupMeta): """Main class representing the Chuangmi Plug V1 and V3.""" def __init__(self, ip: str = None, token: str = None, start_id: int = 0, @@ -98,6 +102,14 @@ def __init__(self, ip: str = None, token: str = None, start_id: int = 0, else: self.model = MODEL_CHUANGMI_PLUG_M1 + @device_command( + echo_return_status("", + "Power: {result.power}\n" + "USB Power: {result.usb_power}\n" + "Temperature: {result.temperature} °C\n" + "Load power: {result.load_power}\n" + "WiFi LED: {result.wifi_led}") + ) def status(self) -> ChuangmiPlugStatus: """Retrieve properties.""" properties = AVAILABLE_PROPERTIES[self.model] @@ -123,6 +135,7 @@ def status(self) -> ChuangmiPlugStatus: return ChuangmiPlugStatus( defaultdict(lambda: None, zip(properties, values))) + @device_command(echo_return_status("Powering on")) def on(self): """Power on.""" if self.model == MODEL_CHUANGMI_PLUG_V1: @@ -130,6 +143,7 @@ def on(self): return self.send("set_power", ["on"]) + @device_command(echo_return_status("Powering off")) def off(self): """Power off.""" if self.model == MODEL_CHUANGMI_PLUG_V1: @@ -137,14 +151,23 @@ def off(self): return self.send("set_power", ["off"]) + @device_command(echo_return_status("Powering USB on")) def usb_on(self): """Power on.""" return self.send("set_usb_on", []) + @device_command(echo_return_status("Powering USB off")) def usb_off(self): """Power off.""" return self.send("set_usb_off", []) + @device_command( + click.argument("wifi_led", type=bool), + echo_return_status( + lambda wifi_led: "Turning on WiFi LED" + if wifi_led else "Turning off WiFi LED" + ) + ) def set_wifi_led(self, led: bool): """Set the wifi led on/off.""" if led: