_cli.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. allow_abbrev=False,
  9. )
  10. argparser.add_argument(
  11. "-a",
  12. "--address",
  13. type=int,
  14. help="address of emulated remote control ({}-bit unsigned integer)".format(
  15. # pylint: disable=protected-access; internal
  16. intertechno_cc1101._ADDRESS_LENGTH_BITS
  17. ),
  18. required=True,
  19. )
  20. argparser.add_argument(
  21. "-b",
  22. "--button-index",
  23. type=int,
  24. help="index of button on emulated remote control"
  25. " ({}-bit unsigned integer, default: %(default)d)".format(
  26. # pylint: disable=protected-access; internal
  27. intertechno_cc1101._BUTTON_INDEX_LENGTH_BITS
  28. ),
  29. default=0,
  30. )
  31. action_arg_group = argparser.add_mutually_exclusive_group(required=True)
  32. action_arg_group.add_argument("-1", "--turn-on", action="store_true")
  33. action_arg_group.add_argument("-0", "--turn-off", action="store_true")
  34. argparser.add_argument(
  35. "-p",
  36. "--power-setting",
  37. type=int,
  38. default=intertechno_cc1101.DEFAULT_POWER_SETTING,
  39. help='see "Table 39: Optimum PATABLE Settings for Various Output Power Levels […]"'
  40. " in CC1101's docs (default: %(default)d / 0x%(default)X)",
  41. )
  42. argparser.add_argument("-d", "--debug", action="store_true")
  43. args = argparser.parse_args()
  44. logging.basicConfig(
  45. level=logging.DEBUG if args.debug else logging.INFO,
  46. format="%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s"
  47. if args.debug
  48. else "%(message)s",
  49. datefmt="%Y-%m-%dT%H:%M:%S%z",
  50. )
  51. _LOGGER.debug("args=%r", args)
  52. remote_control = intertechno_cc1101.RemoteControl(address=args.address)
  53. if args.turn_on:
  54. remote_control.turn_on(
  55. button_index=args.button_index, power_setting=args.power_setting
  56. )
  57. elif args.turn_off:
  58. remote_control.turn_off(
  59. button_index=args.button_index, power_setting=args.power_setting
  60. )