-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyamaha-avr-input-select-action.coffee
69 lines (56 loc) · 1.97 KB
/
yamaha-avr-input-select-action.coffee
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
module.exports = (env) ->
Promise = env.require 'bluebird'
assert = env.require 'cassert'
_ = env.require 'lodash'
M = env.matcher
class YamahaAvrInputSelectActionHandler extends env.actions.ActionHandler
constructor: (@framework, @device, @valueTokens) ->
@_variableManager = @framework.variableManager
super()
setup: ->
@dependOnDevice(@device)
super()
executeAction: (simulate) =>
@_variableManager.evaluateStringExpression(@valueTokens)
.then (value) =>
@selectInput "" + value, simulate
selectInput: (input, simulate) =>
if simulate
return Promise.resolve(__("would set input #{input}"))
else
@device.buttonPressed input.toUpperCase()
return Promise.resolve(__("set input #{input}"))
class YamahaAvrInputSelectActionProvider extends env.actions.ActionProvider
constructor: (@framework) ->
super()
parseAction: (input, context) =>
selectorDevices = _(@framework.deviceManager.devices).values().filter(
(device) => device.config.class is 'YamahaAvrInputSelector'
).value()
# Try to match the input string with: set ->
m = M(input, context).match(['avr input '])
device = null
match = null
valueTokens = null
m.matchDevice selectorDevices, (m, d) ->
# Already had a match with another device?
if device? and device.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous.""")
return
device = d
m.match(' to ')
.matchStringWithVars( (next, ts) =>
valueTokens = ts
match = next.getFullMatch()
)
if match?
assert device?
assert valueTokens?
return {
token: match
nextInput: input.substring(match.length)
actionHandler: new YamahaAvrInputSelectActionHandler(@framework, device, valueTokens)
}
else
return null
return YamahaAvrInputSelectActionProvider