Skip to content

Commit

Permalink
report battery level of button automators on expected topic `homeassi…
Browse files Browse the repository at this point in the history
…stant/switch/switchbot/MAC_ADDRESS/battery-percentage` (commit 4feeb7a introduced report on unexpected topic)
  • Loading branch information
fphammerle committed Oct 19, 2021
1 parent e5cd414 commit 32c3391
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions switchbot_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -296,6 +303,15 @@ def __init__(
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,
Expand Down
17 changes: 10 additions & 7 deletions tests/test_switchbot_button_automator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
@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"
)


Expand All @@ -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"])
Expand Down

0 comments on commit 32c3391

Please sign in to comment.