Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions internal/homekit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,61 @@ streams:
homekit:
aqara1: # same stream ID from streams list
```

## Doorbell Events

You can subscribe to doorbell button press events from paired HomeKit doorbells (e.g. Aqara G4). Events are forwarded to a webhook URL and/or available via an SSE endpoint.

This runs a separate HAP connection alongside the video stream, with auto-reconnect and keepalive pings.

### Events Configuration

```yaml
streams:
doorbell: homekit://...

events:
doorbell:
stream: "doorbell" # references the stream name above (optional, defaults to event name)
webhook: "http://homeassistant.local:8123/api/webhook/doorbell_rang"
```

The webhook receives a JSON POST on each button press:

```json
{
"stream": "doorbell",
"event": "single_press",
"value": 0,
"timestamp": "2026-03-12T10:30:00Z"
}
```

Supported events: `single_press`, `double_press`, `long_press`.

### SSE Endpoint

Connect to `/api/homekit/events` for a Server-Sent Events stream of all doorbell events:

```
GET /api/homekit/events

event: doorbell
data: {"stream":"doorbell","event":"single_press","value":0,"timestamp":"..."}
```

### Home Assistant Example

```yaml
automation:
- alias: "Doorbell Rang"
trigger:
- platform: webhook
webhook_id: doorbell_rang
local_only: true
action:
- service: notify.mobile_app_phone
data:
title: "Doorbell"
message: "Someone is at the door!"
```
30 changes: 30 additions & 0 deletions internal/homekit/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package homekit

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -168,6 +169,35 @@ func apiUnpair(id string) error {
return app.PatchConfig([]string{"streams", id}, nil)
}

func apiHomekitEvents(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "streaming not supported", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
flusher.Flush()

ch := make(chan DoorbellEvent, 8)
addSSEListener(ch)
defer removeSSEListener(ch)

ctx := r.Context()
for {
select {
case <-ctx.Done():
return
case ev := <-ch:
data, _ := json.Marshal(ev)
fmt.Fprintf(w, "event: doorbell\ndata: %s\n\n", data)
flusher.Flush()
}
}
}

func findHomeKitURLs() map[string]*url.URL {
urls := map[string]*url.URL{}
for name, sources := range streams.GetAllSources() {
Expand Down
Loading