-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathapp.go
More file actions
453 lines (383 loc) · 12.2 KB
/
Copy pathapp.go
File metadata and controls
453 lines (383 loc) · 12.2 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package gomeassistant
import (
"context"
"errors"
"fmt"
"log/slog"
"net/url"
"sync"
"sync/atomic"
"time"
"github.com/golang-module/carbon"
sunriseLib "github.com/nathan-osman/go-sunrise"
"saml.dev/gome-assistant/internal"
"saml.dev/gome-assistant/internal/http"
"saml.dev/gome-assistant/websocket"
)
var ErrInvalidArgs = errors.New("invalid arguments provided")
// ErrAppClosed is returned when Start is called after Cleanup, or when Cleanup
// was called before the application had a chance to start.
var ErrAppClosed = errors.New("app is closed")
// ErrAppNotRunning is returned when an operation requires an active session
// but Start has not acquired a connection (or has already released it).
var ErrAppNotRunning = errors.New("app is not running")
// ErrConnectionClosed is returned when a connection terminates without
// recording a more specific terminal error.
var ErrConnectionClosed = errors.New("websocket connection is closed")
// scheduledAction represents an action that can schedule and run
// itself, perhaps repeatedly.
type scheduledAction interface {
run(ctx context.Context, app *App)
}
type App struct {
closed atomic.Bool
workers sync.WaitGroup
ctx context.Context
run atomic.Pointer[runCancellation]
baseURL *url.URL
authToken string
homeZoneEntityID string
// Wraps the ws connection with added mutex locking
conn *websocket.Conn
httpClient *http.HttpClient
service *Service
state *StateImpl
scheduledActions []scheduledAction
scheduleCount int
entityListeners map[string][]*EntityListener
entitySubscription websocket.Subscription
eventListeners map[string][]*EventListener
}
// runCancellation is published before Start begins startup work so Cleanup can
// always cancel a Start call that has passed its initial closed check.
type runCancellation struct {
cancel context.CancelFunc
}
// activeConn returns the connection owned by the current Start call.
func (app *App) activeConn() (*websocket.Conn, error) {
if app.closed.Load() {
return nil, ErrAppClosed
}
if app.conn == nil {
return nil, ErrAppNotRunning
}
return app.conn, nil
}
// DurationString represents a duration, such as "2s" or "24h".
// See https://pkg.go.dev/time#ParseDuration for all valid time units.
type DurationString string
// TimeString is a 24-hr format time "HH:MM" such as "07:30".
type TimeString string
type timeRange struct {
start time.Time
end time.Time
}
type NewAppRequest struct {
// Required
URL string
// Required
// Auth token generated in Home Assistant. Used
// to connect to the Websocket API.
HAAuthToken string
// Required
// EntityID of the zone representing your home e.g. "zone.home".
// Used to pull latitude/longitude from Home Assistant
// to calculate sunset/sunrise times.
HomeZoneEntityID string
}
// NewApp validates its configuration and returns an inert application that
// you can use to register schedules and listeners.
func NewApp(ctx context.Context, request NewAppRequest) (*App, error) {
if ctx == nil || request.URL == "" || request.HAAuthToken == "" {
slog.Error("URL and HAAuthToken are required arguments in NewAppRequest")
return nil, ErrInvalidArgs
}
// Set default home zone if not provided
if request.HomeZoneEntityID == "" {
request.HomeZoneEntityID = "zone.home"
}
baseURL, err := url.Parse(request.URL)
if err != nil {
return nil, ErrInvalidArgs
}
if (baseURL.Scheme != "http" && baseURL.Scheme != "https") || baseURL.Host == "" {
return nil, ErrInvalidArgs
}
httpClient := http.NewHttpClient(baseURL, request.HAAuthToken)
state := newState(httpClient)
app := App{
baseURL: baseURL,
authToken: request.HAAuthToken,
homeZoneEntityID: request.HomeZoneEntityID,
httpClient: httpClient,
state: state,
entityListeners: map[string][]*EntityListener{},
eventListeners: map[string][]*EventListener{},
}
app.service = newService(&app)
return &app, nil
}
// Cleanup permanently closes the app and asks the current Start call to stop.
// Start owns connection cleanup and waits for application-owned goroutines
// before returning.
func (app *App) Cleanup() {
if app.closed.Swap(true) {
return
}
if run := app.run.Load(); run != nil {
run.cancel()
}
}
func (app *App) RegisterSchedules(schedules ...DailySchedule) {
for _, s := range schedules {
// Keep scheduler state internal: registrations take values, and workers
// advance this dedicated copy across Start sessions.
schedule := new(DailySchedule)
*schedule = s
// Solar schedules depend on the home-zone coordinates loaded by Start.
// Keep registration network-free and initialize them after that load.
if schedule.isSunrise || schedule.isSunset {
app.scheduledActions = append(app.scheduledActions, schedule)
app.scheduleCount++
continue
}
now := carbon.Now()
startTime := carbon.Now().SetTimeMilli(schedule.hour, schedule.minute, 0, 0)
// advance first scheduled time by frequency until it is in the future
if startTime.Lt(now) {
startTime = startTime.AddDay()
}
schedule.nextRunTime = startTime.Carbon2Time()
app.scheduledActions = append(app.scheduledActions, schedule)
app.scheduleCount++
}
}
func (app *App) RegisterIntervals(intervals ...Interval) {
for _, i := range intervals {
// Keep scheduler state internal: registrations take values, and workers
// advance this dedicated copy across Start sessions.
interval := new(Interval)
*interval = i
if interval.frequency == 0 {
slog.Error("A schedule must use either set frequency via Every()")
panic(ErrInvalidArgs)
}
interval.nextRunTime = internal.ParseTime(string(interval.startTime)).Carbon2Time()
now := time.Now()
for interval.nextRunTime.Before(now) {
interval.nextRunTime = interval.nextRunTime.Add(interval.frequency)
}
app.scheduledActions = append(app.scheduledActions, interval)
}
}
func (app *App) registerEntityListener(etl EntityListener) {
if etl.delay != 0 && etl.toState == "" {
slog.Error("EntityListener error: you have to use ToState() when using Duration()")
panic(ErrInvalidArgs)
}
for _, entity := range etl.entityIDs {
app.entityListeners[entity] = append(app.entityListeners[entity], &etl)
}
}
func (app *App) RegisterEntityListeners(etls ...EntityListener) {
for _, etl := range etls {
app.registerEntityListener(etl)
}
}
func (app *App) registerEventListener(evl EventListener) {
for _, eventType := range evl.eventTypes {
app.eventListeners[eventType] = append(app.eventListeners[eventType], &evl)
}
}
func (app *App) RegisterEventListeners(evls ...EventListener) {
for _, evl := range evls {
app.registerEventListener(evl)
}
}
func getSunriseSunset(s *StateImpl, sunrise bool, dateToUse carbon.Carbon, offset ...DurationString) carbon.Carbon {
date := dateToUse.Carbon2Time()
rise, set := sunriseLib.SunriseSunset(s.latitude, s.longitude, date.Year(), date.Month(), date.Day())
rise, set = rise.Local(), set.Local()
val := set
printString := "Sunset"
if sunrise {
val = rise
printString = "Sunrise"
}
setOrRiseToday := carbon.Parse(val.String())
var t time.Duration
var err error
if len(offset) == 1 {
t, err = time.ParseDuration(string(offset[0]))
if err != nil {
parsingErr := fmt.Errorf("could not parse offset passed to %s: \"%s\": %w", printString, offset[0], err)
slog.Error(parsingErr.Error())
panic(parsingErr)
}
}
// add offset if set, this code works for negative values too
if t.Microseconds() != 0 {
setOrRiseToday = setOrRiseToday.AddMinutes(int(t.Minutes()))
}
return setOrRiseToday
}
func getNextSunRiseOrSet(app *App, sunrise bool, offset ...DurationString) carbon.Carbon {
sunriseOrSunset := getSunriseSunset(app.state, sunrise, carbon.Now(), offset...)
if sunriseOrSunset.Lt(carbon.Now()) {
// if we're past today's sunset or sunrise (accounting for offset) then get tomorrows
// as that's the next time the schedule will run
sunriseOrSunset = getSunriseSunset(app.state, sunrise, carbon.Tomorrow(), offset...)
}
return sunriseOrSunset
}
func (app *App) initializeSolarSchedules() {
for _, action := range app.scheduledActions {
schedule, ok := action.(*DailySchedule)
if !ok || (!schedule.isSunrise && !schedule.isSunset) {
continue
}
schedule.nextRunTime = getNextSunRiseOrSet(app, schedule.isSunrise, schedule.sunOffset).Carbon2Time()
}
}
// Start owns one Home Assistant connection and blocks until the context is
// canceled or the session ends. It does not retry; callers decide whether and
// when to call Start again. Start calls must not overlap.
func (app *App) Start(ctx context.Context) error {
if ctx == nil {
return ErrInvalidArgs
}
if app.closed.Load() {
return ErrAppClosed
}
if err := ctx.Err(); err != nil {
return err
}
runCtx, cancel := context.WithCancel(ctx)
run := &runCancellation{cancel: cancel}
app.run.Store(run)
if app.closed.Load() {
cancel()
}
app.ctx = runCtx
defer func() {
cancel()
if app.conn != nil {
_ = app.conn.Close()
}
app.workers.Wait()
app.conn = nil
app.run.CompareAndSwap(run, nil)
}()
slog.Info("Starting", "schedules", app.scheduleCount)
slog.Info("Starting", "entity listeners", len(app.entityListeners))
slog.Info("Starting", "event listeners", len(app.eventListeners))
conn, err := websocket.NewConn(runCtx, app.baseURL, app.authToken)
if err != nil {
if runErr := runCtx.Err(); runErr != nil {
return runErr
}
return err
}
app.conn = conn
if err := app.state.loadHomeZone(runCtx, app.homeZoneEntityID); err != nil {
if runErr := runCtx.Err(); runErr != nil {
return runErr
}
return err
}
app.initializeSolarSchedules()
if err := app.subscribeSession(conn); err != nil {
if runErr := runCtx.Err(); runErr != nil {
return runErr
}
return err
}
app.goTracked(func() { app.runScheduledActions(runCtx) })
app.runStartupCallbacks(runCtx)
return conn.Run(runCtx)
}
func (app *App) subscribeSession(conn *websocket.Conn) (err error) {
defer func() {
if recovered := recover(); recovered != nil {
err = fmt.Errorf("subscribe during startup: %v", recovered)
}
}()
// subscribe to state_changed events
app.entitySubscription = conn.SubscribeToStateChangedEvents(
func(msg websocket.Message) {
app.goTracked(func() { app.callEntityListeners(msg.Raw) })
},
)
eventTypes := make([]string, 0, len(app.eventListeners))
for eventType := range app.eventListeners {
eventTypes = append(eventTypes, eventType)
}
for _, eventType := range eventTypes {
eventType := eventType
conn.SubscribeToEventType(eventType, func(msg websocket.Message) {
if msg.Type != "event" {
return
}
app.goTracked(func() { app.callEventListeners(eventType, msg) })
})
}
return nil
}
func (app *App) runStartupCallbacks(ctx context.Context) {
completed := make(map[*EntityListener]bool)
// entity listeners runOnStartup
for eid, etls := range app.entityListeners {
for _, etl := range etls {
// ensure each ETL only runs once, even if
// it listens to multiple entities
if etl.runOnStartup && !completed[etl] {
entityState, err := app.state.getWithContext(ctx, eid)
if err != nil {
slog.Warn("Failed to get entity state \"", eid, "\" during startup, skipping RunOnStartup")
continue
}
completed[etl] = true
data := EntityData{
TriggerEntityID: eid,
FromState: entityState.State,
FromAttributes: entityState.Attributes,
ToState: entityState.State,
ToAttributes: entityState.Attributes,
LastChanged: entityState.LastChanged,
}
app.goTracked(func() { etl.callback(app.service, app.state, data) })
}
}
}
}
func (app *App) goTracked(fn func()) {
if app.ctx != nil && app.ctx.Err() != nil {
return
}
app.workers.Add(1)
go func() {
defer app.workers.Done()
fn()
}()
}
// runScheduledActions starts a goroutine to run each `DailySchedule`
// and each `Interval` that has been configured. The `run()` method of
// each of those instances takes care of deciding when to run and
// invoking its callback.
func (app *App) runScheduledActions(ctx context.Context) {
var wg sync.WaitGroup
defer wg.Wait()
for _, action := range app.scheduledActions {
wg.Add(1)
go func(action scheduledAction) {
defer wg.Done()
action.run(ctx, app)
}(action)
}
}
func (app *App) GetService() *Service {
return app.service
}
func (app *App) GetState() State {
return app.state
}