From e86eee39842e40bba4f1a0889658971951fef41e Mon Sep 17 00:00:00 2001 From: Jordan Hotmann Date: Mon, 16 Oct 2023 10:54:09 -0600 Subject: [PATCH] Get full HA state --- internal/api/api.go | 17 ++++++++++++----- internal/nats/timers.go | 1 + pkg/client/client.go | 18 ++++++++++++++++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/internal/api/api.go b/internal/api/api.go index ac7831a..ad9a9c3 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -71,11 +71,14 @@ func logRequest(w http.ResponseWriter, r *http.Request) { func getEntityStateHandler(w http.ResponseWriter, r *http.Request) { logRequest(w, r) entityId := chi.URLParam(r, "entityId") + full := r.URL.Query().Get("full") == "true" - kvVal, err := nats.GetKeyValue(fmt.Sprintf("%s.%s", HA_STATE_PREFIX, entityId)) - if err == nil && len(kvVal) > 0 { - w.Write(kvVal) - return + if !full { + kvVal, err := nats.GetKeyValue(fmt.Sprintf("%s.%s", HA_STATE_PREFIX, entityId)) + if err == nil && len(kvVal) > 0 { + w.Write(kvVal) + return + } } data, err := haClient.GetState(entityId) @@ -85,7 +88,11 @@ func getEntityStateHandler(w http.ResponseWriter, r *http.Request) { } nats.SetKeyValueString(fmt.Sprintf("%s.%s", HA_STATE_PREFIX, entityId), data.State) - render.PlainText(w, r, data.State) + if full { + render.JSON(w, r, data) + } else { + render.PlainText(w, r, data.State) + } } func setEntityStateHandler(w http.ResponseWriter, r *http.Request) { diff --git a/internal/nats/timers.go b/internal/nats/timers.go index 7981124..1d554cc 100644 --- a/internal/nats/timers.go +++ b/internal/nats/timers.go @@ -120,6 +120,7 @@ func WatchTimers() { timer, err := GetTimer(timerName) if err != nil { logger.Error("Error retrieving timer", "timer", timerName, "error", err) + timer.Cancel() continue } diff --git a/pkg/client/client.go b/pkg/client/client.go index edd4228..d8d1974 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -4,7 +4,7 @@ import ( "fmt" "code.jhot.me/jhot/hats/internal/api" - "code.jhot.me/jhot/hats/pkg/homeassistant" + ha "code.jhot.me/jhot/hats/pkg/homeassistant" "github.com/go-resty/resty/v2" ) @@ -19,6 +19,20 @@ func NewHatsClient(baseUrl string) *HatsClient { } } +func (c *HatsClient) GetStateFull(entityId string) (ha.StateData, error) { + var data ha.StateData + resp, err := c.client.R().SetResult(&data).SetQueryParam("full", "true").Get(fmt.Sprintf("api/state/%s", entityId)) + if err == nil && !resp.IsSuccess() { + err = fmt.Errorf("%d status code received: %s", resp.StatusCode(), resp.String()) + } + + if err != nil { + return data, err + } + + return data, nil +} + func (c *HatsClient) GetState(entityId string) (string, error) { resp, err := c.client.R().Get(fmt.Sprintf("api/state/%s", entityId)) if err == nil && !resp.IsSuccess() { @@ -39,7 +53,7 @@ func (c *HatsClient) GetStateBool(entityId string) (bool, error) { return false, err } - return homeassistant.StateToBool(stateString), nil + return ha.StateToBool(stateString), nil } func (c *HatsClient) CallService(entityId string, service string, extras ...map[string]string) error {