Update HomeKit pairing status

This commit is contained in:
Alexey Khit
2023-09-02 11:05:02 +03:00
parent c0d5a7c01a
commit 13a7957cf3
2 changed files with 18 additions and 3 deletions

View File

@@ -95,7 +95,7 @@ func Init() {
srv.hap.Handler = homekit.ServerHandler(srv) srv.hap.Handler = homekit.ServerHandler(srv)
} }
entry := &mdns.ServiceEntry{ srv.mdns = &mdns.ServiceEntry{
Name: name, Name: name,
Port: uint16(api.Port()), Port: uint16(api.Port()),
Info: map[string]string{ Info: map[string]string{
@@ -110,9 +110,11 @@ func Init() {
hap.TXTSetupHash: srv.hap.SetupHash(), hap.TXTSetupHash: srv.hap.SetupHash(),
}, },
} }
entries = append(entries, entry) entries = append(entries, srv.mdns)
host := entry.Host(mdns.ServiceHAP) srv.UpdateStatus()
host := srv.mdns.Host(mdns.ServiceHAP)
servers[host] = srv servers[host] = srv
} }

View File

@@ -19,12 +19,14 @@ import (
"github.com/AlexxIT/go2rtc/pkg/hap/tlv8" "github.com/AlexxIT/go2rtc/pkg/hap/tlv8"
"github.com/AlexxIT/go2rtc/pkg/homekit" "github.com/AlexxIT/go2rtc/pkg/homekit"
"github.com/AlexxIT/go2rtc/pkg/magic" "github.com/AlexxIT/go2rtc/pkg/magic"
"github.com/AlexxIT/go2rtc/pkg/mdns"
"github.com/AlexxIT/go2rtc/pkg/srtp" "github.com/AlexxIT/go2rtc/pkg/srtp"
) )
type server struct { type server struct {
stream string // stream name from YAML stream string // stream name from YAML
hap *hap.Server // server for HAP connection and encryption hap *hap.Server // server for HAP connection and encryption
mdns *mdns.ServiceEntry
srtp *srtp.Server srtp *srtp.Server
accessory *hap.Accessory // HAP accessory accessory *hap.Accessory // HAP accessory
pairings []string // pairings list pairings []string // pairings list
@@ -33,6 +35,15 @@ type server struct {
consumer *homekit.Consumer consumer *homekit.Consumer
} }
func (s *server) UpdateStatus() {
// true status is important, or device may be offline in Apple Home
if len(s.pairings) == 0 {
s.mdns.Info[hap.TXTStatusFlags] = hap.StatusNotPaired
} else {
s.mdns.Info[hap.TXTStatusFlags] = hap.StatusPaired
}
}
func (s *server) GetAccessories(_ net.Conn) []*hap.Accessory { func (s *server) GetAccessories(_ net.Conn) []*hap.Accessory {
return []*hap.Accessory{s.accessory} return []*hap.Accessory{s.accessory}
} }
@@ -188,6 +199,7 @@ func (s *server) AddPair(conn net.Conn, id string, public []byte, permissions by
"permissions": []string{string('0' + permissions)}, "permissions": []string{string('0' + permissions)},
} }
s.pairings = append(s.pairings, query.Encode()) s.pairings = append(s.pairings, query.Encode())
s.UpdateStatus()
s.PatchConfig() s.PatchConfig()
} }
@@ -201,6 +213,7 @@ func (s *server) DelPair(conn net.Conn, id string) {
} }
s.pairings = append(s.pairings[:i], s.pairings[i+1:]...) s.pairings = append(s.pairings[:i], s.pairings[i+1:]...)
s.UpdateStatus()
s.PatchConfig() s.PatchConfig()
break break
} }