Beispiel #1
0
def go(opts):
    config.exists(fail=True)
    rootcfg = config.load()
    cfg = config.load_user(rootcfg["user"])
    cfg.update(rootcfg)
    interface = cfg["interface"]

    wifi_up(interface)
    print("Looking for Meross device...", end="")

    try:
        attempts = 0
        while attempts < 300:
            print(".", end="")
            sys.stdout.flush()
            content = iwlist.scan(interface)
            cells = iwlist.parse(content)
            for cell in cells:
                if cell["essid"].startswith("Meross_"):
                    print("\nFound {}".format(cell["essid"]))

                    associate(cell, interface)
                    add_ip(interface)

                    # Get initial device data
                    dev0 = get_device_data()
                    device = dict()
                    device[opts.name] = dev0["payload"]["all"]["system"]

                    # Setup server details
                    set_server_details(cfg, opts)

                    # Setup wifi details
                    set_wifi_details(cfg)

                    # Save device config
                    config.add_device(device, opts, cfg["user"])
                    typ = device[opts.name]["hardware"]["type"]
                    print("Added {} ({})".format(opts.name, typ))

                    # We're done
                    attempts = 999
                    break

            attempts += 1
            time.sleep(1)

    except subprocess.CalledProcessError:
        pass

    finally:
        # Remove the IP address from the interface
        del_ip(interface)
        wifi_down(interface)
Beispiel #2
0
def devices(prefix, parsed_args, **kwargs):
    config.exists(fail=True)
    cfg = config.load()
    result = ["all"]
    result.extend(config.list_devices(cfg))
    if not prefix.startswith("~"):
        pre = ""
        pfx = prefix
    else:
        pre = "~"
        pfx = prefix.lstrip("~")

    return [pre + name for name in result if name.startswith(pfx)]
Beispiel #3
0
def go(opts):
    config.exists(fail=False)
    cfg = config.load()
    if os.getuid() == 0:
        # root
        opts.ssid = mangle(opts.ssid)
        opts.password = mangle(opts.password)
        config.root_update(cfg, opts)
        config.save(cfg)
    else:
        # Normal user mode
        config.update(cfg, opts)
        config.save(cfg)
Beispiel #4
0
def just_devices(prefix, parsed_args, **kwargs):
    config.exists(fail=True)
    cfg = config.load()
    result = []
    result.extend(config.list_devices(cfg))
    return [name for name in result if name.startswith(prefix)]