make an interface for State object

This commit is contained in:
Sam Lewis
2023-10-22 18:16:20 -04:00
parent 824d6c12d1
commit 1932f1edf7
8 changed files with 30 additions and 30 deletions

6
app.go
View File

@@ -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
}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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")
}

View File

@@ -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

View File

@@ -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

View File

@@ -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...)
}