feat(bridge): MQTT bridge with Home Assistant auto-discovery
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package bridge
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
func (b *Bridge) publishDiscovery() {
|
||||
type component struct {
|
||||
Platform string `json:"p"`
|
||||
Name string `json:"name"`
|
||||
UniqueID string `json:"unique_id"`
|
||||
CommandTopic string `json:"command_topic"`
|
||||
StateTopic string `json:"state_topic"`
|
||||
Options []string `json:"options"`
|
||||
}
|
||||
type payload struct {
|
||||
Device struct {
|
||||
Identifiers []string `json:"identifiers"`
|
||||
Name string `json:"name"`
|
||||
Manufacturer string `json:"manufacturer"`
|
||||
Model string `json:"model"`
|
||||
} `json:"device"`
|
||||
Origin map[string]string `json:"origin"`
|
||||
Availability []map[string]string `json:"availability"`
|
||||
Components map[string]component `json:"components"`
|
||||
}
|
||||
|
||||
options := make([]string, numInputs)
|
||||
for i := range options {
|
||||
options[i] = inputLabel(i + 1)
|
||||
}
|
||||
|
||||
p := payload{
|
||||
Origin: map[string]string{"name": "ezcoo-usb-control"},
|
||||
Availability: []map[string]string{{"topic": b.availTopic()}},
|
||||
Components: make(map[string]component, numOutputs),
|
||||
}
|
||||
p.Device.Identifiers = []string{"ezcoo_mx42has_arc"}
|
||||
p.Device.Name = "EZCOO HDMI Matrix"
|
||||
p.Device.Manufacturer = "EZCOO"
|
||||
p.Device.Model = "EZ-MX42HAS-ARC"
|
||||
|
||||
for out := 1; out <= numOutputs; out++ {
|
||||
p.Components[fmt.Sprintf("out%d", out)] = component{
|
||||
Platform: "select",
|
||||
Name: fmt.Sprintf("Output %d", out),
|
||||
UniqueID: fmt.Sprintf("ezcoo_mx42_out%d", out),
|
||||
CommandTopic: b.cmdTopic(out),
|
||||
StateTopic: b.stateTopic(out),
|
||||
Options: options,
|
||||
}
|
||||
}
|
||||
|
||||
data, err := json.Marshal(p)
|
||||
if err != nil {
|
||||
slog.Error("marshal discovery payload", "err", err)
|
||||
return
|
||||
}
|
||||
b.client.Publish(b.discoveryTopic(), 1, true, data)
|
||||
slog.Info("published HA discovery")
|
||||
}
|
||||
Reference in New Issue
Block a user