diff --git a/home_assistant/client.go b/home_assistant/client.go new file mode 100644 index 0000000..0406222 --- /dev/null +++ b/home_assistant/client.go @@ -0,0 +1,40 @@ +package home_assistant + +import ( + "context" + "github.com/carlmjohnson/requests" + "net/http" +) + +type Config struct { + Token string + BaseURL string +} + +type Client struct { + config Config + client *http.Client +} + +func New(client *http.Client, config Config) *Client { + return &Client{config: config, client: client} +} + +func (c *Client) GetState(ctx context.Context, entityID string) (string, error) { + type stateResponse struct { + State string `json:"state"` + } + + var resp stateResponse + + err := requests.URL(c.config.BaseURL). + Header("Authorization", "Bearer "+c.config.Token). + Pathf("/api/states/%s", entityID). + ToJSON(&resp). + Fetch(ctx) + if err != nil { + return "", err + } + + return resp.State, nil +} diff --git a/main.go b/main.go index 7d15a95..4e727cb 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "context" "github.com/Crocmagnon/display-epaper/fete" + "github.com/Crocmagnon/display-epaper/home_assistant" "github.com/Crocmagnon/display-epaper/transports" "github.com/Crocmagnon/display-epaper/weather" "github.com/golang/freetype/truetype" @@ -57,6 +58,11 @@ func main() { log.Printf("sleep duration: %v\n", sleep) + hassClient := home_assistant.New(nil, home_assistant.Config{ + Token: os.Getenv("HOME_ASSISTANT_TOKEN"), + BaseURL: os.Getenv("HOME_ASSISTANT_BASE_URL"), + }) + if err := run( ctx, sleep, @@ -64,6 +70,7 @@ func main() { transportsClient, feteClient, weatherClient, + hassClient, ); err != nil { log.Fatal("error: ", err) } diff --git a/run_darwin_arm64.go b/run_darwin_arm64.go index b764ac0..266a48f 100644 --- a/run_darwin_arm64.go +++ b/run_darwin_arm64.go @@ -3,6 +3,7 @@ package main import ( "context" "github.com/Crocmagnon/display-epaper/fete" + "github.com/Crocmagnon/display-epaper/home_assistant" "github.com/Crocmagnon/display-epaper/transports" "github.com/Crocmagnon/display-epaper/weather" "github.com/llgcode/draw2d/draw2dimg" @@ -17,6 +18,7 @@ func run( transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.Client, + _ *home_assistant.Client, ) error { img, err := getImg( ctx, diff --git a/run_linux_arm64.go b/run_linux_arm64.go index 72b69a1..8ea09e7 100644 --- a/run_linux_arm64.go +++ b/run_linux_arm64.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/Crocmagnon/display-epaper/epd" "github.com/Crocmagnon/display-epaper/fete" + "github.com/Crocmagnon/display-epaper/home_assistant" "github.com/Crocmagnon/display-epaper/transports" "github.com/Crocmagnon/display-epaper/weather" "image" @@ -21,6 +22,7 @@ func run( transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.Client, + hassClient *home_assistant.Client, ) error { _, err := host.Init() if err != nil { @@ -52,6 +54,7 @@ func run( transportsClient, feteClient, weatherClient, + hassClient, ) if err != nil { log.Printf("error looping: %v\n", err) @@ -72,7 +75,12 @@ func loop( transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.Client, + hassClient *home_assistant.Client, ) (image.Image, error) { + if !shouldRun(ctx, hassClient) { + return currentImg, nil + } + img, err := getImg( ctx, time.Now, @@ -108,6 +116,24 @@ func loop( return img, nil } +func shouldRun(ctx context.Context, hassClient *home_assistant.Client) bool { + dayNight, err := hassClient.GetState(ctx, "input_select.house_day_night") + if err != nil { + log.Printf("error getting day night: %v ; running\n", err) + return true + } + + presentAway, err := hassClient.GetState(ctx, "input_select.house_present_away") + if err != nil { + log.Printf("error getting day night: %v ; running\n", err) + return true + } + + res := dayNight == "day" && presentAway == "present" + log.Printf("running: %v\n", res) + return res +} + const filename = "/perm/display-epaper-lastFullRefresh" func initDisplay(display *epd.EPD, threshold time.Duration) error {