parent
5473e80463
commit
57e107b226
|
@ -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.
|
|
@ -12,6 +12,15 @@
|
||||||
- [Syncthing](https://github.com/syncthing/syncthing)
|
- [Syncthing](https://github.com/syncthing/syncthing)
|
||||||
- [National Weather Service](https://www.weather.gov/)
|
- [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
|
## Example Client
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
|
|
|
@ -316,29 +316,10 @@ func postNtfyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
func postCommandHandler(w http.ResponseWriter, r *http.Request) {
|
func postCommandHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
commandName := chi.URLParam(r, "commandName")
|
commandName := chi.URLParam(r, "commandName")
|
||||||
|
|
||||||
switch commandName {
|
body, err := io.ReadAll(r.Body)
|
||||||
// Commands without payloads
|
if err != nil {
|
||||||
case "bedtime":
|
logger.Error("Error reading request body", "error", err, "url", r.URL.String())
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
nats.Publish(fmt.Sprintf("command.%s", commandName), body)
|
||||||
|
render.PlainText(w, r, "OK")
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ var (
|
||||||
const (
|
const (
|
||||||
stateChangeEventId = 1001
|
stateChangeEventId = 1001
|
||||||
zhaEventId = 1002
|
zhaEventId = 1002
|
||||||
qrEventId = 1003
|
nfcEventId = 1003
|
||||||
timerFinishedEventId = 1005
|
timerFinishedEventId = 1005
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ func handleMessages() {
|
||||||
haWebsocketConn.WriteJSON(ha.SubscribeEventsMessage{
|
haWebsocketConn.WriteJSON(ha.SubscribeEventsMessage{
|
||||||
Type: ha.MessageType.SubscribeEvents,
|
Type: ha.MessageType.SubscribeEvents,
|
||||||
EventType: ha.MessageType.TagScanned,
|
EventType: ha.MessageType.TagScanned,
|
||||||
Id: qrEventId})
|
Id: nfcEventId})
|
||||||
haWebsocketConn.WriteJSON(ha.SubscribeEventsMessage{
|
haWebsocketConn.WriteJSON(ha.SubscribeEventsMessage{
|
||||||
Type: ha.MessageType.SubscribeEvents,
|
Type: ha.MessageType.SubscribeEvents,
|
||||||
EventType: ha.MessageType.TimerFinished,
|
EventType: ha.MessageType.TimerFinished,
|
||||||
|
@ -139,9 +139,9 @@ func handleMessages() {
|
||||||
case zhaEventId:
|
case zhaEventId:
|
||||||
data, _ := json.Marshal(message.Event.Data)
|
data, _ := json.Marshal(message.Event.Data)
|
||||||
nats.Publish(fmt.Sprintf("homeassistant.zha.%s", message.Event.Data.DeviceIeee), data)
|
nats.Publish(fmt.Sprintf("homeassistant.zha.%s", message.Event.Data.DeviceIeee), data)
|
||||||
case qrEventId:
|
case nfcEventId:
|
||||||
data, _ := json.Marshal(message.Event.Data)
|
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:
|
case timerFinishedEventId:
|
||||||
nats.PublishString(fmt.Sprintf("homeassistant.%s.finished", message.Event.Data.EntityId), "finished")
|
nats.PublishString(fmt.Sprintf("homeassistant.%s.finished", message.Event.Data.EntityId), "finished")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue