use the "sync" attribute to call methods in the CLI

This commit is contained in:
Bas Nijholt 2019-11-11 19:53:17 +01:00
parent e6faf9dd05
commit 8a7d955eb7

View File

@ -145,7 +145,7 @@ def find_host_from_alias(alias, target="255.255.255.255", timeout=1, attempts=3)
click.echo("Attempt %s of %s" % (attempt, attempts))
found_devs = Discover.discover(target=target, timeout=timeout).items()
for ip, dev in found_devs:
if dev.alias.lower() == alias.lower():
if dev.sync.get_alias().lower() == alias.lower():
host = dev.host
return host
return None
@ -156,7 +156,7 @@ def find_host_from_alias(alias, target="255.255.255.255", timeout=1, attempts=3)
def sysinfo(dev):
"""Print out full system information."""
click.echo(click.style("== System info ==", bold=True))
click.echo(pf(dev.sys_info))
click.echo(pf(dev.sync.get_sys_info()))
@cli.command()
@ -164,17 +164,21 @@ def sysinfo(dev):
@click.pass_context
def state(ctx, dev):
"""Print out device state and versions."""
click.echo(click.style("== %s - %s ==" % (dev.alias, dev.model), bold=True))
click.echo(
click.style(
"== %s - %s ==" % (dev.sync.get_alias(), dev.sync.get_model()), bold=True
)
)
click.echo(
click.style(
"Device state: %s" % ("ON" if dev.is_on else "OFF"),
fg="green" if dev.is_on else "red",
"Device state: %s" % ("ON" if dev.sync.is_on() else "OFF"),
fg="green" if dev.sync.is_on() else "red",
)
)
if dev.num_children > 0:
is_on = dev.get_is_on()
aliases = dev.get_alias()
is_on = dev.sync.get_is_on()
aliases = dev.sync.get_alias()
for child in range(dev.num_children):
click.echo(
click.style(
@ -185,14 +189,14 @@ def state(ctx, dev):
)
click.echo("Host/IP: %s" % dev.host)
for k, v in dev.state_information.items():
for k, v in dev.sync.get_state_information().items():
click.echo("%s: %s" % (k, v))
click.echo(click.style("== Generic information ==", bold=True))
click.echo("Time: %s" % dev.time)
click.echo("Hardware: %s" % dev.hw_info["hw_ver"])
click.echo("Software: %s" % dev.hw_info["sw_ver"])
click.echo("MAC (rssi): %s (%s)" % (dev.mac, dev.rssi))
click.echo("Location: %s" % dev.location)
click.echo("Time: %s" % dev.sync.get_time())
click.echo("Hardware: %s" % dev.sync.get_hw_info()["hw_ver"])
click.echo("Software: %s" % dev.sync.get_hw_info()["sw_ver"])
click.echo("MAC (rssi): %s (%s)" % (dev.sync.get_mac(), dev.sync.get_rssi()))
click.echo("Location: %s" % dev.sync.get_location())
ctx.invoke(emeter)
@ -204,9 +208,9 @@ def alias(dev, new_alias):
"""Get or set the device alias."""
if new_alias is not None:
click.echo("Setting alias to %s" % new_alias)
dev.alias = new_alias
dev.sync.set_alias(new_alias)
click.echo("Alias: %s" % dev.alias)
click.echo("Alias: %s" % dev.sync.get_alias())
@cli.command()
@ -220,7 +224,7 @@ def raw_command(dev: SmartDevice, module, command, parameters):
if parameters is not None:
parameters = ast.literal_eval(parameters)
res = asyncio.run(dev._query_helper(module, command, parameters))
res = dev.sync._query_helper(module, command, parameters)
click.echo(res)
@ -232,23 +236,23 @@ def raw_command(dev: SmartDevice, module, command, parameters):
def emeter(dev, year, month, erase):
"""Query emeter for historical consumption."""
click.echo(click.style("== Emeter ==", bold=True))
if not dev.has_emeter:
if not dev.sync.get_has_emeter():
click.echo("Device has no emeter")
return
if erase:
click.echo("Erasing emeter statistics..")
dev.erase_emeter_stats()
dev.sync.erase_emeter_stats()
return
if year:
click.echo("== For year %s ==" % year.year)
emeter_status = dev.get_emeter_monthly(year.year)
emeter_status = dev.sync.get_emeter_monthly(year.year)
elif month:
click.echo("== For month %s of %s ==" % (month.month, month.year))
emeter_status = dev.get_emeter_daily(year=month.year, month=month.month)
emeter_status = dev.sync.get_emeter_daily(year=month.year, month=month.month)
else:
emeter_status = dev.get_emeter_realtime()
emeter_status = dev.sync.get_emeter_realtime()
click.echo("== Current State ==")
if isinstance(emeter_status, list):
@ -263,14 +267,14 @@ def emeter(dev, year, month, erase):
@pass_dev
def brightness(dev, brightness):
"""Get or set brightness."""
if not dev.is_dimmable:
if not dev.sync.is_dimmable():
click.echo("This device does not support brightness.")
return
if brightness is None:
click.echo("Brightness: %s" % dev.brightness)
click.echo("Brightness: %s" % dev.sync.get_brightness())
else:
click.echo("Setting brightness to %s" % brightness)
dev.brightness = brightness
dev.sync.set_brightness(brightness)
@cli.command()
@ -281,17 +285,18 @@ def brightness(dev, brightness):
def temperature(dev: SmartBulb, temperature):
"""Get or set color temperature."""
if temperature is None:
click.echo("Color temperature: %s" % dev.color_temp)
if dev.valid_temperature_range != (0, 0):
click.echo("(min: %s, max: %s)" % dev.valid_temperature_range)
click.echo("Color temperature: %s" % dev.sync.get_color_temp())
valid_temperature_range = dev.sync.get_valid_temperature_range()
if valid_temperature_range != (0, 0):
click.echo("(min: %s, max: %s)" % valid_temperature_range)
else:
click.echo(
"Temperature range unknown, please open a github issue"
" or a pull request for model '%s'" % dev.model
" or a pull request for model '%s'" % dev.sync.get_model()
)
else:
click.echo("Setting color temperature to %s" % temperature)
dev.set_color_temp(temperature)
dev.sync.set_color_temp(temperature)
@cli.command()
@ -303,12 +308,12 @@ def temperature(dev: SmartBulb, temperature):
def hsv(dev, ctx, h, s, v):
"""Get or set color in HSV. (Bulb only)"""
if h is None or s is None or v is None:
click.echo("Current HSV: %s %s %s" % dev.hsv)
click.echo("Current HSV: %s %s %s" % dev.sync.get_hsv())
elif s is None or v is None:
raise click.BadArgumentUsage("Setting a color requires 3 values.", ctx)
else:
click.echo("Setting HSV: %s %s %s" % (h, s, v))
dev.set_hsv(h, s, v)
dev.sync.set_hsv(h, s, v)
@cli.command()
@ -318,16 +323,16 @@ def led(dev, state):
"""Get or set (Plug's) led state."""
if state is not None:
click.echo("Turning led to %s" % state)
dev.led = state
dev.sync.set_led(state)
else:
click.echo("LED state: %s" % dev.led)
click.echo("LED state: %s" % dev.sync.get_led())
@cli.command()
@pass_dev
def time(dev):
"""Get the device time."""
click.echo(dev.time)
click.echo(dev.sync.get_time())
@cli.command()