Browse Source

report battery level of button automators on expected topic `homeassistant/switch/switchbot/MAC_ADDRESS/battery-percentage` (commit 4feeb7a56d99d2a3b3649c027a150157d2678b2c introduced report on unexpected topic)

Fabian Peter Hammerle 2 years ago
parent
commit
32c3391404
4 changed files with 31 additions and 8 deletions
  1. 4 0
      CHANGELOG.md
  2. 1 1
      README.md
  3. 16 0
      switchbot_mqtt/__init__.py
  4. 10 7
      tests/test_switchbot_button_automator.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
+- battery level of button automators will additionally be reported on topic
+  `homeassistant/switch/switchbot/MAC_ADDRESS/battery-percentage`
+  (old topic kept for downward compatibility)
 
 ## [2.0.0] - 2021-10-16
 ### Added

+ 1 - 1
README.md

@@ -39,7 +39,7 @@ $ mosquitto_pub -h MQTT_BROKER -t homeassistant/switch/switchbot/aa:bb:cc:dd:ee:
 ```
 
 The command-line option `--fetch-device-info` enables battery level reports on topic
-`homeassistant/cover/switchbot-curtain/MAC_ADDRESS/battery-percentage` after every command.
+`homeassistant/switch/switchbot/MAC_ADDRESS/battery-percentage` after every command.
 
 ### Curtain Motor
 

+ 16 - 0
switchbot_mqtt/__init__.py

@@ -277,6 +277,13 @@ class _ButtonAutomator(_MQTTControlledActor):
         "state",
     ]
     _MQTT_BATTERY_PERCENTAGE_TOPIC_LEVELS = _MQTT_TOPIC_LEVELS_PREFIX + [
+        "switch",
+        "switchbot",
+        _MQTTTopicPlaceholder.MAC_ADDRESS,
+        "battery-percentage",
+    ]
+    # for downward compatibility (will be removed in v3):
+    _MQTT_BATTERY_PERCENTAGE_TOPIC_LEVELS_LEGACY = _MQTT_TOPIC_LEVELS_PREFIX + [
         "cover",
         "switchbot",
         _MQTTTopicPlaceholder.MAC_ADDRESS,
@@ -296,6 +303,15 @@ class _ButtonAutomator(_MQTTControlledActor):
     def _get_device(self) -> switchbot.SwitchbotDevice:
         return self.__device
 
+    def _report_battery_level(self, mqtt_client: paho.mqtt.client.Client) -> None:
+        super()._report_battery_level(mqtt_client=mqtt_client)
+        # kept for downward compatibility (will be removed in v3)
+        self._mqtt_publish(
+            topic_levels=self._MQTT_BATTERY_PERCENTAGE_TOPIC_LEVELS_LEGACY,
+            payload=str(self._get_device().get_battery_percent()).encode(),
+            mqtt_client=mqtt_client,
+        )
+
     def execute_command(
         self,
         mqtt_message_payload: bytes,

+ 10 - 7
tests/test_switchbot_button_automator.py

@@ -31,10 +31,10 @@ import switchbot_mqtt
 @pytest.mark.parametrize("mac_address", ["{MAC_ADDRESS}", "aa:bb:cc:dd:ee:ff"])
 def test_get_mqtt_battery_percentage_topic(mac_address):
     assert (
-        switchbot_mqtt._CurtainMotor.get_mqtt_battery_percentage_topic(
+        switchbot_mqtt._ButtonAutomator.get_mqtt_battery_percentage_topic(
             mac_address=mac_address
         )
-        == f"homeassistant/cover/switchbot-curtain/{mac_address}/battery-percentage"
+        == f"homeassistant/switch/switchbot/{mac_address}/battery-percentage"
     )
 
 
@@ -51,11 +51,14 @@ def test__update_and_report_device_info(
     with unittest.mock.patch("switchbot.Switchbot.update") as update_mock:
         actor._update_and_report_device_info(mqtt_client=mqtt_client_mock)
     update_mock.assert_called_once_with()
-    mqtt_client_mock.publish.assert_called_once_with(
-        topic="homeassistant/cover/switchbot/dummy/battery-percentage",
-        payload=battery_percent_encoded,
-        retain=True,
-    )
+    assert mqtt_client_mock.publish.call_args_list == [
+        unittest.mock.call(topic=t, payload=battery_percent_encoded, retain=True)
+        for t in [
+            "homeassistant/switch/switchbot/dummy/battery-percentage",
+            # will be removed in v3:
+            "homeassistant/cover/switchbot/dummy/battery-percentage",
+        ]
+    ]
 
 
 @pytest.mark.parametrize("mac_address", ["aa:bb:cc:dd:ee:ff", "aa:bb:cc:11:22:33"])