-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathexample.go
More file actions
131 lines (112 loc) · 3.51 KB
/
example.go
File metadata and controls
131 lines (112 loc) · 3.51 KB
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
package main
import (
"context"
"encoding/json"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"saml.dev/gome-assistant/cmd/example/entities" // Optional import generated entities
ga "saml.dev/gome-assistant"
)
//go:generate go run saml.dev/gome-assistant/cmd/generate
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
app, err := ga.NewApp(
ctx,
ga.NewAppRequest{
URL: "http://192.168.86.67:8123", // Replace with your Home Assistant URL
HAAuthToken: os.Getenv("HA_AUTH_TOKEN"),
HomeZoneEntityID: "zone.home",
},
)
if err != nil {
slog.Error("Error connecting to HASS:", "error", err)
os.Exit(1)
}
defer app.Cleanup()
pantryDoor := ga.
NewEntityListener().
EntityIDs(entities.BinarySensor.PantryDoor). // Use generated entity constant
Call(func(service *ga.Service, state ga.State, sensor ga.EntityData) {
pantryLights(ctx, service, state, sensor)
}).
Build()
_11pmSched := ga.
NewDailySchedule().
Call(func(service *ga.Service, state ga.State) {
lightsOut(ctx, service, state)
}).
At("23:00").
Build()
_30minsBeforeSunrise := ga.
NewDailySchedule().
Call(func(service *ga.Service, state ga.State) {
sunriseSched(ctx, service, state)
}).
Sunrise("-30m").
Build()
zwaveEventListener := ga.
NewEventListener().
EventTypes("zwave_js_value_notification").
Call(onEvent).
Build()
app.RegisterEntityListeners(pantryDoor)
app.RegisterSchedules(_11pmSched, _30minsBeforeSunrise)
app.RegisterEventListeners(zwaveEventListener)
app.Start()
}
func pantryLights(
ctx context.Context, service *ga.Service, state ga.State, sensor ga.EntityData,
) {
l := "light.pantry"
// l := entities.Light.Pantry // Or use generated entity constant
if sensor.ToState == "on" {
if _, err := service.HomeAssistant.TurnOn(ctx, l); err != nil {
slog.Warn("couldn't turn on pantry light")
}
} else {
if _, err := service.HomeAssistant.TurnOff(ctx, l); err != nil {
slog.Warn("couldn't turn off pantry light")
}
}
}
func onEvent(service *ga.Service, state ga.State, data ga.EventData) {
// Since the structure of the event changes depending
// on the event type, you can Unmarshal the raw json
// into a Go type. If a type for your event doesn't
// exist, you can write it yourself! PR's welcome to
// the eventTypes.go file :)
ev := ga.EventZWaveJSValueNotification{}
json.Unmarshal(data.RawEventJSON, &ev)
slog.Info("On event invoked", "event", ev)
}
func lightsOut(ctx context.Context, service *ga.Service, state ga.State) {
// always turn off outside lights
if _, err := service.Light.TurnOff(ctx, entities.Light.OutsideLights); err != nil {
slog.Warn("couldn't turn off living room light, doing nothing")
return
}
s, err := state.Get(entities.BinarySensor.LivingRoomMotion)
if err != nil {
slog.Warn("couldn't get living room motion state, doing nothing")
return
}
// if no motion detected in living room for 30mins
if s.State == "off" && time.Since(s.LastChanged).Minutes() > 30 {
if _, err := service.Light.TurnOff(ctx, entities.Light.MainLights); err != nil {
slog.Warn("couldn't turn off living light")
return
}
}
}
func sunriseSched(ctx context.Context, service *ga.Service, state ga.State) {
if _, err := service.Light.TurnOn(ctx, entities.Light.LivingRoomLamps); err != nil {
slog.Warn("couldn't turn on living light")
}
if _, err := service.Light.TurnOff(ctx, entities.Light.ChristmasLights); err != nil {
slog.Warn("couldn't turn off Christmas lights")
}
}