Add Home Assistant MQTT Discovery support - #708
Draft
OKDaG wants to merge 1 commit into
Draft
Conversation
Adds an opt-in `ha_discovery` block under the existing `mqtt` config. When enabled, vzlogger publishes retained MQTT Discovery configs (`<prefix>/sensor/vzlogger_<uuid>/config`) for each channel on first reading, letting Home Assistant auto-create sensors with the correct unit, device_class and state_class derived from the channel's OBIS identifier. - Default off; no behaviour change for existing setups. - No new dependencies; uses the already-linked libjson-c. - Lazy publish, integrated next to the existing per-channel announce flow. - 18 common SML OBIS codes mapped (energy import/export, active power per phase, voltage and current per phase). Unknown codes fall back to a generic unit-less sensor. - All channels grouped under one HA device per vzlogger process; per-meter grouping would require a Channel->Meter back-reference and is left as a follow-up. - Metadata lookup and config-JSON assembly live in their own translation unit (mqtt_ha_discovery.cpp) so they can be unit-tested without a broker. Includes 6 gtest cases covering OBIS lookup and discovery JSON assembly. Example config snippet added to etc/vzlogger.conf.
Contributor
|
thanks for your contribution!
|
Author
|
LLM was partly involved creating the PR text. |
OKDaG
marked this pull request as draft
June 3, 2026 09:43
Author
|
Will adjust this PR if 550 is accepted. |
narc-Ontakac2
force-pushed
the
master
branch
6 times, most recently
from
July 24, 2026 11:43
32c98ac to
2a4d18e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds opt-in Home Assistant MQTT Discovery to the existing MQTT client. When enabled,
vzlogger publishes retained discovery configs (
<prefix>/sensor/vzlogger_<uuid>/config)for each channel on first reading, letting Home Assistant auto-create sensors with the
correct
unit_of_measurement,device_classandstate_classderived from thechannel's OBIS identifier.
Why
Today every Home Assistant user running vzlogger needs to hand-write a YAML sensor
block per channel just to give the raw
smartmeter/reading/chnN/rawtopic a unitand a meaningful name. Doing this once inside vzlogger removes that boilerplate and
makes the readings show up in HA as proper Energy / Power / Voltage / Current sensors
out of the box.
Design notes
ha_discovery.enableddefaults tofalse— no behaviour change forexisting setups.
libjson-c.existing per-channel announce flow in
MqttClient::publish(). Channels withoutreadings never produce discovery topics.
16.7.0, 1.7.0, 2.7.0, 36/56/76.7.0, 32/52/72.7.0, 31/51/71.7.0). Unknown OBIS codes
fall back to a unit-less generic sensor.
value_templateis only emitted whenmqtt.timestamp=true(JSON payload);otherwise the raw numeric payload is consumed directly by HA.
src/mqtt_ha_discovery.cpp) for the metadata lookupand config-JSON assembly so they can be unit-tested without a broker.
vzloggerby default, configurable viadevice_name/device_identifier). Per-meter grouping would require aChannel→Meterback-reference and is intentionally left as a follow-up to keepthis PR contained.
Config
Tests
Adds
tests/ut_mqtt_ha_discovery.cppwith 6 gtest cases covering:nullptrname,unique_id,state_topic,unit_of_measurement,device_class,state_class,device.*)unit_of_measurement,device_class,state_classtimestamp: trueadds thevalue_templateAll existing tests still pass.
Test plan
docker build --build-arg build_test=on .succeedsvzlogger_unit_tests,mock_metermap,mock_MeterW1therm,mock_MeterOMS,mock_MeterS0— 5/5 passing viamake testMqttHaDiscovery.*— 6/6 passing./check-formatting.shclean (verified in anubuntu:latestcontainer withclang-format-20)appear under the
vzloggerdevice with correct units (Wh / W) and the EnergyDashboard picks up the
total_increasingenergy sensors.