_cli.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # switchbot-mqtt - MQTT client controlling SwitchBot button & curtain automators,
  2. # compatible with home-assistant.io's MQTT Switch & Cover platform
  3. #
  4. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. import argparse
  19. import asyncio
  20. import json
  21. import logging
  22. import os
  23. import pathlib
  24. import warnings
  25. import switchbot
  26. import switchbot_mqtt
  27. from switchbot_mqtt._actors import _ButtonAutomator, _CurtainMotor
  28. _MQTT_DEFAULT_PORT = 1883
  29. _MQTT_DEFAULT_TLS_PORT = 8883
  30. _LOGGER = logging.getLogger(__name__)
  31. def _main() -> None:
  32. argparser = argparse.ArgumentParser(
  33. description="MQTT client controlling SwitchBot button automators, "
  34. "compatible with home-assistant.io's MQTT Switch platform"
  35. )
  36. argparser.add_argument("--mqtt-host", type=str, required=True)
  37. argparser.add_argument(
  38. "--mqtt-port",
  39. type=int,
  40. help=f"default {_MQTT_DEFAULT_PORT} ({_MQTT_DEFAULT_TLS_PORT} with --mqtt-enable-tls)",
  41. )
  42. mqtt_tls_argument_group = argparser.add_mutually_exclusive_group()
  43. mqtt_tls_argument_group.add_argument(
  44. "--mqtt-enable-tls",
  45. action="store_true",
  46. help="TLS will be enabled by default in the next major release",
  47. )
  48. mqtt_tls_argument_group.add_argument( # for upward compatibility
  49. "--mqtt-disable-tls", action="store_true", help="Currently enabled by default"
  50. )
  51. argparser.add_argument("--mqtt-username", type=str)
  52. password_argument_group = argparser.add_mutually_exclusive_group()
  53. password_argument_group.add_argument("--mqtt-password", type=str)
  54. password_argument_group.add_argument(
  55. "--mqtt-password-file",
  56. type=pathlib.Path,
  57. metavar="PATH",
  58. dest="mqtt_password_path",
  59. help="Stripping trailing newline",
  60. )
  61. argparser.add_argument(
  62. "--mqtt-topic-prefix",
  63. metavar="PREFIX",
  64. default="homeassistant/", # for historic reasons (change to empty string?)
  65. help="Default: %(default)s",
  66. )
  67. argparser.add_argument(
  68. "--device-password-file",
  69. type=pathlib.Path,
  70. metavar="PATH",
  71. dest="device_password_path",
  72. help="Path to json file mapping mac addresses of switchbot devices to passwords, e.g. "
  73. + json.dumps({"11:22:33:44:55:66": "password", "aa:bb:cc:dd:ee:ff": "secret"}),
  74. )
  75. argparser.add_argument(
  76. "--retries",
  77. dest="retry_count",
  78. type=int,
  79. default=switchbot.DEFAULT_RETRY_COUNT,
  80. help="Maximum number of attempts to send a command to a SwitchBot device"
  81. " (default: %(default)d)",
  82. )
  83. argparser.add_argument(
  84. "--fetch-device-info",
  85. action="store_true",
  86. help="Report devices' battery level on topic "
  87. + _ButtonAutomator.get_mqtt_battery_percentage_topic(
  88. prefix="[PREFIX]", mac_address="MAC_ADDRESS"
  89. )
  90. + " or, respectively, "
  91. + _CurtainMotor.get_mqtt_battery_percentage_topic(
  92. prefix="[PREFIX]", mac_address="MAC_ADDRESS"
  93. )
  94. + " after every command. Additionally report curtain motors' position on topic "
  95. + _CurtainMotor.get_mqtt_position_topic(
  96. prefix="[PREFIX]", mac_address="MAC_ADDRESS"
  97. )
  98. + " after executing stop commands."
  99. " When this option is enabled, the mentioned reports may also be requested"
  100. " by sending a MQTT message to the topic "
  101. + _ButtonAutomator.get_mqtt_update_device_info_topic(
  102. prefix="[PREFIX]", mac_address="MAC_ADDRESS"
  103. )
  104. + " or "
  105. + _CurtainMotor.get_mqtt_update_device_info_topic(
  106. prefix="[PREFIX]", mac_address="MAC_ADDRESS"
  107. )
  108. + ". This option can also be enabled by assigning a non-empty value to the"
  109. " environment variable FETCH_DEVICE_INFO."
  110. " [PREFIX] can be set via --mqtt-topic-prefix.",
  111. )
  112. argparser.add_argument("--debug", action="store_true")
  113. args = argparser.parse_args()
  114. # https://github.com/fphammerle/python-cc1101/blob/26d8122661fc4587ecc7c73df55b92d05cf98fe8/cc1101/_cli.py#L51
  115. logging.basicConfig(
  116. level=logging.DEBUG if args.debug else logging.INFO,
  117. format="%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s"
  118. if args.debug
  119. else "%(message)s",
  120. datefmt="%Y-%m-%dT%H:%M:%S%z",
  121. )
  122. _LOGGER.debug("args=%r", args)
  123. if args.mqtt_port:
  124. mqtt_port = args.mqtt_port
  125. elif args.mqtt_enable_tls:
  126. mqtt_port = _MQTT_DEFAULT_TLS_PORT
  127. else:
  128. mqtt_port = _MQTT_DEFAULT_PORT
  129. if not args.mqtt_enable_tls and not args.mqtt_disable_tls:
  130. warnings.warn(
  131. "In switchbot-mqtt's next major release, TLS will be enabled by default"
  132. " (--mqtt-enable-tls)."
  133. " Please add --mqtt-disable-tls to your command for upward compatibility.",
  134. UserWarning, # DeprecationWarning ignored by default
  135. )
  136. if args.mqtt_password_path:
  137. # .read_text() replaces \r\n with \n
  138. mqtt_password = args.mqtt_password_path.read_bytes().decode()
  139. if mqtt_password.endswith("\r\n"):
  140. mqtt_password = mqtt_password[:-2]
  141. elif mqtt_password.endswith("\n"):
  142. mqtt_password = mqtt_password[:-1]
  143. else:
  144. mqtt_password = args.mqtt_password
  145. if ( # pylint: disable=consider-ternary-expression; bulky with black's wraps
  146. args.device_password_path
  147. ):
  148. device_passwords = json.loads(args.device_password_path.read_text())
  149. else:
  150. device_passwords = {}
  151. asyncio.run(
  152. switchbot_mqtt._run( # pylint: disable=protected-access; internal
  153. mqtt_host=args.mqtt_host,
  154. mqtt_port=mqtt_port,
  155. mqtt_disable_tls=not args.mqtt_enable_tls,
  156. mqtt_username=args.mqtt_username,
  157. mqtt_password=mqtt_password,
  158. mqtt_topic_prefix=args.mqtt_topic_prefix,
  159. retry_count=args.retry_count,
  160. device_passwords=device_passwords,
  161. fetch_device_info=args.fetch_device_info
  162. # > In formal language theory, the empty string, [...],
  163. # > is the unique string of length zero.
  164. # https://en.wikipedia.org/wiki/Empty_string
  165. or bool(os.environ.get("FETCH_DEVICE_INFO")),
  166. )
  167. )