-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
85 lines (74 loc) · 2.58 KB
/
state.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
package goha
import "time"
type Attributes struct {
RgbColor []int
ColorTemp int
SupportedFeatures int
XyColor []float64
Transition int
Brightness int
BrightnessPct int
WhiteValue int
NextDawn time.Time
NextDusk time.Time
NextMidnight time.Time
NextNoon time.Time
NextRising time.Time
NextSetting time.Time
Elevation float64
Azimuth float64
Rising bool
FriendlyName string
Source string
}
type State struct {
LastChanged float64
LastUpdated float64
State string
Attributes Attributes
}
/*
func mergef[T comparable](a, b *T) {
if b != nil {
*a = *b
}
}
// TODO: Create per entity attribute structs and copy values into those.
// Use a generic optional package like https://github.com/Southclaws/opt for struct fields.
func (state *State) Merge(newState State) {
mergef(&state.LastChanged, &newState.LastChanged)
mergef(&state.LastUpdated, &newState.LastUpdated)
mergef(&state.State, &newState.State)
if len(newState.Attributes.RgbColor) > 0 {
state.Attributes.RgbColor = newState.Attributes.RgbColor
}
mergef(&state.Attributes.ColorTemp, &newState.Attributes.ColorTemp)
mergef(&state.Attributes.SupportedFeatures, &newState.Attributes.SupportedFeatures)
if len(newState.Attributes.XyColor) > 0 {
state.Attributes.XyColor = newState.Attributes.XyColor
}
mergef(&state.Attributes.Brightness, &newState.Attributes.Brightness)
mergef(&state.Attributes.BrightnessPct, &newState.Attributes.BrightnessPct)
mergef(&state.Attributes.WhiteValue, &newState.Attributes.WhiteValue)
mergef(&state.Attributes.NextDawn, &newState.Attributes.NextDawn)
mergef(&state.Attributes.NextDusk, &newState.Attributes.NextDusk)
mergef(&state.Attributes.NextMidnight, &newState.Attributes.NextMidnight)
mergef(&state.Attributes.NextNoon, &newState.Attributes.NextNoon)
mergef(&state.Attributes.NextRising, &newState.Attributes.NextRising)
mergef(&state.Attributes.NextSetting, &newState.Attributes.NextSetting)
mergef(&state.Attributes.Elevation, &newState.Attributes.Elevation)
mergef(&state.Attributes.Azimuth, &newState.Attributes.Azimuth)
mergef(&state.Attributes.Rising, &newState.Attributes.Rising)
mergef(&state.Attributes.FriendlyName, &newState.Attributes.FriendlyName)
mergef(&state.Attributes.Source, &newState.Attributes.Source)
mergef(&state.Attributes.Transition, &newState.Attributes.Transition)
state.Context = newState.Context
}
*/
func OrZero[T comparable](value *T) T {
if value != nil {
return *value
} else {
return *new(T)
}
}