Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

availability_topic for Home assistant #105

Merged
merged 9 commits into from Aug 30, 2022

Conversation

phbasler
Copy link
Contributor

With this PR a Birth message and a Will message is set.
Upon connection the message "online" is published to switchbot_mqtt/availability and upon loosing connection (KeepAlve Timeout or job was killed) the Will publishes "offline" to the same topic.
For the mqtt sensors in home assistant you can set the availability_topic to this topic and then you can check if the service is running or not.

If you want we could add a CLI Option to set this topic to have different topics for multiple Instances.

@fphammerle fphammerle added the enhancement New feature or request label Aug 29, 2022
Copy link
Owner

@fphammerle fphammerle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @phbasler !

Could you take a look at my suggestions and format the changes with black?

switchbot_mqtt/__init__.py Outdated Show resolved Hide resolved
switchbot_mqtt/__init__.py Outdated Show resolved Hide resolved
switchbot_mqtt/__init__.py Outdated Show resolved Hide resolved
switchbot_mqtt/__init__.py Outdated Show resolved Hide resolved
phbasler and others added 6 commits August 29, 2022 20:19
Co-authored-by: Fabian Peter Hammerle <fabian@hammerle.me>
Co-authored-by: Fabian Peter Hammerle <fabian@hammerle.me>
Co-authored-by: Fabian Peter Hammerle <fabian@hammerle.me>
@phbasler
Copy link
Contributor Author

@fphammerle I applied the changes and formatted with black. I also changed the topic to end with "status" like suggested.

@fphammerle
Copy link
Owner

Thank you!

What happens if switchbot-mqtt shuts down gracefully? (planned disconnect)
Will "offline" be reported in that case?

@fphammerle
Copy link
Owner

Shall we set retain=True in .will_set and .publish (to preserve the status information in case some recipients reset/restarted, e.g. home assistant)?

@phbasler
Copy link
Contributor Author

phbasler commented Aug 30, 2022

@fphammerle Good idea with the retain message. I added the flags.
In case of a clean disconnect (the client actually sends the disconnect message), the will is not triggered and "offline" is not send. Is there an actual way for the user to call disconnect? I always ended the code by stopping the systemd service or ctrl+c it

Edit: It doesn't seem that you are calling the disconnect function of the mqtt client anywhere in your code, so it never exits with a clean disconnect

fphammerle added a commit that referenced this pull request Aug 30, 2022
@fphammerle fphammerle merged commit 7bd771b into fphammerle:master Aug 30, 2022
@fphammerle
Copy link
Owner

Thank you!

I expected mqtt_client.loop_forever() to trigger a clean disconnect when receiving SIGTERM, but apparently it does not:
https://github.com/eclipse/paho.mqtt.python/blob/master/src/paho/mqtt/client.py#L1740

@phbasler phbasler deleted the BirthLWT branch August 30, 2022 18:50
@fphammerle
Copy link
Owner

released here: https://pypi.org/project/switchbot-mqtt/3.3.0/

fphammerle added a commit that referenced this pull request Aug 31, 2022
…will on `homeassistant/switchbot_mqtt/status`)

partially reverts commit 9947875

#106 (comment)
#105
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants