Controlling an entity over MQTT starts from what its integration announced. Home Assistant defines the format in its MQTT discovery documentation.
What the client reads
All the forms Home Assistant accepts: abbreviated keys, the ~ topic base, and one payload describing a whole device with several components. Publishing always uses the full form.
Topics your broker must allow
The client subscribes to <prefix>/<domain>/... for the domains you use, and to <prefix>/device/... for device payloads. A broker access rule that refuses either makes discovery fail.
Read a payload yourself
// Integrations announce abbreviated keys and a "~" topic base.
// Config types read the full form only, so expand first.
full, err := mqtt.ExpandDiscoveryConfig(payload)
if err != nil {
return config, err
}
err = json.Unmarshal(full, &config)
Each domain's Config type reads the full form only. For a payload from a .../device/... topic, mqtt.ExpandDeviceDiscovery returns one full config per component.
Size limits
A discovery payload comes from whoever can publish on the broker, and the short forms multiply it. So the library has limits Home Assistant does not: 1 MiB per payload, 1000 components per device payload and 8 MiB in full form (the mqtt.MaxDiscovery… constants). A payload past one is not discovered: the error is mqtt.ErrDiscoveryTooLarge, and the client logs it.