mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 15:15:14 -06:00
make some things private
This commit is contained in:
4
app.go
4
app.go
@@ -43,7 +43,7 @@ func NewApp(connString string) app {
|
||||
httpClient := http.NewHttpClient(connString, token)
|
||||
|
||||
service := NewService(conn, ctx, httpClient)
|
||||
state := NewState(httpClient)
|
||||
state := newState(httpClient)
|
||||
|
||||
return app{
|
||||
conn: conn,
|
||||
@@ -159,7 +159,7 @@ func carbon2TimeString(c carbon.Carbon) string {
|
||||
|
||||
func (a *app) Start() {
|
||||
// schedules
|
||||
go RunSchedules(a)
|
||||
go runSchedules(a)
|
||||
|
||||
// subscribe to state_changed events
|
||||
id := internal.GetId()
|
||||
|
||||
@@ -138,16 +138,16 @@ func callEntityListeners(app *app, msgBytes []byte) {
|
||||
|
||||
for _, l := range listeners {
|
||||
// Check conditions
|
||||
if c := CheckWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail {
|
||||
if c := checkWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail {
|
||||
return
|
||||
}
|
||||
if c := CheckStatesMatch(l.fromState, data.OldState.State); c.fail {
|
||||
if c := checkStatesMatch(l.fromState, data.OldState.State); c.fail {
|
||||
return
|
||||
}
|
||||
if c := CheckStatesMatch(l.toState, data.NewState.State); c.fail {
|
||||
if c := checkStatesMatch(l.toState, data.NewState.State); c.fail {
|
||||
return
|
||||
}
|
||||
if c := CheckThrottle(l.throttle, l.lastRan); c.fail {
|
||||
if c := checkThrottle(l.throttle, l.lastRan); c.fail {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -105,10 +105,10 @@ func callEventListeners(app *app, msg ws.ChanMsg) {
|
||||
|
||||
for _, l := range listeners {
|
||||
// Check conditions
|
||||
if c := CheckWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail {
|
||||
if c := checkWithinTimeRange(l.betweenStart, l.betweenEnd); c.fail {
|
||||
return
|
||||
}
|
||||
if c := CheckThrottle(l.throttle, l.lastRan); c.fail {
|
||||
if c := checkThrottle(l.throttle, l.lastRan); c.fail {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ type conditionCheck struct {
|
||||
fail bool
|
||||
}
|
||||
|
||||
func CheckWithinTimeRange(startTime, endTime string) conditionCheck {
|
||||
func checkWithinTimeRange(startTime, endTime string) conditionCheck {
|
||||
cc := conditionCheck{fail: false}
|
||||
// if betweenStart and betweenEnd both set, first account for midnight
|
||||
// overlap, then check if between those times.
|
||||
@@ -42,7 +42,7 @@ func CheckWithinTimeRange(startTime, endTime string) conditionCheck {
|
||||
return cc
|
||||
}
|
||||
|
||||
func CheckStatesMatch(listenerState, s string) conditionCheck {
|
||||
func checkStatesMatch(listenerState, s string) conditionCheck {
|
||||
cc := conditionCheck{fail: false}
|
||||
// check if fromState or toState are set and don't match
|
||||
if listenerState != "" && listenerState != s {
|
||||
@@ -51,7 +51,7 @@ func CheckStatesMatch(listenerState, s string) conditionCheck {
|
||||
return cc
|
||||
}
|
||||
|
||||
func CheckThrottle(throttle time.Duration, lastRan carbon.Carbon) conditionCheck {
|
||||
func checkThrottle(throttle time.Duration, lastRan carbon.Carbon) conditionCheck {
|
||||
cc := conditionCheck{fail: false}
|
||||
// check if Throttle is set and that duration hasn't passed since lastRan
|
||||
if throttle.Seconds() > 0 &&
|
||||
|
||||
@@ -138,7 +138,7 @@ func getFunctionName(i interface{}) string {
|
||||
}
|
||||
|
||||
// app.Start() functions
|
||||
func RunSchedules(a *app) {
|
||||
func runSchedules(a *app) {
|
||||
if a.schedules.Len() == 0 {
|
||||
return
|
||||
}
|
||||
@@ -167,6 +167,11 @@ func popSchedule(a *app) schedule {
|
||||
}
|
||||
|
||||
func requeueSchedule(a *app, s schedule) {
|
||||
// TODO: figure out how to handle sunset/sunrise in here. Maybe just
|
||||
// add sunrise bool and sunset bool to Schedule, might have to change
|
||||
// API to be .Call().Sunset("1h") instead of .Call().At(ga.Sunset("1h"))
|
||||
// then that function could easily set the flag. Kinda ruins the english
|
||||
// language sentence structure but maybe simplest way to get it working
|
||||
s.realStartTime = s.realStartTime.Add(s.frequency)
|
||||
a.schedules.Insert(s, float64(s.realStartTime.Unix()))
|
||||
}
|
||||
|
||||
3
state.go
3
state.go
@@ -17,10 +17,9 @@ type EntityState struct {
|
||||
State string `json:"state"`
|
||||
Attributes map[string]any `json:"attributes"`
|
||||
LastChanged time.Time `json:"last_changed"`
|
||||
LastUpdated time.Time `json:"last_updated"`
|
||||
}
|
||||
|
||||
func NewState(c *http.HttpClient) *State {
|
||||
func newState(c *http.HttpClient) *State {
|
||||
return &State{httpClient: c}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user