mirror of
https://github.com/Crocmagnon/display-epaper.git
synced 2024-11-25 15:38:03 +01:00
Compare commits
2 commits
df0ccc536d
...
816d0e53d0
Author | SHA1 | Date | |
---|---|---|---|
816d0e53d0 | |||
b7a8a9850a |
4 changed files with 86 additions and 10 deletions
40
home_assistant/client.go
Normal file
40
home_assistant/client.go
Normal file
|
@ -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
|
||||||
|
}
|
7
main.go
7
main.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/Crocmagnon/display-epaper/fete"
|
"github.com/Crocmagnon/display-epaper/fete"
|
||||||
|
"github.com/Crocmagnon/display-epaper/home_assistant"
|
||||||
"github.com/Crocmagnon/display-epaper/transports"
|
"github.com/Crocmagnon/display-epaper/transports"
|
||||||
"github.com/Crocmagnon/display-epaper/weather"
|
"github.com/Crocmagnon/display-epaper/weather"
|
||||||
"github.com/golang/freetype/truetype"
|
"github.com/golang/freetype/truetype"
|
||||||
|
@ -57,6 +58,11 @@ func main() {
|
||||||
|
|
||||||
log.Printf("sleep duration: %v\n", sleep)
|
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(
|
if err := run(
|
||||||
ctx,
|
ctx,
|
||||||
sleep,
|
sleep,
|
||||||
|
@ -64,6 +70,7 @@ func main() {
|
||||||
transportsClient,
|
transportsClient,
|
||||||
feteClient,
|
feteClient,
|
||||||
weatherClient,
|
weatherClient,
|
||||||
|
hassClient,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
log.Fatal("error: ", err)
|
log.Fatal("error: ", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/Crocmagnon/display-epaper/fete"
|
"github.com/Crocmagnon/display-epaper/fete"
|
||||||
|
"github.com/Crocmagnon/display-epaper/home_assistant"
|
||||||
"github.com/Crocmagnon/display-epaper/transports"
|
"github.com/Crocmagnon/display-epaper/transports"
|
||||||
"github.com/Crocmagnon/display-epaper/weather"
|
"github.com/Crocmagnon/display-epaper/weather"
|
||||||
"github.com/llgcode/draw2d/draw2dimg"
|
"github.com/llgcode/draw2d/draw2dimg"
|
||||||
|
@ -17,6 +18,7 @@ func run(
|
||||||
transportsClient *transports.Client,
|
transportsClient *transports.Client,
|
||||||
feteClient *fete.Client,
|
feteClient *fete.Client,
|
||||||
weatherClient *weather.Client,
|
weatherClient *weather.Client,
|
||||||
|
_ *home_assistant.Client,
|
||||||
) error {
|
) error {
|
||||||
img, err := getImg(
|
img, err := getImg(
|
||||||
ctx,
|
ctx,
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Crocmagnon/display-epaper/epd"
|
"github.com/Crocmagnon/display-epaper/epd"
|
||||||
"github.com/Crocmagnon/display-epaper/fete"
|
"github.com/Crocmagnon/display-epaper/fete"
|
||||||
|
"github.com/Crocmagnon/display-epaper/home_assistant"
|
||||||
"github.com/Crocmagnon/display-epaper/transports"
|
"github.com/Crocmagnon/display-epaper/transports"
|
||||||
"github.com/Crocmagnon/display-epaper/weather"
|
"github.com/Crocmagnon/display-epaper/weather"
|
||||||
"image"
|
"image"
|
||||||
|
@ -21,6 +22,7 @@ func run(
|
||||||
transportsClient *transports.Client,
|
transportsClient *transports.Client,
|
||||||
feteClient *fete.Client,
|
feteClient *fete.Client,
|
||||||
weatherClient *weather.Client,
|
weatherClient *weather.Client,
|
||||||
|
hassClient *home_assistant.Client,
|
||||||
) error {
|
) error {
|
||||||
_, err := host.Init()
|
_, err := host.Init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -52,6 +54,7 @@ func run(
|
||||||
transportsClient,
|
transportsClient,
|
||||||
feteClient,
|
feteClient,
|
||||||
weatherClient,
|
weatherClient,
|
||||||
|
hassClient,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error looping: %v\n", err)
|
log.Printf("error looping: %v\n", err)
|
||||||
|
@ -72,8 +75,13 @@ func loop(
|
||||||
transportsClient *transports.Client,
|
transportsClient *transports.Client,
|
||||||
feteClient *fete.Client,
|
feteClient *fete.Client,
|
||||||
weatherClient *weather.Client,
|
weatherClient *weather.Client,
|
||||||
|
hassClient *home_assistant.Client,
|
||||||
) (image.Image, error) {
|
) (image.Image, error) {
|
||||||
img, err := getImg(
|
var img image.Image = image.White
|
||||||
|
|
||||||
|
if shouldDisplay(ctx, hassClient) {
|
||||||
|
var err error
|
||||||
|
img, err = getImg(
|
||||||
ctx,
|
ctx,
|
||||||
time.Now,
|
time.Now,
|
||||||
transportsClient,
|
transportsClient,
|
||||||
|
@ -83,6 +91,7 @@ func loop(
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("getting black: %w", err)
|
return nil, fmt.Errorf("getting black: %w", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if imgEqual(currentImg, img, epd.Width, epd.Height) {
|
if imgEqual(currentImg, img, epd.Width, epd.Height) {
|
||||||
log.Println("Images are equal, doing nothing.")
|
log.Println("Images are equal, doing nothing.")
|
||||||
|
@ -95,7 +104,7 @@ func loop(
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err = initDisplay(display, initFastThreshold)
|
err := initDisplay(display, initFastThreshold)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("initializing display: %w", err)
|
return nil, fmt.Errorf("initializing display: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -108,6 +117,24 @@ func loop(
|
||||||
return img, nil
|
return img, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func shouldDisplay(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 ; displaying anyway\n", err)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
presentAway, err := hassClient.GetState(ctx, "input_select.house_present_away")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error getting day night: %v ; displaying anyway\n", err)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
res := dayNight == "day" && presentAway == "present"
|
||||||
|
log.Printf("shouldDisplay: %v\n", res)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
const filename = "/perm/display-epaper-lastFullRefresh"
|
const filename = "/perm/display-epaper-lastFullRefresh"
|
||||||
|
|
||||||
func initDisplay(display *epd.EPD, threshold time.Duration) error {
|
func initDisplay(display *epd.EPD, threshold time.Duration) error {
|
||||||
|
|
Loading…
Reference in a new issue