-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
143 lines (119 loc) · 3.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2024 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
// Package switchapp demonstrates an app that displays a switch in Home
// Assistant.
//
//revive:disable:unused-receiver
package switchapp
import (
"context"
"encoding/json"
"log/slog"
"github.com/eclipse/paho.golang/paho"
mqtthass "github.com/joshuar/go-hass-anything/v12/pkg/hass"
mqttapi "github.com/joshuar/go-hass-anything/v12/pkg/mqtt"
)
const (
appName = "Go Hass Anything Switch Example App"
appID = "mqtt_switch_example"
)
type SwitchApp struct {
entity *mqtthass.SwitchEntity
msgCh chan *mqttapi.Msg
entityState bool
}
func New(_ context.Context) (*SwitchApp, error) {
app := &SwitchApp{
msgCh: make(chan *mqttapi.Msg),
}
app.entity = mqtthass.NewSwitchEntity().
OptimisticMode().
WithDetails(
mqtthass.App(appName),
mqtthass.Name("Switch"),
mqtthass.ID("switch"),
mqtthass.DeviceInfo(newDevice()),
).
WithState(
mqtthass.StateCallback(app.switchStateCallback),
mqtthass.ValueTemplate("{{ value }}"),
).
WithCommand(
mqtthass.CommandCallback(app.switchCommandCallback),
)
return app, nil
}
func (a *SwitchApp) Name() string {
return appName
}
func (a *SwitchApp) Configuration() []*mqttapi.Msg {
switchEntityCfg, err := a.entity.MarshalConfig()
if err != nil {
slog.Error("Could not marshal switch entity config.", "error", err)
return nil
}
return []*mqttapi.Msg{switchEntityCfg}
}
func (a *SwitchApp) States() []*mqttapi.Msg {
switchEntityState, err := a.entity.MarshalState()
if err != nil {
slog.Warn("Unable to marshal switch state to MQTT message.", "error", err.Error())
}
return []*mqttapi.Msg{switchEntityState}
}
func (a *SwitchApp) Subscriptions() []*mqttapi.Subscription {
switchEntitySub, err := a.entity.MarshalSubscription()
if err != nil {
slog.Warn("Unable to marshal switch state subscription.", "error", err.Error())
}
return []*mqttapi.Subscription{switchEntitySub}
}
// Update is unused, there is no app data to update.
func (a *SwitchApp) Update(_ context.Context) error { return nil }
// MsgCh returns a channel through which we could pass any message to MQTT on
// any kind of custom event trigger or other non time-based polling.
func (a *SwitchApp) MsgCh() chan *mqttapi.Msg {
return a.msgCh
}
// switchCallback is our callback function for when the switch entity is
// manipulated in Home Assistant.
func (a *SwitchApp) switchStateCallback(_ ...any) (json.RawMessage, error) {
switch a.entityState {
case true:
return json.RawMessage(`ON`), nil
default:
return json.RawMessage(`OFF`), nil
}
}
// switchCommandCallback is our callback function for when the switch entity is
// manipulated in Home Assistant.
func (a *SwitchApp) switchCommandCallback(p *paho.Publish) {
// Record the new state.
state := string(p.Payload)
switch state {
case "ON":
slog.Info("Switch was turned on.")
a.entityState = true
case "OFF":
slog.Info("Switch was turned off.")
a.entityState = false
}
// Publish a message with the new state.
msg, err := a.entity.MarshalState()
if err != nil {
slog.Warn("Unable to marshal new state message.", "error", err.Error())
} else {
a.msgCh <- msg
}
}
func newDevice() *mqtthass.Device {
return &mqtthass.Device{
Name: appName,
Identifiers: []string{appID},
URL: "https://github.com/joshuar/go-hass-anything",
Manufacturer: "go-hass-anything",
Model: appID,
}
}