コード例 #1
0
ファイル: imagepipe.py プロジェクト: muraig/asyncclick
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
コード例 #2
0
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}")
コード例 #3
0
async def reboot(plug, delay):
    """Reboot the device."""
    click.echo("Rebooting the device..")
    return await plug.reboot(delay)
コード例 #4
0
ファイル: test_commands.py プロジェクト: muraig/asyncclick
 def test(count):
     click.echo("Count: {:d}".format(count))
コード例 #5
0
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
コード例 #6
0
ファイル: test_commands.py プロジェクト: muraig/asyncclick
 def other_cmd(ctx, foo):
     assert ctx.info_name == "other-cmd"
     click.echo(foo)
コード例 #7
0
ファイル: test_commands.py プロジェクト: muraig/asyncclick
 def sync():
     click.echo("in subcommand")
コード例 #8
0
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))
コード例 #9
0
async def time(dev):
    """Get the device time."""
    click.echo(await dev.get_time())
コード例 #10
0
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))
コード例 #11
0
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)
コード例 #12
0
ファイル: imagepipe.py プロジェクト: muraig/asyncclick
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)
コード例 #13
0
ファイル: imagepipe.py プロジェクト: muraig/asyncclick
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)
コード例 #14
0
ファイル: imagepipe.py プロジェクト: muraig/asyncclick
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)
コード例 #15
0
ファイル: test_commands.py プロジェクト: muraig/asyncclick
 def test_callback(args, filename, verbose):
     click.echo(" ".join(args))
     click.echo(filename)
     click.echo(verbose)
コード例 #16
0
ファイル: test_context.py プロジェクト: makkus/asyncclick
 def test(obj):
     click.echo(obj)
コード例 #17
0
ファイル: test_commands.py プロジェクト: muraig/asyncclick
 def sync(ctx):
     click.echo(
         "Debug is {}".format("on" if ctx.obj["DEBUG"] else "off"))
コード例 #18
0
ファイル: test_context.py プロジェクト: makkus/asyncclick
 def test1(foo, ctx):
     click.echo(foo.title)
コード例 #19
0
ファイル: test_commands.py プロジェクト: muraig/asyncclick
 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")
コード例 #20
0
ファイル: test_context.py プロジェクト: makkus/asyncclick
 def test2(ctx, foo):
     click.echo(foo.title)
コード例 #21
0
ファイル: test_commands.py プロジェクト: muraig/asyncclick
 def cli(verbose, args):
     click.echo("Verbosity: {}".format(verbose))
     click.echo("Args: {}".format("|".join(args)))
コード例 #22
0
ファイル: test_context.py プロジェクト: makkus/asyncclick
 def test(foo):
     click.echo(foo.title)
コード例 #23
0
ファイル: test_commands.py プロジェクト: muraig/asyncclick
 def foo(name):
     click.echo(name)
コード例 #24
0
ファイル: test_commands.py プロジェクト: muraig/asyncclick
 def cli(obj):
     click.echo("obj={}".format(obj))
コード例 #25
0
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}")
コード例 #26
0
ファイル: test_commands.py プロジェクト: muraig/asyncclick
 def move():
     click.echo("move")
コード例 #27
0
async def time(dev):
    """Get the device time."""
    res = await dev.get_time()
    click.echo(f"Current time: {res}")
    return res
コード例 #28
0
ファイル: test_commands.py プロジェクト: muraig/asyncclick
 def other_cmd(arg):
     click.echo(arg)
コード例 #29
0
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)
コード例 #30
0
ファイル: cli.py プロジェクト: python-kasa/python-kasa
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)