Browse Source

log subscribed mqtt topics

Fabian Peter Hammerle 3 years ago
parent
commit
607019f8fc
2 changed files with 29 additions and 3 deletions
  1. 1 0
      switchbot_mqtt/__init__.py
  2. 28 3
      tests/test_mqtt.py

+ 1 - 0
switchbot_mqtt/__init__.py

@@ -98,6 +98,7 @@ class _MQTTControlledActor(abc.ABC):
             "+" if isinstance(l, _MQTTTopicPlaceholder) else l
             for l in cls.MQTT_COMMAND_TOPIC_LEVELS
         )
+        _LOGGER.info("subscribing to MQTT topic %r", command_topic)
         mqtt_client.subscribe(command_topic)
         mqtt_client.message_callback_add(
             sub=command_topic, callback=cls._mqtt_command_callback

+ 28 - 3
tests/test_mqtt.py

@@ -30,8 +30,10 @@ import switchbot_mqtt
 
 @pytest.mark.parametrize("mqtt_host", ["mqtt-broker.local"])
 @pytest.mark.parametrize("mqtt_port", [1833])
-def test__run(mqtt_host, mqtt_port):
-    with unittest.mock.patch("paho.mqtt.client.Client") as mqtt_client_mock:
+def test__run(caplog, mqtt_host, mqtt_port):
+    with unittest.mock.patch(
+        "paho.mqtt.client.Client"
+    ) as mqtt_client_mock, caplog.at_level(logging.DEBUG):
         switchbot_mqtt._run(
             mqtt_host=mqtt_host,
             mqtt_port=mqtt_port,
@@ -42,7 +44,8 @@ def test__run(mqtt_host, mqtt_port):
     assert not mqtt_client_mock().username_pw_set.called
     mqtt_client_mock().connect.assert_called_once_with(host=mqtt_host, port=mqtt_port)
     mqtt_client_mock().socket().getpeername.return_value = (mqtt_host, mqtt_port)
-    mqtt_client_mock().on_connect(mqtt_client_mock(), None, {}, 0)
+    with caplog.at_level(logging.DEBUG):
+        mqtt_client_mock().on_connect(mqtt_client_mock(), None, {}, 0)
     assert mqtt_client_mock().subscribe.call_args_list == [
         unittest.mock.call("homeassistant/switch/switchbot/+/set"),
         unittest.mock.call("homeassistant/cover/switchbot-curtain/+/set"),
@@ -58,6 +61,28 @@ def test__run(mqtt_host, mqtt_port):
         ),
     ]
     mqtt_client_mock().loop_forever.assert_called_once_with()
+    assert caplog.record_tuples == [
+        (
+            "switchbot_mqtt",
+            logging.INFO,
+            "connecting to MQTT broker {}:{}".format(mqtt_host, mqtt_port),
+        ),
+        (
+            "switchbot_mqtt",
+            logging.DEBUG,
+            "connected to MQTT broker {}:{}".format(mqtt_host, mqtt_port),
+        ),
+        (
+            "switchbot_mqtt",
+            logging.INFO,
+            "subscribing to MQTT topic 'homeassistant/switch/switchbot/+/set'",
+        ),
+        (
+            "switchbot_mqtt",
+            logging.INFO,
+            "subscribing to MQTT topic 'homeassistant/cover/switchbot-curtain/+/set'",
+        ),
+    ]
 
 
 @pytest.mark.parametrize("mqtt_host", ["mqtt-broker.local"])