class DuofernCLI(Cmd): def __init__(self, serial_port=None, system_code=None, config_file=None, *args, **kwargs): super().__init__(*args, **kwargs) self.stick = DuofernStickThreaded(serial_port=args.device, system_code=args.code, config_file_json=args.configfile) self.stick._initialize() self.stick.start() self.prompt = "duofern> " def emptyline(self): pass def do_pair(self, args): """ Usage: pair <TIMEOUT> Start pairing mode. Pass a timeout in seconds as <TIMEOUT>. Will return after the timeout if no devices start pairing within the given timeout. Example: duofern> pair 10 """ timeout = 10 if len(args) != 0: try: timeout = int(args[0]) except: print( "Please use an integer number to indicate TIMEOUT in seconds" ) print("Starting pairing mode... waiting {} seconds".format( int(timeout))) self.stick.pair(timeout=timeout) time.sleep(args.pairtime + 0.5) self.stick.sync_devices() print("Pairing done, Config file updated.") def do_unpair(self, args): """ Usage: unpair <TIMEOUT> Start pairing mode. Pass a timeout in seconds as <TIMEOUT>. Will return after the timeout if no devices start pairing within the given timeout. Example: duofern> unpair 10 """ timeout = 10 if len(args) != 0: try: timeout = int(args[0]) except: print( "Please use an integer number to indicate TIMEOUT in seconds" ) print("Starting pairing mode... waiting {} seconds".format( int(timeout))) self.stick.unpair(timeout=timeout) time.sleep(args.pairtime + 0.5) self.stick.sync_devices() print("Pairing done, Config file updated.") def do_remote(self, args): code = args[0][0:6] timeout = int(args[1]) self.stick.remote(code, timeout) time.sleep(args.pairtime + 0.5) self.stick.sync_devices() print("Pairing done, Config file updated.") @splitargs @ids_for_names def do_up(self, blinds): """ Usage: up <SHUTTER> [<SHUTTER> <SHUTTER>] Lift one or several shutters. Accepts a list of shutter names sepatated by space. Example: duofern> up Livingroom duofern> up Livingroom Kitchen """ for blind_id in blinds: print("lifting {}".format(blinds[blind_id])) self.stick.command(blind_id, "up") @splitargs @ids_for_names def do_down(self, blinds): """ Usage: up <SHUTTER> [<SHUTTER> <SHUTTER>...] Lower one or several shutters. Accepts a list of shutter names sepatated by space. Example: duofern> up Livingroom duofern> up Livingroom Kitchen """ for blind_id in blinds: print("lowering {}".format(blinds[blind_id])) self.stick.command(blind_id, "down") @splitargs def do_rename(self, args): """ Usage: rename <NAME> <NEW_NAME> Rename an actor. Write changes to config file when done. Example: duofern> rename 13f897 kitchen_west """ id = [ device['id'] for device in self.stick.config['devices'] if device['name'] == args[0] ] if len(id) == 0: print("Please enter a valid device name for renaming.") self.stick.set_name(id[0], args[1]) print("Set name for {} to {}".format(id[0], args[0])) def refresh(self, args): """ Usage: refresh Refresh config file with current changes. example: duofern> refresh """ self.stick.sync_devices() @splitargs @ids_for_names def do_on(self, blinds): """ Usage: off <SWITCH1> [<SWITCH2> <SWITCH3>] Switch on one or several switch actors. Accepts a list of actor names. Example: duofern> off Livingroom duofern> off Livingroom Kitchen """ for blind_id in blinds: print("lifting {}".format(blinds[blind_id])) self.stick.command(blind_id, "up") @splitargs @ids_for_names def do_off(self, blinds): """ Usage: off <SWITCH1> [<SWITCH2> <SWITCH3>] Switch off one or several switch actors. Accepts a list of actor names. Example: duofern> off Livingroom duofern> off Livingroom Kitchen """ for blind_id in blinds: print("lifting {}".format(blinds[blind_id])) self.stick.command(blind_id, "up")
assert len(args.code) == 4, "System code must be a 4 digit hex code" try: bytearray.fromhex(args.code) except: print("System code must be a 4 digit hex code") exit(1) stick = DuofernStickThreaded(serial_port=args.device, system_code=args.code, config_file_json=args.configfile) if args.set_name is not None: assert len(args.set_name[0]) == 6 and re.match("^[0-9a-f]+$", args.set_name[0], re.IGNORECASE), \ "id for renaming must be a valid 6 digit hex ID not {}".format(args.set_name[0]) stick.set_name(args.set_name[0], args.set_name[1]) stick.sync_devices() print("\nThe following devices are configured:\n") print("\n".join([ "id: {:6} name: {}".format(device['id'], device['name']) for device in stick.config['devices'] ])) print("\n") if args.pair: print("entering pairing mode") stick._initialize() stick.start() stick.pair(timeout=args.pairtime) time.sleep(args.pairtime + 0.5) stick.sync_devices()