From ffd1ff689ba9e884aa8f12d047ede9a3bc28a4fa Mon Sep 17 00:00:00 2001 From: Jordan Hotmann Date: Fri, 17 Nov 2023 12:15:15 -0700 Subject: [PATCH] get state logging --- internal/api/api.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/api/api.go b/internal/api/api.go index b501f0e..f3ce939 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -123,17 +123,22 @@ func loggerMiddleware(next http.Handler) http.Handler { func getEntityStateHandler(w http.ResponseWriter, r *http.Request) { entityId := chi.URLParam(r, "entityId") full := r.URL.Query().Get("full") == "true" + l := logger.With("endpoint", "GET /api/state/{entityId}", "entityId", entityId, "full", full) if !full { + l.Debug("Getting state from KV store") kvVal, err := nats.GetKeyValue(fmt.Sprintf("%s.%s", HA_STATE_PREFIX, entityId)) if err == nil && len(kvVal) > 0 { - w.Write(kvVal) + l.Debug("Returning", "value", string(kvVal)) + render.PlainText(w, r, string(kvVal)) return } } + l.Debug("Getting state from Home Assistant") data, err := haClient.GetState(entityId) if err != nil { + l.Error("Error getting state from Home Assistant", "error", err) http.Error(w, err.Error(), http.StatusInternalServerError) return }