Browse Source

add command-line option `--mqtt-enable-tls`

https://github.com/fphammerle/switchbot-mqtt/issues/76
Fabian Peter Hammerle 2 years ago
parent
commit
4ffe0c4023
3 changed files with 55 additions and 3 deletions
  1. 2 0
      CHANGELOG.md
  2. 21 3
      switchbot_mqtt/_cli.py
  3. 32 0
      tests/test_cli.py

+ 2 - 0
CHANGELOG.md

@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [Unreleased]
+### Added
+- command-line option `--mqtt-enable-tls`
 
 ## [3.0.0] - 2022-02-05
 ### Added

+ 21 - 3
switchbot_mqtt/_cli.py

@@ -27,6 +27,9 @@ import switchbot
 import switchbot_mqtt
 from switchbot_mqtt._actors import _ButtonAutomator, _CurtainMotor
 
+_MQTT_DEFAULT_PORT = 1883
+_MQTT_DEFAULT_TLS_PORT = 8883
+
 _LOGGER = logging.getLogger(__name__)
 
 
@@ -36,7 +39,16 @@ def _main() -> None:
         "compatible with home-assistant.io's MQTT Switch platform"
     )
     argparser.add_argument("--mqtt-host", type=str, required=True)
-    argparser.add_argument("--mqtt-port", type=int, default=1883)
+    argparser.add_argument(
+        "--mqtt-port",
+        type=int,
+        help=f"default {_MQTT_DEFAULT_PORT} ({_MQTT_DEFAULT_TLS_PORT} with --mqtt-enable-tls)",
+    )
+    argparser.add_argument(
+        "--mqtt-enable-tls",
+        action="store_true",
+        help="TLS will be enabled by default in the next major release",
+    )
     argparser.add_argument("--mqtt-username", type=str)
     password_argument_group = argparser.add_mutually_exclusive_group()
     password_argument_group.add_argument("--mqtt-password", type=str)
@@ -92,6 +104,12 @@ def _main() -> None:
         datefmt="%Y-%m-%dT%H:%M:%S%z",
     )
     _LOGGER.debug("args=%r", args)
+    if args.mqtt_port:
+        mqtt_port = args.mqtt_port
+    elif args.mqtt_enable_tls:
+        mqtt_port = _MQTT_DEFAULT_TLS_PORT
+    else:
+        mqtt_port = _MQTT_DEFAULT_PORT
     if args.mqtt_password_path:
         # .read_text() replaces \r\n with \n
         mqtt_password = args.mqtt_password_path.read_bytes().decode()
@@ -107,10 +125,10 @@ def _main() -> None:
         device_passwords = {}
     switchbot_mqtt._run(  # pylint: disable=protected-access; internal
         mqtt_host=args.mqtt_host,
-        mqtt_port=args.mqtt_port,
+        mqtt_port=mqtt_port,
         mqtt_username=args.mqtt_username,
         mqtt_password=mqtt_password,
-        mqtt_disable_tls=True,
+        mqtt_disable_tls=not args.mqtt_enable_tls,
         retry_count=args.retry_count,
         device_passwords=device_passwords,
         fetch_device_info=args.fetch_device_info

+ 32 - 0
tests/test_cli.py

@@ -223,6 +223,38 @@ def test__main_device_password_file(
     )
 
 
+_RUN_DEFAULT_KWARGS: typing.Dict[str, typing.Any] = {
+    "mqtt_port": 1883,
+    "mqtt_username": None,
+    "mqtt_password": None,
+    "mqtt_disable_tls": False,
+    "retry_count": 3,
+    "device_passwords": {},
+    "fetch_device_info": False,
+}
+
+
+def test__main_mqtt_enable_tls() -> None:
+    with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
+        "sys.argv", ["", "--mqtt-host", "mqtt.local", "--mqtt-enable-tls"]
+    ):
+        switchbot_mqtt._cli._main()
+    run_mock.assert_called_once_with(
+        **{**_RUN_DEFAULT_KWARGS, "mqtt_host": "mqtt.local", "mqtt_port": 8883}
+    )
+
+
+def test__main_mqtt_enable_tls_overwrite_port() -> None:
+    with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
+        "sys.argv",
+        ["", "--mqtt-host", "mqtt.local", "--mqtt-port", "1883", "--mqtt-enable-tls"],
+    ):
+        switchbot_mqtt._cli._main()
+    run_mock.assert_called_once_with(
+        **{**_RUN_DEFAULT_KWARGS, "mqtt_host": "mqtt.local", "mqtt_port": 1883}
+    )
+
+
 def test__main_fetch_device_info() -> None:
     with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
         "sys.argv",