Skip to content

Commit

Permalink
document new birth & last will message in readme & changelog; extend …
Browse files Browse the repository at this point in the history
…tests; refactor

#105
  • Loading branch information
fphammerle committed Aug 30, 2022
1 parent 7bd771b commit ed7f106
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 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
- 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
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions switchbot_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit ed7f106

Please sign in to comment.