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

Add stand-alone script to generate and publish Home Assistant discovery messages #927

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

pbasista
Copy link

@pbasista pbasista commented Mar 24, 2023

Add a stand-alone Python script to generate and publish Home Assistant discovery messages to MQTT broker based on the files with the current meter readings and on the driver templates.

Motivation:

The current approach to provide Home Assistant discovery is implemented via a Home Assistant add-on. However, only some Home Assistant installation methods allow the users to install add-ons. For example, when using Home Assistant Core or Home Assistant Container, the option of installing and using add-ons is unavailable.

In addition, if I understand correctly, the current way of performing the Home Assistant discovery relies on explicit definition of every meter in a configuration file. The wmbusmeters application can, however, use wildcards for specifying meter IDs. This allows defining multiple similar meters in a single file. I believe that such a feature significantly improves user experience and that it should be utilized by the Home Assistant discovery procedure as well.

If you find this script useful, feel free to merge it.

@BIBOLV
Copy link
Collaborator

BIBOLV commented Mar 24, 2023

Tnx for PR!
Regarding template files - please submit them to https://github.com/wmbusmeters/wmbusmeters-ha-addon. Add-on folder in this repo is legacy one and should not be used.
Regarding script - could you explain a bit more? Is it intended for addon or for standalone installations (#610)?

@pbasista
Copy link
Author

could you explain a bit more? Is it intended for addon or for standalone installations

As is mentioned in the PR's title, this is a stand-alone script. It can of course be used by the Home Assistant add-on as well. But its primary purpose is to perform Home Assistant discovery manually, without the Home Assistant add-on.

@BIBOLV
Copy link
Collaborator

BIBOLV commented Mar 24, 2023

Could you explain working principles of script in more details?
After quick review of code, looks like it reads saved data in meter files, not parses each message, as it is with mosquitto_pub.sh for example or script suggested in #610. What are advantages of such approach? Should it be called on each reading reception?
One concern I have is that it is python with some requirements, so requirements file should be created, documentation updated with clear instructions how to use this script and we should also update installer to copy it in proper place together with template files.

@pbasista
Copy link
Author

Could you explain working principles of script in more details?

Yes. The discovery messages are generated from two sources. The first one are the actual meter readings files. They contain the meter name, meter ID and the used driver. The second source are the templates, i.e. files in the ha-addon/mqtt_discovery directory.

What are advantages of such approach?

As is mentioned in the PR's description, there are two main benefits. The first is decoupling from Home Assistant add-on. And the second is the support of meters defined with multiple or wildcard IDs.

Should it be called on each reading reception?

No. Due to the discovery messages being posted to the MQTT broker as retained messages, it is sufficient to only call it once for every wmbusmeters instance after all of its meters have acquired at least one reading.

In the script's current form, a typical usage scenario would be similar to the following:

  1. The user enables file-based meter readings and configures the meters within the wmbusmeters configuration as usual.
  2. Then they run the application for some time so that at least one reading for every relevant meter is captured.
  3. Then they run the script for generating the Home Assistant discovery messages and publishing them to MQTT broker.

requirements file should be created, documentation updated with clear instructions how to use this script

Possibly yes. It all depends on the desired design.

@BIBOLV
Copy link
Collaborator

BIBOLV commented Apr 1, 2023

Fact that this script should be executed only once and when data is received from meter or after changes are made (meters added or removed) does not seem convenient from user perspective, at least to me. Can this script be adopted to be used while calling with shell parameter from wmbusmeters and would process meter data and send it to mqtt?
@KrzysztofHajdamowicz, @patrislav1 - what are your thoughts?

@weetmuts
Copy link
Member

weetmuts commented Dec 4, 2023

Thanks to @pimlie we now have a new shell callback, metershell which is invoked the first time a meter is seen. Perhaps this shell can be used to fix this?

Note that if wmbusmeters is restarted, since it does not keep state, it will resend the metershell again when the first telegram from a metter arrives.

@avandorp
Copy link
Contributor

avandorp commented Apr 3, 2024

Resending HA auto discovery messages on startup seems completely acceptable and similar to what other projects do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants