mirror of
https://github.com/python-kasa/python-kasa.git
synced 2026-01-01 17:32:44 +00:00
Discover refactoring, enhancements to the cli tool (#71)
* Discover refactoring, enhancements to the cli tool * Discover tries to detect the type of the device from sysinfo response * Discover.discover() returns an IP address keyed dictionary, values are initialized instances of the automatically detected device type. * When no IP is given, autodetect all supported devices and print out their states * When only IP but no type is given, autodetect type and make a call based on that information. * One can define --bulb or --plug to skip the detection. * renamed pyHS100.py -> smartdevice.py * SmartPlugException -> SmartDeviceException in comments * fix mic_type check * make time() return None on failure as we don't know which devices support getting the time and it's used in the cli tool * hw_info: check if key exists before accessing it, add mic_mac and mic_type * Check for mic_mac on mac, based on work by kdschloesser on issue #59 * make hound happy, __init__ on SmartDevice cannot error out so removing 'raises' documentation
This commit is contained in:
@@ -2,7 +2,7 @@ import sys
|
||||
import click
|
||||
import logging
|
||||
from click_datetime import Datetime
|
||||
from pprint import pformat
|
||||
from pprint import pformat as pf
|
||||
|
||||
if sys.version_info < (3, 4):
|
||||
print("To use this script you need python 3.4 or newer! got %s" %
|
||||
@@ -10,7 +10,7 @@ if sys.version_info < (3, 4):
|
||||
sys.exit(1)
|
||||
|
||||
from pyHS100 import (SmartDevice, SmartPlug, SmartBulb,
|
||||
TPLinkSmartHomeProtocol) # noqa: E402
|
||||
TPLinkSmartHomeProtocol, Discover) # noqa: E402
|
||||
|
||||
pass_dev = click.make_pass_decorator(SmartDevice)
|
||||
|
||||
@@ -19,8 +19,9 @@ pass_dev = click.make_pass_decorator(SmartDevice)
|
||||
@click.option('--ip', envvar="PYHS100_IP", required=False)
|
||||
@click.option('--debug/--normal', default=False)
|
||||
@click.option('--bulb', default=False, is_flag=True)
|
||||
@click.option('--plug', default=False, is_flag=True)
|
||||
@click.pass_context
|
||||
def cli(ctx, ip, debug, bulb):
|
||||
def cli(ctx, ip, debug, bulb, plug):
|
||||
"""A cli tool for controlling TP-Link smart home plugs."""
|
||||
if debug:
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
@@ -31,26 +32,46 @@ def cli(ctx, ip, debug, bulb):
|
||||
return
|
||||
|
||||
if ip is None:
|
||||
click.echo("You must specify the IP!")
|
||||
sys.exit(-1)
|
||||
click.echo("No IP given, trying discovery..")
|
||||
ctx.invoke(discover)
|
||||
return
|
||||
|
||||
if bulb:
|
||||
dev = SmartBulb(ip)
|
||||
else:
|
||||
dev = SmartPlug(ip)
|
||||
ctx.obj = dev
|
||||
elif ip is not None:
|
||||
if not bulb and not plug:
|
||||
click.echo("No --bulb nor --plug given, discovering..")
|
||||
devs = ctx.invoke(discover, discover_only=True)
|
||||
for discovered_ip, discovered_dev in devs:
|
||||
if discovered_ip == ip:
|
||||
dev = discovered_dev
|
||||
break
|
||||
elif bulb:
|
||||
dev = SmartBulb(ip)
|
||||
elif plug:
|
||||
dev = SmartPlug(ip)
|
||||
else:
|
||||
click.echo("Unable to detect type, use --bulb or --plug!")
|
||||
return
|
||||
ctx.obj = dev
|
||||
|
||||
if ctx.invoked_subcommand is None:
|
||||
ctx.invoke(state)
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.option('--timeout', default=5, required=False)
|
||||
def discover(timeout):
|
||||
@click.option('--timeout', default=3, required=False)
|
||||
@click.option('--discover-only', default=False)
|
||||
@click.pass_context
|
||||
def discover(ctx, timeout, discover_only):
|
||||
"""Discover devices in the network."""
|
||||
click.echo("Discovering devices for %s seconds" % timeout)
|
||||
for dev in TPLinkSmartHomeProtocol.discover(timeout=timeout):
|
||||
print("Found device: %s" % pformat(dev))
|
||||
found_devs = Discover.discover(timeout=timeout).items()
|
||||
if not discover_only:
|
||||
for ip, dev in found_devs:
|
||||
ctx.obj = dev
|
||||
ctx.invoke(state)
|
||||
print()
|
||||
|
||||
return found_devs
|
||||
|
||||
|
||||
@cli.command()
|
||||
@@ -58,7 +79,7 @@ def discover(timeout):
|
||||
def sysinfo(dev):
|
||||
"""Print out full system information."""
|
||||
click.echo(click.style("== System info ==", bold=True))
|
||||
click.echo(pformat(dev.sys_info))
|
||||
click.echo(pf(dev.sys_info))
|
||||
|
||||
|
||||
@cli.command()
|
||||
@@ -71,6 +92,7 @@ def state(ctx, dev):
|
||||
|
||||
click.echo(click.style("Device state: %s" % "ON" if dev.is_on else "OFF",
|
||||
fg="green" if dev.is_on else "red"))
|
||||
click.echo("IP address: %s" % dev.ip_address)
|
||||
for k, v in dev.state_information.items():
|
||||
click.echo("%s: %s" % (k, v))
|
||||
click.echo(click.style("== Generic information ==", bold=True))
|
||||
|
||||
Reference in New Issue
Block a user