-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
121 lines (100 loc) · 2.89 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
// Copyright (c) 2024 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
// Package textapp demostrates an app that displays a text area in Home
// Assistant. Any text entered in the textarea will be shown as a notification
// on the device running the Go Hass Anything the notify-send command-line tool.
//
//revive:disable:unused-receiver
package textapp
import (
"context"
"encoding/json"
"log/slog"
"os/exec"
"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 Text Area Example App"
appID = "mqtt_textentity_example"
minTextLen = 0
maxTextLen = 255
)
type TextApp struct {
entity *mqtthass.TextEntity
text string
}
func New(_ context.Context) (*TextApp, error) {
app := &TextApp{}
app.text = "Replace this text and hit return!"
app.entity = mqtthass.NewTextEntity().
WithMode(mqtthass.PlainText).
WithMin(minTextLen).
WithMax(maxTextLen).
WithDetails(
mqtthass.App(appName),
mqtthass.Name("Text"),
mqtthass.ID("text"),
mqtthass.DeviceInfo(newDevice()),
).
WithCommand(
mqtthass.CommandCallback(app.commandCallback),
).
WithState(
mqtthass.StateCallback(app.stateCallback),
mqtthass.ValueTemplate("{{ value }}"),
)
return app, nil
}
func (a *TextApp) Name() string {
return appName
}
func (a *TextApp) Configuration() []*mqttapi.Msg {
textEntityCfg, err := a.entity.MarshalConfig()
if err != nil {
slog.Error("Could not marshal text entity config.",
slog.Any("error", err))
return nil
}
return []*mqttapi.Msg{textEntityCfg}
}
func (a *TextApp) States() []*mqttapi.Msg {
textEntityState, err := a.entity.MarshalState()
if err != nil {
slog.Warn("Unable to marshal text state to MQTT message.",
slog.Any("error", err))
}
return []*mqttapi.Msg{textEntityState}
}
func (a *TextApp) Subscriptions() []*mqttapi.Subscription {
textEntitySub, err := a.entity.MarshalSubscription()
if err != nil {
slog.Warn("Unable to marshal text state subscription.",
slog.Any("error", err))
}
return []*mqttapi.Subscription{textEntitySub}
}
// Update is unused, there is no app data to update.
func (a *TextApp) Update(_ context.Context) error { return nil }
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,
}
}
func (a *TextApp) stateCallback(_ ...any) (json.RawMessage, error) {
return json.RawMessage(a.text), nil
}
func (a *TextApp) commandCallback(p *paho.Publish) {
a.text = string(p.Payload)
if err := exec.Command("notify-send", a.text).Run(); err != nil {
slog.Warn("Could not execute notify-send.",
slog.Any("error", err))
}
}