From 57e107b226fb515842b094cd7f61b549a238598e Mon Sep 17 00:00:00 2001 From: Jordan Hotmann Date: Mon, 20 Nov 2023 12:24:33 -0700 Subject: [PATCH] License, readme update, command/tag changes --- LICENSE | 21 ++++++++++++++++++++ README.md | 9 +++++++++ internal/api/api.go | 29 +++++----------------------- internal/homeassistant/subscriber.go | 8 ++++---- 4 files changed, 39 insertions(+), 28 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9977236 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 jhot.me + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index a889c8e..52e266a 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,15 @@ - [Syncthing](https://github.com/syncthing/syncthing) - [National Weather Service](https://www.weather.gov/) +## NATS Topics + +- `homeassistant.states.{domain}.{entity}.{state}` - Home Assistant device state changes: [payload](https://www.home-assistant.io/docs/configuration/state_object/) +- `homeassistant.zha.{device IEEE}` - ZHA events: [payload](https://www.home-assistant.io/docs/configuration/state_object/) +- `homeassistant.nfc.{tag ID}` - Home Assistant NFC tag scanned: [payload](https://www.home-assistant.io/docs/configuration/state_object/) +- `homeassistant.timer.{timer name}.finished` - Home Assistant timer finished: payload is simply `"finished"` +- `schedules.{schedule name}` - HATS schedule finished: payload is simply `"finished"` +- `command.{command name}` - Command called via HATS API: payload is a byte array of the HTTP Post body + ## Example Client ```golang diff --git a/internal/api/api.go b/internal/api/api.go index 477a956..422ac89 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -316,29 +316,10 @@ func postNtfyHandler(w http.ResponseWriter, r *http.Request) { func postCommandHandler(w http.ResponseWriter, r *http.Request) { commandName := chi.URLParam(r, "commandName") - switch commandName { - // Commands without payloads - case "bedtime": - nats.PublishString(fmt.Sprintf("command.%s", commandName), "called") - render.PlainText(w, r, "OK") - return - // Commands with payloads - case "sonarr": - case "radarr": - case "paupdate": - body, err := io.ReadAll(r.Body) - if err != nil { - logger.Error("Error reading request body", "error", err, "url", r.URL.String()) - http.Error(w, "Unable to read body", http.StatusBadRequest) - return - } - nats.Publish(fmt.Sprintf("command.%s", commandName), body) - render.PlainText(w, r, "OK") - return - // Otherwise - default: - logger.Error("Command not implemented", "commandName", commandName) - http.Error(w, "Command not implemented", http.StatusNotFound) - return + body, err := io.ReadAll(r.Body) + if err != nil { + logger.Error("Error reading request body", "error", err, "url", r.URL.String()) } + nats.Publish(fmt.Sprintf("command.%s", commandName), body) + render.PlainText(w, r, "OK") } diff --git a/internal/homeassistant/subscriber.go b/internal/homeassistant/subscriber.go index 735acb7..7423423 100644 --- a/internal/homeassistant/subscriber.go +++ b/internal/homeassistant/subscriber.go @@ -24,7 +24,7 @@ var ( const ( stateChangeEventId = 1001 zhaEventId = 1002 - qrEventId = 1003 + nfcEventId = 1003 timerFinishedEventId = 1005 ) @@ -115,7 +115,7 @@ func handleMessages() { haWebsocketConn.WriteJSON(ha.SubscribeEventsMessage{ Type: ha.MessageType.SubscribeEvents, EventType: ha.MessageType.TagScanned, - Id: qrEventId}) + Id: nfcEventId}) haWebsocketConn.WriteJSON(ha.SubscribeEventsMessage{ Type: ha.MessageType.SubscribeEvents, EventType: ha.MessageType.TimerFinished, @@ -139,9 +139,9 @@ func handleMessages() { case zhaEventId: data, _ := json.Marshal(message.Event.Data) nats.Publish(fmt.Sprintf("homeassistant.zha.%s", message.Event.Data.DeviceIeee), data) - case qrEventId: + case nfcEventId: data, _ := json.Marshal(message.Event.Data) - nats.Publish(fmt.Sprintf("homeassistant.qr.%s", message.Event.Data.TagId), data) + nats.Publish(fmt.Sprintf("homeassistant.nfc.%s", message.Event.Data.TagId), data) case timerFinishedEventId: nats.PublishString(fmt.Sprintf("homeassistant.%s.finished", message.Event.Data.EntityId), "finished") }