Skip to content

Commit

Permalink
feat(hass): ✨ add support for sending events to Home Assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Oct 17, 2024
1 parent 5a327e9 commit 6debf7e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions internal/hass/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/go-resty/resty/v2"

"github.com/joshuar/go-hass-agent/internal/hass/event"
"github.com/joshuar/go-hass-agent/internal/hass/sensor"
"github.com/joshuar/go-hass-agent/internal/hass/sensor/registry"
"github.com/joshuar/go-hass-agent/internal/logging"
Expand Down Expand Up @@ -124,6 +125,21 @@ func (c *Client) HassVersion(ctx context.Context) string {
return config.Version
}

func (c *Client) ProcessEvent(ctx context.Context, details event.Event) error {
req := &request{Data: details, RequestType: requestTypeEvent}

if err := req.Validate(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}

_, err := send[eventResponse](ctx, c, req)
if err != nil {
return fmt.Errorf("failed to send event request: %w", err)
}

return nil
}

func (c *Client) ProcessSensor(ctx context.Context, details sensor.Entity) error {
if c.isDisabled(ctx, details) {
logging.FromContext(ctx).
Expand Down
11 changes: 11 additions & 0 deletions internal/hass/event/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2024 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

package event

type Event struct {
EventData any `json:"event_data" validate:"required"`
EventType string `json:"event_type" validate:"required"`
}
1 change: 1 addition & 0 deletions internal/hass/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
requestTypeRegister = "register_sensor"
requestTypeUpdate = "update_sensor_states"
requestTypeLocation = "update_location"
requestTypeEvent = "fire_event"
)

var (
Expand Down
2 changes: 2 additions & 0 deletions internal/hass/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,5 @@ type locationResponse struct {
func (r *locationResponse) updated() error {
return r
}

type eventResponse struct{}

0 comments on commit 6debf7e

Please sign in to comment.