Browse Source

document new birth & last will message in readme & changelog; extend tests; refactor

https://github.com/fphammerle/switchbot-mqtt/pull/105
Fabian Peter Hammerle 1 year ago
parent
commit
ed7f106a95
4 changed files with 19 additions and 6 deletions
  1. 4 0
      CHANGELOG.md
  2. 5 0
      README.md
  3. 4 6
      switchbot_mqtt/__init__.py
  4. 6 0
      tests/test_mqtt.py

+ 4 - 0
CHANGELOG.md

@@ -5,6 +5,10 @@ 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
+- Birth ("online") and last will ("offline") message on topic
+  `homeassistant/switchbot-mqtt/status`
+  (@hbasler, https://github.com/fphammerle/switchbot-mqtt/pull/105)
 
 ## [3.2.1] - 2022-07-09
 ### Fixed

+ 5 - 0
README.md

@@ -105,6 +105,11 @@ switchbot-mqtt --mqtt-topic-prefix living-room/ …
 switchbot-mqtt --mqtt-topic-prefix '' …
 ```
 
+### Service Status Report
+
+After connecting to the MQTT broker, `switchbot-mqtt` will report `online` on topic `homeassistant/switchbot-mqtt/status`.
+When disconnecting (graceful shutdown or unexpected loss of connection), `offline` will be reported on the same topic.
+
 ## Home Assistant 🏡
 
 ### Rationale

+ 4 - 6
switchbot_mqtt/__init__.py

@@ -52,11 +52,10 @@ def _mqtt_on_connect(
         else mqtt_broker_host,
         mqtt_broker_port,
     )
-    _availability_topic_with_prefix = (
-        userdata.mqtt_topic_prefix + _MQTT_AVAILABILITY_TOPIC
-    )
     mqtt_client.publish(
-        topic=_availability_topic_with_prefix, payload=_MQTT_BIRTH_PAYLOAD, retain=True
+        topic=userdata.mqtt_topic_prefix + _MQTT_AVAILABILITY_TOPIC,
+        payload=_MQTT_BIRTH_PAYLOAD,
+        retain=True,
     )
     _ButtonAutomator.mqtt_subscribe(mqtt_client=mqtt_client, settings=userdata)
     _CurtainMotor.mqtt_subscribe(mqtt_client=mqtt_client, settings=userdata)
@@ -96,9 +95,8 @@ def _run(
         mqtt_client.username_pw_set(username=mqtt_username, password=mqtt_password)
     elif mqtt_password:
         raise ValueError("Missing MQTT username")
-    _availability_topic_with_prefix = mqtt_topic_prefix + _MQTT_AVAILABILITY_TOPIC
     mqtt_client.will_set(
-        topic=_availability_topic_with_prefix,
+        topic=mqtt_topic_prefix + _MQTT_AVAILABILITY_TOPIC,
         payload=_MQTT_LAST_WILL_PAYLOAD,
         retain=True,
     )

+ 6 - 0
tests/test_mqtt.py

@@ -65,6 +65,9 @@ def test__mqtt_on_connect(
             {},
             0,
         )
+    mqtt_client.publish.assert_called_once_with(
+        topic="whatever/switchbot_mqtt/status", payload="online", retain=True
+    )
     assert mqtt_client.subscribe.call_args_list == [
         unittest.mock.call("whatever/switch/switchbot/+/set"),
         unittest.mock.call("whatever/cover/switchbot-curtain/+/set"),
@@ -138,6 +141,9 @@ def test__run(
     )
     assert not mqtt_client_mock().username_pw_set.called
     mqtt_client_mock().tls_set.assert_called_once_with(ca_certs=None)
+    mqtt_client_mock().will_set.assert_called_once_with(
+        topic="homeassistant/switchbot_mqtt/status", payload="offline", retain=True
+    )
     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)
     with caplog.at_level(logging.DEBUG):