-
Notifications
You must be signed in to change notification settings - Fork 0
/
homekit-receiver.go
136 lines (107 loc) · 3.52 KB
/
homekit-receiver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"context"
"net/http"
"github.com/brutella/hap/accessory"
"github.com/brutella/hap/characteristic"
"github.com/brutella/hap/service"
)
type Source struct {
name byte
displayName string
}
type HomekitReceiver struct {
model string
sources []Source
Bridge *accessory.Bridge
Tv *accessory.Television
dimmer *service.Lightbulb
brightness *characteristic.Brightness
directModeSwitch *service.Switch
}
type intCallback func(int, int, *http.Request)
type boolCallback func(oldVal, newVal bool, r *http.Request)
func NewHomekitReceiver(ctx context.Context, model string, sources []Source) *HomekitReceiver {
receiver := HomekitReceiver{
model: model,
sources: sources,
}
receiver.init(ctx)
return &receiver
}
func (r *HomekitReceiver) init(ctx context.Context) {
r.Bridge = accessory.NewBridge(accessory.Info{
Name: "ARCAM Receiver Bridge",
Firmware: Version,
Model: "ARCAM Receiver Bridge",
Manufacturer: "CSdread",
})
tv := accessory.NewTelevision(accessory.Info{
Name: "ARCAM Receiver",
SerialNumber: Version,
Model: r.model,
Manufacturer: "Arcam",
})
tv.Television.ConfiguredName.SetValue("Arcam Receiver")
tv.Television.SleepDiscoveryMode.SetValue(characteristic.SleepDiscoveryModeAlwaysDiscoverable)
r.Tv = tv
for _, source := range r.sources {
inputSource := createSource(source)
r.Tv.Television.AddS(inputSource.S)
r.Tv.A.AddS(inputSource.S)
}
r.dimmer = service.NewLightbulb()
r.brightness = characteristic.NewBrightness()
r.brightness.Description = "Volume"
r.dimmer.AddC(r.brightness.C)
r.Tv.Television.AddS(r.dimmer.S)
r.Tv.A.AddS(r.dimmer.S)
r.directModeSwitch = service.NewSwitch()
r.Tv.Television.AddS(r.directModeSwitch.S)
r.Tv.A.AddS(r.directModeSwitch.S)
}
func (r *HomekitReceiver) SetDirectMode(isDirectMode bool) error {
r.directModeSwitch.On.SetValue(isDirectMode)
return nil
}
func (r *HomekitReceiver) SetVolume(volume int) error {
return r.brightness.SetValue(volume)
}
func (r *HomekitReceiver) SetMute(isMuted bool) error {
r.dimmer.On.SetValue(isMuted)
return nil
}
func (r *HomekitReceiver) SetSource(source int) error {
return r.Tv.Television.ActiveIdentifier.SetValue(source)
}
func (r *HomekitReceiver) SetPowerState(state int) error {
return r.Tv.Television.Active.SetValue(state)
}
func (r *HomekitReceiver) RegisterDirectModeCallback(callback boolCallback) {
r.directModeSwitch.On.OnValueUpdate(callback)
}
func (r *HomekitReceiver) RegisterVolumeCallback(callback intCallback) {
r.brightness.OnValueUpdate(callback)
}
func (r *HomekitReceiver) RegisterPowerCallback(callback intCallback) {
r.Tv.Television.Active.OnValueUpdate(callback)
}
func (r *HomekitReceiver) RegisterMuteCallback(callback boolCallback) {
r.dimmer.On.OnValueUpdate(callback)
}
func (r *HomekitReceiver) RegisterSourceCallback(callback intCallback) {
r.Tv.Television.ActiveIdentifier.OnValueUpdate(callback)
}
func createSource(source Source) *service.InputSource {
inputSource := service.NewInputSource()
inputId := int(source.name)
id := characteristic.NewIdentifier()
id.SetValue(inputId)
inputSource.AddC(id.C)
inputSource.ConfiguredName.SetValue(source.displayName)
inputSource.IsConfigured.SetValue(characteristic.IsConfiguredConfigured)
inputSource.InputSourceType.SetValue(characteristic.InputSourceTypeHdmi)
inputSource.CurrentVisibilityState.SetValue(characteristic.CurrentVisibilityStateShown)
inputSource.ConfiguredName.Permissions = []string{characteristic.PermissionRead}
return inputSource
}