def display_cmd(images): """Opens all images in an image viewer.""" for image in images: click.echo("Displaying '{}'".format(image.filename)) image.show() yield image
async def emeter(dev: SmartDevice, year, month, erase): """Query emeter for historical consumption. Daily and monthly data provided in CSV format. """ click.echo(click.style("== Emeter ==", bold=True)) if not dev.has_emeter: click.echo("Device has no emeter") return if erase: click.echo("Erasing emeter statistics..") click.echo(await dev.erase_emeter_stats()) return if year: click.echo(f"== For year {year.year} ==") click.echo("Month, usage (kWh)") usage_data = await dev.get_emeter_monthly(year.year) elif month: click.echo(f"== For month {month.month} of {month.year} ==") click.echo("Day, usage (kWh)") usage_data = await dev.get_emeter_daily(year=month.year, month=month.month) else: # Call with no argument outputs summary data and returns usage_data = {} emeter_status = dev.emeter_realtime click.echo("Current: %s A" % emeter_status["current"]) click.echo("Voltage: %s V" % emeter_status["voltage"]) click.echo("Power: %s W" % emeter_status["power"]) click.echo("Total consumption: %s kWh" % emeter_status["total"]) click.echo("Today: %s kWh" % dev.emeter_today) click.echo("This month: %s kWh" % dev.emeter_this_month) return # output any detailed usage data for index, usage in usage_data.items(): click.echo(f"{index}, {usage}")
async def reboot(plug, delay): """Reboot the device.""" click.echo("Rebooting the device..") return await plug.reboot(delay)
def test(count): click.echo("Count: {:d}".format(count))
async def sysinfo(dev): """Print out full system information.""" click.echo(click.style("== System info ==", bold=True)) click.echo(pf(dev.sys_info)) return dev.sys_info
def other_cmd(ctx, foo): assert ctx.info_name == "other-cmd" click.echo(foo)
def sync(): click.echo("in subcommand")
async def emeter(dev, year, month, erase): """Query emeter for historical consumption.""" click.echo(click.style("== Emeter ==", bold=True)) await dev.update() if not dev.has_emeter: click.echo("Device has no emeter") return if erase: click.echo("Erasing emeter statistics..") click.echo(await dev.erase_emeter_stats()) return if year: click.echo(f"== For year {year.year} ==") emeter_status = await dev.get_emeter_monthly(year.year) elif month: click.echo(f"== For month {month.month} of {month.year} ==") emeter_status = await dev.get_emeter_daily(year=month.year, month=month.month) else: emeter_status = await dev.get_emeter_realtime() click.echo("== Current State ==") if isinstance(emeter_status, list): for plug in emeter_status: index = emeter_status.index(plug) + 1 click.echo(f"Plug {index}: {plug}") else: click.echo(str(emeter_status))
async def time(dev): """Get the device time.""" click.echo(await dev.get_time())
async def sysinfo(dev): """Print out full system information.""" await dev.update() click.echo(click.style("== System info ==", bold=True)) click.echo(pf(dev.sys_info))
async def state(ctx, dev: SmartDevice): """Print out device state and versions.""" await dev.update() click.echo(click.style(f"== {dev.alias} - {dev.model} ==", bold=True)) click.echo( click.style( "Device state: {}".format("ON" if dev.is_on else "OFF"), fg="green" if dev.is_on else "red", ) ) if dev.is_strip: for plug in dev.plugs: # type: ignore is_on = plug.is_on alias = plug.alias click.echo( click.style( " * Socket '{}' state: {} on_since: {}".format( alias, ("ON" if is_on else "OFF"), plug.on_since ), fg="green" if is_on else "red", ) ) click.echo(f"Host/IP: {dev.host}") for k, v in dev.state_information.items(): click.echo(f"{k}: {v}") click.echo(click.style("== Generic information ==", bold=True)) click.echo(f"Time: {await dev.get_time()}") click.echo(f"Hardware: {dev.hw_info['hw_ver']}") click.echo(f"Software: {dev.hw_info['sw_ver']}") click.echo(f"MAC (rssi): {dev.mac} ({dev.rssi})") click.echo(f"Location: {dev.location}") await ctx.invoke(emeter)
def sharpen_cmd(images, factor): """Sharpens an image.""" for image in images: click.echo("Sharpen '{}' by {}".format(image.filename, factor)) enhancer = ImageEnhance.Sharpness(image) yield copy_filename(enhancer.enhance(max(1.0, factor)), image)
def emboss_cmd(images): """Embosses an image.""" for image in images: click.echo("Embossing '{}'".format(image.filename)) yield copy_filename(image.filter(ImageFilter.EMBOSS), image)
def blur_cmd(images, radius): """Applies gaussian blur.""" blur = ImageFilter.GaussianBlur(radius) for image in images: click.echo("Blurring '{}' by {}px".format(image.filename, radius)) yield copy_filename(image.filter(blur), image)
def test_callback(args, filename, verbose): click.echo(" ".join(args)) click.echo(filename) click.echo(verbose)
def test(obj): click.echo(obj)
def sync(ctx): click.echo( "Debug is {}".format("on" if ctx.obj["DEBUG"] else "off"))
def test1(foo, ctx): click.echo(foo.title)
async def cli(ctx): if ctx.invoked_subcommand is None: click.echo("no subcommand, use default") await ctx.invoke(sync) else: click.echo("invoke subcommand")
def test2(ctx, foo): click.echo(foo.title)
def cli(verbose, args): click.echo("Verbosity: {}".format(verbose)) click.echo("Args: {}".format("|".join(args)))
def test(foo): click.echo(foo.title)
def foo(name): click.echo(name)
def cli(obj): click.echo("obj={}".format(obj))
async def state(ctx, dev: SmartDevice): """Print out device state and versions.""" click.echo(click.style(f"== {dev.alias} - {dev.model} ==", bold=True)) click.echo(f"\tHost: {dev.host}") click.echo( click.style( "\tDevice state: {}\n".format("ON" if dev.is_on else "OFF"), fg="green" if dev.is_on else "red", )) if dev.is_strip: click.echo(click.style("\t== Plugs ==", bold=True)) for plug in dev.children: # type: ignore is_on = plug.is_on alias = plug.alias click.echo( click.style( "\t* Socket '{}' state: {} on_since: {}".format( alias, ("ON" if is_on else "OFF"), plug.on_since), fg="green" if is_on else "red", )) click.echo() click.echo(click.style("\t== Generic information ==", bold=True)) click.echo(f"\tTime: {await dev.get_time()}") click.echo(f"\tHardware: {dev.hw_info['hw_ver']}") click.echo(f"\tSoftware: {dev.hw_info['sw_ver']}") click.echo(f"\tMAC (rssi): {dev.mac} ({dev.rssi})") click.echo(f"\tLocation: {dev.location}") click.echo(click.style("\n\t== Device specific information ==", bold=True)) for k, v in dev.state_information.items(): click.echo(f"\t{k}: {v}") click.echo() if dev.has_emeter: click.echo(click.style("\n\t== Current State ==", bold=True)) emeter_status = dev.emeter_realtime click.echo(f"\t{emeter_status}")
def move(): click.echo("move")
async def time(dev): """Get the device time.""" res = await dev.get_time() click.echo(f"Current time: {res}") return res
def other_cmd(arg): click.echo(arg)
async def cli(ctx, host, alias, target, debug, bulb, plug, lightstrip, strip): """A tool for controlling TP-Link smart home devices.""" # noqa if debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) if ctx.invoked_subcommand == "discover": return if alias is not None and host is None: click.echo(f"Alias is given, using discovery to find host {alias}") host = await find_host_from_alias(alias=alias, target=target) if host: click.echo(f"Found hostname is {host}") else: click.echo(f"No device with name {alias} found") return if host is None: click.echo("No host name given, trying discovery..") await ctx.invoke(discover) return else: if not bulb and not plug and not strip and not lightstrip: click.echo("No --strip nor --bulb nor --plug given, discovering..") dev = await Discover.discover_single(host) elif bulb: dev = SmartBulb(host) elif plug: dev = SmartPlug(host) elif strip: dev = SmartStrip(host) elif lightstrip: dev = SmartLightStrip(host) else: click.echo( "Unable to detect type, use --strip or --bulb or --plug!") return await dev.update() ctx.obj = dev if ctx.invoked_subcommand is None: await ctx.invoke(state)
async def cli(ctx, host, alias, target, debug, bulb, plug, lightstrip, strip, type): """A tool for controlling TP-Link smart home devices.""" # noqa if debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) if ctx.invoked_subcommand == "discover": return if alias is not None and host is None: click.echo(f"Alias is given, using discovery to find host {alias}") host = await find_host_from_alias(alias=alias, target=target) if host: click.echo(f"Found hostname is {host}") else: click.echo(f"No device with name {alias} found") return if host is None: click.echo("No host name given, trying discovery..") await ctx.invoke(discover) return if bulb or plug or strip or lightstrip: click.echo( "Using --bulb, --plug, --strip, and --lightstrip is deprecated. Use --type instead to define the type" ) if bulb: type = "bulb" elif plug: type = "plug" elif strip: type = "strip" elif lightstrip: type = "lightstrip" if type is not None: dev = TYPE_TO_CLASS[type](host) else: click.echo("No --type defined, discovering..") dev = await Discover.discover_single(host) await dev.update() ctx.obj = dev if ctx.invoked_subcommand is None: await ctx.invoke(state)