import sys
from argparse import ArgumentParser

from .environment import Environment
from ouimeaux.config import get_cache


def wemo():
    parser = ArgumentParser()

    parser.add_argument("--timeout", type=int, default=5,
                        help="Time in seconds to allow for discovery")
    parser.add_argument("--bind", default=None,
                        help="ip:port to which to bind the response server."
                             " Default is localhost:54321")
    parser.add_argument("--no-cache", dest="use_cache", default=None,
                        action="store_false",
                        help="Disable the device cache")
    subparsers = parser.add_subparsers()

    stateparser = subparsers.add_parser("switch",
                                        help="Turn a WeMo Switch on or off")
    stateparser.add_argument("device", help="Name or alias of the device")
    stateparser.add_argument("state", help="'on' or 'off")

    subparsers.add_parser("list",
                          help="List all devices found in the environment")

    args = parser.parse_args()

    ls = state = None

    if getattr(args, 'device', None):
        if args.state.lower() in ("on", "1", "true"):
            state = "on"
        elif args.state.lower() in ("off", "0", "false"):
            state = "off"
        elif args.state.lower() == "toggle":
            state = "toggle"
    else:
        ls = True

    def on_switch(switch):
        if state:
            if switch.name == args.device:
                if state == "toggle":
                    found_state = switch.get_state(force_update=True)
                    switch.set_state(not found_state)
                else:
                    getattr(switch, state)()
                sys.exit(0)
        elif ls:
            print "Switch: ", switch.name

    def on_motion(motion):
        if ls:
            print "Motion: ", motion.name

    try:
        env = Environment(on_switch, on_motion, with_subscribers=False,
                          bind=args.bind, with_cache=args.use_cache)
        if getattr(args, 'device', None):
            args.device = env._config.aliases.get(args.device, args.device)
        env.start()
        with get_cache() as c:
            if c.empty:
                args.use_cache = False
        if (args.use_cache is not None and not args.use_cache) or (
                    env._config.cache is not None and not env._config.cache):
            env.discover(args.timeout)
    except KeyboardInterrupt:
        sys.exit(0)

    if not ls:
        print "No device found with that name."
        sys.exit(1)


@@ -4,47 +4,37 @@
 ort 
-sys%0Afrom argparse 
+os%0Aimport sys%0A
 import 
-ArgumentP
+argp
 arse
-r
 %0A%0Afr
@@ -105,16 +105,25 @@
 et_cache
+, in_home
 %0A%0A%0Adef w
@@ -142,16 +142,25 @@
 arser = 
+argparse.
 Argument
@@ -694,24 +694,298 @@
 bparsers()%0A%0A
+    clearparser = subparsers.add_parser(%0A        %22clear%22, help=%22Clear the device cache%22)%0A    # Add a hidden argument so we can detect when we're in this command%0A    clearparser.add_argument('--clearcache', default=%221%22, %0A                             help=argparse.SUPPRESS)%0A%0A
     statepar
@@ -1400,16 +1400,360 @@
 = None%0A%0A
+    if getattr(args, 'clearcache', None):%0A        for fname in 'cache', 'cache.db':%0A            filename = in_home('.wemo', fname)%0A            try:%0A                os.remove(filename)%0A            except OSError:%0A                # File didn't exist; cache must be clear%0A                pass%0A        print %22Device cache cleared.%22%0A        return%0A%0A
     if g

Add command to clear device cache