From 1932f1edf7d945bb2bbaf4697a47bb7f6c6e5882 Mon Sep 17 00:00:00 2001 From: Sam Lewis Date: Sun, 22 Oct 2023 18:16:20 -0400 Subject: [PATCH] make an interface for State object --- app.go | 6 +++--- checkers.go | 4 ++-- entitylistener.go | 2 +- eventListener.go | 2 +- example/example.go | 8 ++++---- interval.go | 2 +- schedule.go | 2 +- state.go | 34 +++++++++++++++++----------------- 8 files changed, 30 insertions(+), 30 deletions(-) diff --git a/app.go b/app.go index 3497c53..e7db2fe 100644 --- a/app.go +++ b/app.go @@ -29,7 +29,7 @@ type App struct { httpClient *http.HttpClient service *Service - state *State + state *StateImpl schedules pq.PriorityQueue intervals pq.PriorityQueue @@ -192,7 +192,7 @@ func (a *App) RegisterEventListeners(evls ...EventListener) { } } -func getSunriseSunset(s *State, sunrise bool, dateToUse carbon.Carbon, offset ...DurationString) carbon.Carbon { +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() @@ -291,6 +291,6 @@ func (a *App) GetService() *Service { return a.service } -func (a *App) GetState() *State { +func (a *App) GetState() State { return a.state } diff --git a/checkers.go b/checkers.go index bb2291c..1936190 100644 --- a/checkers.go +++ b/checkers.go @@ -86,7 +86,7 @@ func checkExceptionRanges(eList []timeRange) conditionCheck { return cc } -func checkEnabledEntity(s *State, infos []internal.EnabledDisabledInfo) conditionCheck { +func checkEnabledEntity(s State, infos []internal.EnabledDisabledInfo) conditionCheck { cc := conditionCheck{fail: false} if len(infos) == 0 { return cc @@ -114,7 +114,7 @@ func checkEnabledEntity(s *State, infos []internal.EnabledDisabledInfo) conditio return cc } -func checkDisabledEntity(s *State, infos []internal.EnabledDisabledInfo) conditionCheck { +func checkDisabledEntity(s State, infos []internal.EnabledDisabledInfo) conditionCheck { cc := conditionCheck{fail: false} if len(infos) == 0 { return cc diff --git a/entitylistener.go b/entitylistener.go index c5063c6..2dd3a3a 100644 --- a/entitylistener.go +++ b/entitylistener.go @@ -33,7 +33,7 @@ type EntityListener struct { disabledEntities []internal.EnabledDisabledInfo } -type EntityListenerCallback func(*Service, *State, EntityData) +type EntityListenerCallback func(*Service, State, EntityData) type EntityData struct { TriggerEntityId string diff --git a/eventListener.go b/eventListener.go index d5716ed..37fe98a 100644 --- a/eventListener.go +++ b/eventListener.go @@ -25,7 +25,7 @@ type EventListener struct { disabledEntities []internal.EnabledDisabledInfo } -type EventListenerCallback func(*Service, *State, EventData) +type EventListenerCallback func(*Service, State, EventData) type EventData struct { Type string diff --git a/example/example.go b/example/example.go index f2599d7..8d3dc05 100644 --- a/example/example.go +++ b/example/example.go @@ -54,7 +54,7 @@ func main() { } -func pantryLights(service *ga.Service, state *ga.State, sensor ga.EntityData) { +func pantryLights(service *ga.Service, state ga.State, sensor ga.EntityData) { l := "light.pantry" if sensor.ToState == "on" { service.HomeAssistant.TurnOn(l) @@ -63,7 +63,7 @@ func pantryLights(service *ga.Service, state *ga.State, sensor ga.EntityData) { } } -func onEvent(service *ga.Service, state *ga.State, data ga.EventData) { +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 @@ -74,7 +74,7 @@ func onEvent(service *ga.Service, state *ga.State, data ga.EventData) { log.Default().Println(ev) } -func lightsOut(service *ga.Service, state *ga.State) { +func lightsOut(service *ga.Service, state ga.State) { // always turn off outside lights service.Light.TurnOff("light.outside_lights") s, err := state.Get("binary_sensor.living_room_motion") @@ -89,7 +89,7 @@ func lightsOut(service *ga.Service, state *ga.State) { } } -func sunriseSched(service *ga.Service, state *ga.State) { +func sunriseSched(service *ga.Service, state ga.State) { service.Light.TurnOn("light.living_room_lamps") service.Light.TurnOff("light.christmas_lights") } diff --git a/interval.go b/interval.go index 23f187b..3d525d9 100644 --- a/interval.go +++ b/interval.go @@ -7,7 +7,7 @@ import ( "saml.dev/gome-assistant/internal" ) -type IntervalCallback func(*Service, *State) +type IntervalCallback func(*Service, State) type Interval struct { frequency time.Duration diff --git a/schedule.go b/schedule.go index a194fbd..2325890 100644 --- a/schedule.go +++ b/schedule.go @@ -9,7 +9,7 @@ import ( "saml.dev/gome-assistant/internal" ) -type ScheduleCallback func(*Service, *State) +type ScheduleCallback func(*Service, State) type DailySchedule struct { // 0-23 diff --git a/state.go b/state.go index 2eb0ad0..edc9c91 100644 --- a/state.go +++ b/state.go @@ -10,17 +10,17 @@ import ( "saml.dev/gome-assistant/internal/http" ) -type StateInterface interface { - AfterSunrise() bool - BeforeSunrise() bool - AfterSunset() bool - BeforeSunset() bool - Get() (EntityState, error) - Equals() (bool, error) +type State interface { + AfterSunrise(...DurationString) bool + BeforeSunrise(...DurationString) bool + AfterSunset(...DurationString) bool + BeforeSunset(...DurationString) bool + Get(entityId string) (EntityState, error) + Equals(entityId, state string) (bool, error) } // State is used to retrieve state from Home Assistant. -type State struct { +type StateImpl struct { httpClient *http.HttpClient latitude float64 longitude float64 @@ -33,8 +33,8 @@ type EntityState struct { LastChanged time.Time `json:"last_changed"` } -func newState(c *http.HttpClient, homeZoneEntityId string) (*State, error) { - state := &State{httpClient: c} +func newState(c *http.HttpClient, homeZoneEntityId string) (*StateImpl, error) { + state := &StateImpl{httpClient: c} err := state.getLatLong(c, homeZoneEntityId) if err != nil { return nil, err @@ -42,7 +42,7 @@ func newState(c *http.HttpClient, homeZoneEntityId string) (*State, error) { return state, nil } -func (s *State) getLatLong(c *http.HttpClient, homeZoneEntityId string) error { +func (s *StateImpl) getLatLong(c *http.HttpClient, homeZoneEntityId string) error { resp, err := s.Get(homeZoneEntityId) if err != nil { return fmt.Errorf("couldn't get latitude/longitude from home assistant entity '%s'. Did you type it correctly? It should be a zone like 'zone.home'", homeZoneEntityId) @@ -63,7 +63,7 @@ func (s *State) getLatLong(c *http.HttpClient, homeZoneEntityId string) error { return nil } -func (s *State) Get(entityId string) (EntityState, error) { +func (s *StateImpl) Get(entityId string) (EntityState, error) { resp, err := s.httpClient.GetState(entityId) if err != nil { return EntityState{}, err @@ -73,7 +73,7 @@ func (s *State) Get(entityId string) (EntityState, error) { return es, nil } -func (s *State) Equals(entityId string, expectedState string) (bool, error) { +func (s *StateImpl) Equals(entityId string, expectedState string) (bool, error) { currentState, err := s.Get(entityId) if err != nil { return false, err @@ -81,20 +81,20 @@ func (s *State) Equals(entityId string, expectedState string) (bool, error) { return currentState.State == expectedState, nil } -func (s *State) BeforeSunrise(offset ...DurationString) bool { +func (s *StateImpl) BeforeSunrise(offset ...DurationString) bool { sunrise := getSunriseSunset(s /* sunrise = */, true, carbon.Now(), offset...) return carbon.Now().Lt(sunrise) } -func (s *State) AfterSunrise(offset ...DurationString) bool { +func (s *StateImpl) AfterSunrise(offset ...DurationString) bool { return !s.BeforeSunrise(offset...) } -func (s *State) BeforeSunset(offset ...DurationString) bool { +func (s *StateImpl) BeforeSunset(offset ...DurationString) bool { sunset := getSunriseSunset(s /* sunrise = */, false, carbon.Now(), offset...) return carbon.Now().Lt(sunset) } -func (s *State) AfterSunset(offset ...DurationString) bool { +func (s *StateImpl) AfterSunset(offset ...DurationString) bool { return !s.BeforeSunset(offset...) }