Browse Source

add command-line option --mqtt-disable-tls (for upward compatibility; enabled by default)

Fabian Peter Hammerle 2 years ago
parent
commit
3431242e67
4 changed files with 49 additions and 3 deletions
  1. 4 0
      CHANGELOG.md
  2. 3 1
      README.md
  3. 13 1
      switchbot_mqtt/_cli.py
  4. 29 1
      tests/test_cli.py

+ 4 - 0
CHANGELOG.md

@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ## [Unreleased]
 ### Added
 - command-line option `--mqtt-enable-tls`
+- command-line option `--mqtt-disable-tls` (enabled by default)
+
+### Deprecated
+- invocation without `--mqtt-enable-tls` and `--mqtt-disable-tls`
 
 ## [3.0.0] - 2022-02-05
 ### Added

+ 3 - 1
README.md

@@ -22,7 +22,9 @@ $ pip3 install --user --upgrade switchbot-mqtt
 ## Usage
 
 ```sh
-$ switchbot-mqtt --mqtt-host HOSTNAME_OR_IP_ADDRESS
+$ switchbot-mqtt --mqtt-host HOSTNAME_OR_IP_ADDRESS --mqtt-enable-tls
+# or
+$ switchbot-mqtt --mqtt-host HOSTNAME_OR_IP_ADDRESS --mqtt-disable-tls
 ```
 
 Use `sudo hcitool lescan`

+ 13 - 1
switchbot_mqtt/_cli.py

@@ -21,6 +21,7 @@ import json
 import logging
 import os
 import pathlib
+import warnings
 
 import switchbot
 
@@ -44,11 +45,15 @@ def _main() -> None:
         type=int,
         help=f"default {_MQTT_DEFAULT_PORT} ({_MQTT_DEFAULT_TLS_PORT} with --mqtt-enable-tls)",
     )
-    argparser.add_argument(
+    mqtt_tls_argument_group = argparser.add_mutually_exclusive_group()
+    mqtt_tls_argument_group.add_argument(
         "--mqtt-enable-tls",
         action="store_true",
         help="TLS will be enabled by default in the next major release",
     )
+    mqtt_tls_argument_group.add_argument(  # for upward compatibility
+        "--mqtt-disable-tls", action="store_true", help="Currently enabled by default"
+    )
     argparser.add_argument("--mqtt-username", type=str)
     password_argument_group = argparser.add_mutually_exclusive_group()
     password_argument_group.add_argument("--mqtt-password", type=str)
@@ -110,6 +115,13 @@ def _main() -> None:
         mqtt_port = _MQTT_DEFAULT_TLS_PORT
     else:
         mqtt_port = _MQTT_DEFAULT_PORT
+    if not args.mqtt_enable_tls and not args.mqtt_disable_tls:
+        warnings.warn(
+            "In switchbot-mqtt's next major release, TLS will be enabled by default"
+            " (--mqtt-enable-tls)."
+            " Please add --mqtt-disable-tls to your command for upward compatibility.",
+            UserWarning,  # DeprecationWarning ignored by default
+        )
     if args.mqtt_password_path:
         # .read_text() replaces \r\n with \n
         mqtt_password = args.mqtt_password_path.read_bytes().decode()

+ 29 - 1
tests/test_cli.py

@@ -103,7 +103,7 @@ def test__main(
 ) -> None:
     with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
         "sys.argv", argv
-    ):
+    ), pytest.warns(UserWarning, match=r"Please add --mqtt-disable-tls\b"):
         switchbot_mqtt._cli._main()
     run_mock.assert_called_once_with(
         mqtt_host=expected_mqtt_host,
@@ -234,6 +234,21 @@ _RUN_DEFAULT_KWARGS: typing.Dict[str, typing.Any] = {
 }
 
 
+def test__main_mqtt_disable_tls_implicit() -> None:
+    with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
+        "sys.argv", ["", "--mqtt-host", "mqtt.local"]
+    ), pytest.warns(UserWarning, match=r"Please add --mqtt-disable-tls\b"):
+        switchbot_mqtt._cli._main()
+    run_mock.assert_called_once_with(
+        **{
+            **_RUN_DEFAULT_KWARGS,
+            "mqtt_host": "mqtt.local",
+            "mqtt_disable_tls": True,
+            "mqtt_port": 1883,
+        }
+    )
+
+
 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"]
@@ -255,6 +270,19 @@ def test__main_mqtt_enable_tls_overwrite_port() -> None:
     )
 
 
+def test__main_mqtt_tls_collision(capsys: _pytest.capture.CaptureFixture) -> None:
+    with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
+        "sys.argv",
+        ["", "--mqtt-host", "mqtt.local", "--mqtt-enable-tls", "--mqtt-disable-tls"],
+    ), pytest.raises(SystemExit):
+        switchbot_mqtt._cli._main()
+    run_mock.assert_not_called()
+    assert (
+        "error: argument --mqtt-disable-tls: not allowed with argument --mqtt-enable-tls\n"
+        in capsys.readouterr()[1]
+    )
+
+
 def test__main_fetch_device_info() -> None:
     with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
         "sys.argv",