mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-11 02:07:26 -06:00
make an interface for State object
This commit is contained in:
6
app.go
6
app.go
@@ -29,7 +29,7 @@ type App struct {
|
|||||||
httpClient *http.HttpClient
|
httpClient *http.HttpClient
|
||||||
|
|
||||||
service *Service
|
service *Service
|
||||||
state *State
|
state *StateImpl
|
||||||
|
|
||||||
schedules pq.PriorityQueue
|
schedules pq.PriorityQueue
|
||||||
intervals 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()
|
date := dateToUse.Carbon2Time()
|
||||||
rise, set := sunriseLib.SunriseSunset(s.latitude, s.longitude, date.Year(), date.Month(), date.Day())
|
rise, set := sunriseLib.SunriseSunset(s.latitude, s.longitude, date.Year(), date.Month(), date.Day())
|
||||||
rise, set = rise.Local(), set.Local()
|
rise, set = rise.Local(), set.Local()
|
||||||
@@ -291,6 +291,6 @@ func (a *App) GetService() *Service {
|
|||||||
return a.service
|
return a.service
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) GetState() *State {
|
func (a *App) GetState() State {
|
||||||
return a.state
|
return a.state
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ func checkExceptionRanges(eList []timeRange) conditionCheck {
|
|||||||
return cc
|
return cc
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkEnabledEntity(s *State, infos []internal.EnabledDisabledInfo) conditionCheck {
|
func checkEnabledEntity(s State, infos []internal.EnabledDisabledInfo) conditionCheck {
|
||||||
cc := conditionCheck{fail: false}
|
cc := conditionCheck{fail: false}
|
||||||
if len(infos) == 0 {
|
if len(infos) == 0 {
|
||||||
return cc
|
return cc
|
||||||
@@ -114,7 +114,7 @@ func checkEnabledEntity(s *State, infos []internal.EnabledDisabledInfo) conditio
|
|||||||
return cc
|
return cc
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkDisabledEntity(s *State, infos []internal.EnabledDisabledInfo) conditionCheck {
|
func checkDisabledEntity(s State, infos []internal.EnabledDisabledInfo) conditionCheck {
|
||||||
cc := conditionCheck{fail: false}
|
cc := conditionCheck{fail: false}
|
||||||
if len(infos) == 0 {
|
if len(infos) == 0 {
|
||||||
return cc
|
return cc
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ type EntityListener struct {
|
|||||||
disabledEntities []internal.EnabledDisabledInfo
|
disabledEntities []internal.EnabledDisabledInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
type EntityListenerCallback func(*Service, *State, EntityData)
|
type EntityListenerCallback func(*Service, State, EntityData)
|
||||||
|
|
||||||
type EntityData struct {
|
type EntityData struct {
|
||||||
TriggerEntityId string
|
TriggerEntityId string
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ type EventListener struct {
|
|||||||
disabledEntities []internal.EnabledDisabledInfo
|
disabledEntities []internal.EnabledDisabledInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventListenerCallback func(*Service, *State, EventData)
|
type EventListenerCallback func(*Service, State, EventData)
|
||||||
|
|
||||||
type EventData struct {
|
type EventData struct {
|
||||||
Type string
|
Type string
|
||||||
|
|||||||
@@ -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"
|
l := "light.pantry"
|
||||||
if sensor.ToState == "on" {
|
if sensor.ToState == "on" {
|
||||||
service.HomeAssistant.TurnOn(l)
|
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
|
// Since the structure of the event changes depending
|
||||||
// on the event type, you can Unmarshal the raw json
|
// on the event type, you can Unmarshal the raw json
|
||||||
// into a Go type. If a type for your event doesn't
|
// 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)
|
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
|
// always turn off outside lights
|
||||||
service.Light.TurnOff("light.outside_lights")
|
service.Light.TurnOff("light.outside_lights")
|
||||||
s, err := state.Get("binary_sensor.living_room_motion")
|
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.TurnOn("light.living_room_lamps")
|
||||||
service.Light.TurnOff("light.christmas_lights")
|
service.Light.TurnOff("light.christmas_lights")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"saml.dev/gome-assistant/internal"
|
"saml.dev/gome-assistant/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IntervalCallback func(*Service, *State)
|
type IntervalCallback func(*Service, State)
|
||||||
|
|
||||||
type Interval struct {
|
type Interval struct {
|
||||||
frequency time.Duration
|
frequency time.Duration
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"saml.dev/gome-assistant/internal"
|
"saml.dev/gome-assistant/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ScheduleCallback func(*Service, *State)
|
type ScheduleCallback func(*Service, State)
|
||||||
|
|
||||||
type DailySchedule struct {
|
type DailySchedule struct {
|
||||||
// 0-23
|
// 0-23
|
||||||
|
|||||||
34
state.go
34
state.go
@@ -10,17 +10,17 @@ import (
|
|||||||
"saml.dev/gome-assistant/internal/http"
|
"saml.dev/gome-assistant/internal/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StateInterface interface {
|
type State interface {
|
||||||
AfterSunrise() bool
|
AfterSunrise(...DurationString) bool
|
||||||
BeforeSunrise() bool
|
BeforeSunrise(...DurationString) bool
|
||||||
AfterSunset() bool
|
AfterSunset(...DurationString) bool
|
||||||
BeforeSunset() bool
|
BeforeSunset(...DurationString) bool
|
||||||
Get() (EntityState, error)
|
Get(entityId string) (EntityState, error)
|
||||||
Equals() (bool, error)
|
Equals(entityId, state string) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// State is used to retrieve state from Home Assistant.
|
// State is used to retrieve state from Home Assistant.
|
||||||
type State struct {
|
type StateImpl struct {
|
||||||
httpClient *http.HttpClient
|
httpClient *http.HttpClient
|
||||||
latitude float64
|
latitude float64
|
||||||
longitude float64
|
longitude float64
|
||||||
@@ -33,8 +33,8 @@ type EntityState struct {
|
|||||||
LastChanged time.Time `json:"last_changed"`
|
LastChanged time.Time `json:"last_changed"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func newState(c *http.HttpClient, homeZoneEntityId string) (*State, error) {
|
func newState(c *http.HttpClient, homeZoneEntityId string) (*StateImpl, error) {
|
||||||
state := &State{httpClient: c}
|
state := &StateImpl{httpClient: c}
|
||||||
err := state.getLatLong(c, homeZoneEntityId)
|
err := state.getLatLong(c, homeZoneEntityId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -42,7 +42,7 @@ func newState(c *http.HttpClient, homeZoneEntityId string) (*State, error) {
|
|||||||
return state, nil
|
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)
|
resp, err := s.Get(homeZoneEntityId)
|
||||||
if err != nil {
|
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)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) Get(entityId string) (EntityState, error) {
|
func (s *StateImpl) Get(entityId string) (EntityState, error) {
|
||||||
resp, err := s.httpClient.GetState(entityId)
|
resp, err := s.httpClient.GetState(entityId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return EntityState{}, err
|
return EntityState{}, err
|
||||||
@@ -73,7 +73,7 @@ func (s *State) Get(entityId string) (EntityState, error) {
|
|||||||
return es, nil
|
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)
|
currentState, err := s.Get(entityId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
@@ -81,20 +81,20 @@ func (s *State) Equals(entityId string, expectedState string) (bool, error) {
|
|||||||
return currentState.State == expectedState, nil
|
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...)
|
sunrise := getSunriseSunset(s /* sunrise = */, true, carbon.Now(), offset...)
|
||||||
return carbon.Now().Lt(sunrise)
|
return carbon.Now().Lt(sunrise)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) AfterSunrise(offset ...DurationString) bool {
|
func (s *StateImpl) AfterSunrise(offset ...DurationString) bool {
|
||||||
return !s.BeforeSunrise(offset...)
|
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...)
|
sunset := getSunriseSunset(s /* sunrise = */, false, carbon.Now(), offset...)
|
||||||
return carbon.Now().Lt(sunset)
|
return carbon.Now().Lt(sunset)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) AfterSunset(offset ...DurationString) bool {
|
func (s *StateImpl) AfterSunset(offset ...DurationString) bool {
|
||||||
return !s.BeforeSunset(offset...)
|
return !s.BeforeSunset(offset...)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user