From fc1e13ad67dbf832a97dcf8b0c64e8076b140e67 Mon Sep 17 00:00:00 2001 From: Jordan Hotmann Date: Mon, 27 Nov 2023 15:59:35 -0700 Subject: [PATCH] zwave events --- README.md | 1 + internal/homeassistant/subscriber.go | 8 ++++ pkg/homeassistant/structs.go | 67 +++++++++++++++++++--------- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index b2b153b..4c61309 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ - `homeassistant.states.{domain}.{entity}.{state}` - Home Assistant device state changes: [payload](https://www.home-assistant.io/docs/configuration/state_object/) - `homeassistant.attributes.{domain}.{entity}.{state}` - When a device's attributes change but the state hasn't changed: [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.zwave-scene.{device ID}` - ZwaveJS scene events: [payload](https://www.home-assistant.io/integrations/zwave_js/#scene-events-value-notification) - `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"` diff --git a/internal/homeassistant/subscriber.go b/internal/homeassistant/subscriber.go index 6c86278..4d9c3d2 100644 --- a/internal/homeassistant/subscriber.go +++ b/internal/homeassistant/subscriber.go @@ -25,6 +25,7 @@ const ( stateChangeEventId = 1001 zhaEventId = 1002 nfcEventId = 1003 + zwaveEventId = 1004 timerFinishedEventId = 1005 ) @@ -116,6 +117,10 @@ func handleMessages() { Type: ha.MessageType.SubscribeEvents, EventType: ha.MessageType.TagScanned, Id: nfcEventId}) + haWebsocketConn.WriteJSON(ha.SubscribeEventsMessage{ + Type: ha.MessageType.SubscribeEvents, + EventType: ha.MessageType.ZwaveEvent, + Id: zwaveEventId}) haWebsocketConn.WriteJSON(ha.SubscribeEventsMessage{ Type: ha.MessageType.SubscribeEvents, EventType: ha.MessageType.TimerFinished, @@ -147,6 +152,9 @@ func handleMessages() { case nfcEventId: data, _ := json.Marshal(message.Event.Data) nats.Publish(fmt.Sprintf("homeassistant.nfc.%s", message.Event.Data.TagId), data) + case zwaveEventId: + data, _ := json.Marshal(message.Event.Data) + nats.Publish(fmt.Sprintf("homeassistant.zwave-scene.%s", message.Event.Data.DeviceId), data) case timerFinishedEventId: nats.PublishString(fmt.Sprintf("homeassistant.%s.finished", message.Event.Data.EntityId), "finished") } diff --git a/pkg/homeassistant/structs.go b/pkg/homeassistant/structs.go index 4550c5e..8b32b05 100644 --- a/pkg/homeassistant/structs.go +++ b/pkg/homeassistant/structs.go @@ -8,6 +8,7 @@ var MessageType = struct { Result string Event string ZhaEvent string + ZwaveEvent string SubscribeEvents string StateChanged string TagScanned string @@ -20,6 +21,7 @@ var MessageType = struct { Result: "result", Event: "event", ZhaEvent: "zha_event", + ZwaveEvent: "zwave_js_value_notification", SubscribeEvents: "subscribe_events", StateChanged: "state_changed", TagScanned: "tag_scanned", @@ -83,25 +85,27 @@ var Services = struct { // Extra props that can be sent when calling a Home Assistant service var ExtraProps = struct { - Transition string - Brightness string - BrightnessPercent string - HvacMode string - Temperature string - TargetTempHigh string - TargetTempLow string - Duration string - Value string + Transition string + Brightness string + BrightnessPercent string + BrightnessPercentStep string + HvacMode string + Temperature string + TargetTempHigh string + TargetTempLow string + Duration string + Value string }{ - Transition: "transition", - Brightness: "brightness", - BrightnessPercent: "brightness_pct", - HvacMode: "hvac_mode", - Temperature: "temperature", - TargetTempHigh: "target_temp_high", - TargetTempLow: "target_temp_low", - Duration: "duration", - Value: "value", + Transition: "transition", + Brightness: "brightness", + BrightnessPercent: "brightness_pct", + BrightnessPercentStep: "brightness_step_pct", + HvacMode: "hvac_mode", + Temperature: "temperature", + TargetTempHigh: "target_temp_high", + TargetTempLow: "target_temp_low", + Duration: "duration", + Value: "value", } type ResultContext struct { @@ -121,15 +125,34 @@ type StateData struct { } type EventData struct { - EntityId string `json:"entity_id,omitempty"` - NewState StateData `json:"new_state,omitempty"` - OldState StateData `json:"old_state,omitempty"` + EntityId string `json:"entity_id,omitempty"` + NewState StateData `json:"new_state,omitempty"` + OldState StateData `json:"old_state,omitempty"` + + // ZHA DeviceIeee string `json:"device_ieee,omitempty"` DeviceId string `json:"device_id,omitempty"` Command string `json:"command,omitempty"` Args interface{} `json:"args,omitempty"` Params interface{} `json:"params,omitempty"` - TagId string `json:"tag_id,omitempty"` + + // NFC + TagId string `json:"tag_id,omitempty"` + + // ZwaveJS Scene + Domain string `json:"domain,omitempty"` + NodeID int `json:"node_id,omitempty"` + HomeID string `json:"home_id,omitempty"` + Endpoint int `json:"endpoint,omitempty"` + CommandClass int `json:"command_class,omitempty"` + CommandClassName string `json:"command_class_name,omitempty"` + Label string `json:"label,omitempty"` + Property string `json:"property,omitempty"` + PropertyName string `json:"property_name,omitempty"` + PropertyKey string `json:"property_key,omitempty"` + PropertyKeyName string `json:"property_key_name,omitempty"` + Value string `json:"value,omitempty"` + ValueRaw int `json:"value_raw,omitempty"` } type Event struct {