-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_provider.go
238 lines (209 loc) · 6.53 KB
/
mock_provider.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"context"
action "github.com/rmrobinson/google-smart-home-action-go"
"go.uber.org/zap"
)
// MockLightbulb represents a mock lightbulb device.
type MockLightbulb struct {
ID string
Name string
IsOn bool
Brightness int
Color struct {
Hue float64
Saturation float64
Value float64
}
}
// GetState returns the current state of the lightbulb formatted for Google Assistant
func (l *MockLightbulb) GetState() action.DeviceState {
return action.NewDeviceState(true).RecordOnOff(l.IsOn).RecordBrightness(l.Brightness).RecordColorHSV(l.Color.Hue, l.Color.Saturation, l.Color.Value)
}
// MockReceiver represents a mock AV receiver device.
type MockReceiver struct {
ID string
Name string
IsOn bool
Volume int
Muted bool
CurrInput string
}
// GetState returns the current state of the receiver formatted for Google Assistant
func (r *MockReceiver) GetState() action.DeviceState {
return action.NewDeviceState(true).RecordOnOff(r.IsOn).RecordInput(r.CurrInput).RecordVolume(r.Volume, r.Muted)
}
// MockProvider implements a Google Assistent provider with 'mock' data
type MockProvider struct {
logger *zap.Logger
service *action.Service
agentID string
lights map[string]MockLightbulb
receiver MockReceiver
}
// NewMockProvider creates a new mock provider with the specified details.
func NewMockProvider(logger *zap.Logger, service *action.Service, lights map[string]MockLightbulb, receiver MockReceiver, agentID string) *MockProvider {
return &MockProvider{
logger: logger,
service: service,
lights: lights,
receiver: receiver,
agentID: agentID,
}
}
// Sync returns the set of known devices.
func (m *MockProvider) Sync(context.Context, string) (*action.SyncResponse, error) {
m.logger.Debug("sync")
resp := &action.SyncResponse{}
for _, lb := range m.lights {
ad := action.NewLight(lb.ID)
ad.Name = action.DeviceName{
DefaultNames: []string{
"Test lamp",
},
Name: lb.Name,
}
ad.WillReportState = false
ad.RoomHint = "Test Room"
ad.DeviceInfo = action.DeviceInfo{
Manufacturer: "Faltung Systems",
Model: "tl001",
HwVersion: "0.2",
SwVersion: "0.3",
}
ad.AddOnOffTrait(false, false).AddBrightnessTrait(false).AddColourTrait(action.HSV, false)
resp.Devices = append(resp.Devices, ad)
}
inputs := []action.DeviceInput{
{
Key: "input_1",
Names: []action.DeviceInputName{
{
Synonyms: []string{
"Input 1",
"Google Chromecast Audio",
},
LanguageCode: "en",
},
},
},
{
Key: "input_2",
Names: []action.DeviceInputName{
{
Synonyms: []string{
"Input 2",
"Raspberry Pi",
},
LanguageCode: "en",
},
},
},
}
ar := action.NewSimpleAVReceiver(m.receiver.ID, inputs, 100, true, false)
ar.Name = action.DeviceName{
DefaultNames: []string{
"Test receiver",
},
Name: m.receiver.Name,
}
ar.WillReportState = true
ar.RoomHint = "Test Room"
ar.DeviceInfo = action.DeviceInfo{
Manufacturer: "Faltung Systems",
Model: "tavr001",
HwVersion: "0.2",
SwVersion: "0.3",
}
resp.Devices = append(resp.Devices, ar)
return resp, nil
}
// Disconnect removes this agent ID provider.
func (m *MockProvider) Disconnect(context.Context, string) error {
m.logger.Debug("disconnect")
return nil
}
// Query retrieves the requested device data
func (m *MockProvider) Query(_ context.Context, req *action.QueryRequest) (*action.QueryResponse, error) {
m.logger.Debug("query")
resp := &action.QueryResponse{
States: map[string]action.DeviceState{},
}
for _, deviceArg := range req.Devices {
if light, found := m.lights[deviceArg.ID]; found {
resp.States[deviceArg.ID] = light.GetState()
} else if m.receiver.ID == deviceArg.ID {
resp.States[deviceArg.ID] = m.receiver.GetState()
}
}
return resp, nil
}
// Execute makes the specified devices change state.
func (m *MockProvider) Execute(_ context.Context, req *action.ExecuteRequest) (*action.ExecuteResponse, error) {
m.logger.Debug("execute")
resp := &action.ExecuteResponse{
UpdatedState: action.NewDeviceState(true),
}
for _, commandArg := range req.Commands {
for _, command := range commandArg.Commands {
m.logger.Debug("received command",
zap.String("command", command.Name),
)
for _, deviceArg := range commandArg.TargetDevices {
if m.receiver.ID == deviceArg.ID {
if command.OnOff != nil {
m.receiver.IsOn = command.OnOff.On
resp.UpdatedState.RecordOnOff(m.receiver.IsOn)
} else if command.SetVolume != nil {
m.receiver.Volume = command.SetVolume.Level
resp.UpdatedState.RecordVolume(m.receiver.Volume, false)
} else if command.AdjustVolume != nil {
m.receiver.Volume += command.AdjustVolume.Amount
resp.UpdatedState.RecordVolume(m.receiver.Volume, false)
} else if command.SetInput != nil {
m.receiver.CurrInput = command.SetInput.NewInput
resp.UpdatedState.RecordInput(m.receiver.CurrInput)
} else {
m.logger.Info("unsupported command",
zap.String("command", command.Name),
)
continue
}
resp.UpdatedDevices = append(resp.UpdatedDevices, deviceArg.ID)
continue
} else if device, found := m.lights[deviceArg.ID]; found {
if command.OnOff != nil {
device.IsOn = command.OnOff.On
resp.UpdatedState.RecordOnOff(device.IsOn)
m.lights[deviceArg.ID] = device
} else if command.BrightnessAbsolute != nil {
device.Brightness = command.BrightnessAbsolute.Brightness
resp.UpdatedState.RecordBrightness(device.Brightness)
m.lights[deviceArg.ID] = device
} else if command.BrightnessRelative != nil {
device.Brightness += command.BrightnessRelative.RelativeWeight
resp.UpdatedState.RecordBrightness(device.Brightness)
m.lights[deviceArg.ID] = device
} else if command.ColorAbsolute != nil {
device.Color.Hue = command.ColorAbsolute.Color.HSV.Hue
device.Color.Saturation = command.ColorAbsolute.Color.HSV.Saturation
device.Color.Value = command.ColorAbsolute.Color.HSV.Value
resp.UpdatedState.RecordColorHSV(device.Color.Hue, device.Color.Saturation, device.Color.Value)
m.lights[deviceArg.ID] = device
} else {
m.logger.Info("unsupported command",
zap.String("command", command.Name))
continue
}
resp.UpdatedDevices = append(resp.UpdatedDevices, deviceArg.ID)
continue
}
m.logger.Info("device not found",
zap.String("device_id", deviceArg.ID),
zap.String("command", command.Name),
)
}
}
}
return resp, nil
}