Skip to content

Commit

Permalink
indicate in log message whether TLS is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
fphammerle committed Apr 2, 2022
1 parent 3431242 commit 965ca26
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 6 additions & 1 deletion switchbot_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 11 additions & 3 deletions tests/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand All @@ -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"])
Expand Down

0 comments on commit 965ca26

Please sign in to comment.