_cli.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import argparse
  2. import logging
  3. import intertechno_cc1101
  4. _LOGGER = logging.getLogger(__name__)
  5. def _main():
  6. argparser = argparse.ArgumentParser(
  7. description="Control Intertechno Outlets via CC1101 Transceivers"
  8. )
  9. argparser.add_argument(
  10. "-a",
  11. "--address",
  12. type=int,
  13. help="address of emulated remote control ({}-bit unsigned integer)".format(
  14. # pylint: disable=protected-access; internal
  15. intertechno_cc1101._ADDRESS_LENGTH_BITS
  16. ),
  17. required=True,
  18. )
  19. argparser.add_argument(
  20. "-b",
  21. "--button-index",
  22. type=int,
  23. help="index of button on emulated remote control"
  24. " ({}-bit unsigned integer, default: %(default)d)".format(
  25. # pylint: disable=protected-access; internal
  26. intertechno_cc1101._BUTTON_INDEX_LENGTH_BITS
  27. ),
  28. default=0,
  29. )
  30. action_arg_group = argparser.add_mutually_exclusive_group(required=True)
  31. action_arg_group.add_argument("-1", "--turn-on", action="store_true")
  32. action_arg_group.add_argument("-0", "--turn-off", action="store_true")
  33. argparser.add_argument("-d", "--debug", action="store_true")
  34. args = argparser.parse_args()
  35. logging.basicConfig(
  36. level=logging.DEBUG if args.debug else logging.INFO,
  37. format="%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s"
  38. if args.debug
  39. else "%(message)s",
  40. datefmt="%Y-%m-%dT%H:%M:%S%z",
  41. )
  42. _LOGGER.debug("args=%r", args)
  43. remote_control = intertechno_cc1101.RemoteControl(address=args.address)
  44. if args.turn_on:
  45. remote_control.turn_on(button_index=args.button_index)
  46. elif args.turn_off:
  47. remote_control.turn_off(button_index=args.button_index)