Browse Source

indicate in log message whether TLS is enabled

Fabian Peter Hammerle 2 years ago
parent
commit
965ca2646e
2 changed files with 17 additions and 4 deletions
  1. 6 1
      switchbot_mqtt/__init__.py
  2. 11 3
      tests/test_mqtt.py

+ 6 - 1
switchbot_mqtt/__init__.py

@@ -68,7 +68,12 @@ def _run(
         )
     )
     mqtt_client.on_connect = _mqtt_on_connect
-    _LOGGER.info("connecting to MQTT broker %s:%d", mqtt_host, mqtt_port)
+    _LOGGER.info(
+        "connecting to MQTT broker %s:%d (TLS %s)",
+        mqtt_host,
+        mqtt_port,
+        "disabled" if mqtt_disable_tls else "enabled",
+    )
     if not mqtt_disable_tls:
         mqtt_client.tls_set(ca_certs=None)  # enable tls trusting default system certs
     if mqtt_username:

+ 11 - 3
tests/test_mqtt.py

@@ -106,7 +106,7 @@ def test__run(
         (
             "switchbot_mqtt",
             logging.INFO,
-            f"connecting to MQTT broker {mqtt_host}:{mqtt_port}",
+            f"connecting to MQTT broker {mqtt_host}:{mqtt_port} (TLS enabled)",
         ),
         (
             "switchbot_mqtt",
@@ -128,8 +128,12 @@ def test__run(
 
 
 @pytest.mark.parametrize("mqtt_disable_tls", [True, False])
-def test__run_tls(mqtt_disable_tls: bool) -> None:
-    with unittest.mock.patch("paho.mqtt.client.Client") as mqtt_client_mock:
+def test__run_tls(
+    caplog: _pytest.logging.LogCaptureFixture, mqtt_disable_tls: bool
+) -> None:
+    with unittest.mock.patch(
+        "paho.mqtt.client.Client"
+    ) as mqtt_client_mock, caplog.at_level(logging.INFO):
         switchbot_mqtt._run(
             mqtt_host="mqtt.local",
             mqtt_port=1234,
@@ -144,6 +148,10 @@ def test__run_tls(mqtt_disable_tls: bool) -> None:
         mqtt_client_mock().tls_set.assert_not_called()
     else:
         mqtt_client_mock().tls_set.assert_called_once_with(ca_certs=None)
+    if mqtt_disable_tls:
+        assert caplog.record_tuples[0][2].endswith(" (TLS disabled)")
+    else:
+        assert caplog.record_tuples[0][2].endswith(" (TLS enabled)")
 
 
 @pytest.mark.parametrize("mqtt_host", ["mqtt-broker.local"])