-
-
Notifications
You must be signed in to change notification settings - Fork 563
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
Unified and scalable command line interface #191
Changes from all commits
1ee4568
d140f23
ac79d8b
6b1d5fa
d72944f
03bbab3
52383f4
6b8b74f
4b975f8
c3b4eb2
689bbf2
4e3e3e5
f817888
902b43d
2885f92
1b66cce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
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 command, format_output | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
@@ -84,6 +86,9 @@ def __repr__(self) -> str: | |
self.wifi_led) | ||
return s | ||
|
||
def __json__(self): | ||
return self.data | ||
|
||
|
||
class ChuangmiPlug(Device): | ||
"""Main class representing the Chuangmi Plug V1 and V3.""" | ||
|
@@ -98,6 +103,15 @@ def __init__(self, ip: str = None, token: str = None, start_id: int = 0, | |
else: | ||
self.model = MODEL_CHUANGMI_PLUG_M1 | ||
|
||
@command( | ||
default_output=format_output( | ||
"", | ||
"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}\n") | ||
) | ||
def status(self) -> ChuangmiPlugStatus: | ||
"""Retrieve properties.""" | ||
properties = AVAILABLE_PROPERTIES[self.model] | ||
|
@@ -123,28 +137,47 @@ def status(self) -> ChuangmiPlugStatus: | |
return ChuangmiPlugStatus( | ||
defaultdict(lambda: None, zip(properties, values))) | ||
|
||
@command( | ||
default_output = format_output("Powering on"), | ||
) | ||
def on(self): | ||
"""Power on.""" | ||
if self.model == MODEL_CHUANGMI_PLUG_V1: | ||
return self.send("set_on", []) | ||
|
||
return self.send("set_power", ["on"]) | ||
|
||
@command( | ||
default_output = format_output("Powering off"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unexpected spaces around keyword / parameter equals |
||
) | ||
def off(self): | ||
"""Power off.""" | ||
if self.model == MODEL_CHUANGMI_PLUG_V1: | ||
return self.send("set_off", []) | ||
|
||
return self.send("set_power", ["off"]) | ||
|
||
@command( | ||
default_output = format_output("Powering USB on"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unexpected spaces around keyword / parameter equals |
||
) | ||
def usb_on(self): | ||
"""Power on.""" | ||
return self.send("set_usb_on", []) | ||
|
||
@command( | ||
default_output = format_output("Powering USB off"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unexpected spaces around keyword / parameter equals |
||
) | ||
def usb_off(self): | ||
"""Power off.""" | ||
return self.send("set_usb_off", []) | ||
|
||
@command( | ||
click.argument("wifi_led", type=bool), | ||
default_output=format_output( | ||
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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# -*- coding: UTF-8 -*- | ||
import logging | ||
import click | ||
from miio.click_common import ( | ||
ExceptionHandlerGroup, DeviceGroupMeta, GlobalContextObject, | ||
json_output, | ||
) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
@click.group(cls=ExceptionHandlerGroup) | ||
@click.option('-d', '--debug', default=False, count=True) | ||
@click.option('-o', '--output', type=click.Choice([ | ||
'default', 'json', 'json_pretty', | ||
]), default='default') | ||
@click.pass_context | ||
def cli(ctx, debug: int, output: str): | ||
if debug: | ||
logging.basicConfig(level=logging.DEBUG) | ||
_LOGGER.info("Debug mode active") | ||
else: | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
if output in ('json', 'json_pretty'): | ||
output_func = json_output(pretty=output == 'json_pretty') | ||
else: | ||
output_func = None | ||
|
||
ctx.obj = GlobalContextObject( | ||
debug=debug, | ||
output=output_func, | ||
) | ||
|
||
|
||
for device_class in DeviceGroupMeta.device_classes: | ||
cli.add_command(device_class.get_device_group()) | ||
|
||
|
||
def create_cli(): | ||
return cli(auto_envvar_prefix="MIIO") | ||
|
||
|
||
if __name__ == '__main__': | ||
create_cli() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unexpected spaces around keyword / parameter equals