Skip to content

Commit fa16d12

Browse files
committed
added experimental scene selector device, issue #9
1 parent 96fca31 commit fa16d12

File tree

5 files changed

+124
-2
lines changed

5 files changed

+124
-2
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ depend on the AVR model.
148148

149149
{
150150
"id": "avr-5",
151-
"name": "AVR Inout Selector",
151+
"name": "AVR Input Selector",
152152
"class": "YamahaAvrInputSelector"
153153
"buttons": [
154154
{
@@ -171,6 +171,29 @@ The following action is provided to switch the input source as part of rules
171171

172172
* `avr input <device> to "<id>"`, for example: `avr input yamaha-avr to "tv"`
173173

174+
### YamahaAvrSceneSelector (Experimental)
175+
176+
Similar to the input selector, the YamahaAvrSceneSelector can be used switch the the input by selecting a scene if the
177+
AVR supports that feature. Allowed values for scene selection depend on the AVR model.
178+
179+
{
180+
"id": "avr-5",
181+
"name": "AVR Scene Selector",
182+
"class": "YamahaAvrSceneSelector"
183+
"buttons": [
184+
{
185+
"id": "TV"
186+
}
187+
]
188+
}
189+
190+
The device has the following configuration properties:
191+
192+
| Property | Default | Type | Description |
193+
|:------------------|:---------|:--------|:--------------------------------------------|
194+
| interval | 20 | Number | The time interval in seconds (minimum 10) at which the power state of the AVR will be read |
195+
| buttons | see example | Array | The buttons to display for selection. See device configuration schema for details |
196+
174197
## History
175198

176199
See [Release History](https://github.com/mwittig/pimatic-yamaha-avr/blob/master/HISTORY.md).

device-config-schema.coffee

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,39 @@ module.exports = {
103103
type: "boolean"
104104
default: false
105105
}
106+
YamahaAvrSceneSelector: {
107+
title: "Yamaha AVR Scene Selector"
108+
description: "Yamaha AVR Scene Selector"
109+
type: "object"
110+
extensions: ["xLink", "xOnLabel", "xOffLabel"]
111+
properties:
112+
interval:
113+
description: "The time interval in seconds (minimum 10) at which the mutr state of the AVR will be read"
114+
type: "number"
115+
default: 30
116+
minimum: 10
117+
buttons:
118+
description: "The scenes to select from"
119+
type: "array"
120+
default: [
121+
{
122+
id: "TV"
123+
}
124+
]
125+
format: "table"
126+
items:
127+
type: "object"
128+
properties:
129+
id:
130+
type: "string"
131+
description: "The scene ids switchable by the AVR"
132+
text:
133+
type: "string"
134+
description: "The button text to be displayed. The id will be displayed if not set"
135+
required: false
136+
confirm:
137+
description: "Ask the user to confirm the scene select"
138+
type: "boolean"
139+
default: false
140+
}
106141
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module.exports = (env) ->
2+
3+
Promise = env.require 'bluebird'
4+
_ = env.require 'lodash'
5+
commons = require('pimatic-plugin-commons')(env)
6+
commands = require '../yamaha-avr-commands'
7+
cmd = commands.cmd
8+
9+
# Device class representing a scene button of the Yamaha AVR
10+
class YamahaAvrSceneSelector extends env.devices.ButtonsDevice
11+
12+
# Create a new YamahaAvrSceneSelector device
13+
# @param [Object] config device configuration
14+
# @param [YamahaAvrPlugin] plugin plugin instance
15+
# @param [Object] lastState state information stored in database
16+
constructor: (@config, @plugin, lastState) ->
17+
@base = commons.base @, @config.class
18+
@id = @config.id
19+
@name = @config.name
20+
@interval = @base.normalize @config.interval, 10
21+
@debug = @plugin.debug || false
22+
for b in @config.buttons
23+
b.text = b.id unless b.text?
24+
@statusUpdateHandler = @_createStatusUpdateHandler()
25+
@plugin.on 'statusUpdate', @statusUpdateHandler
26+
super(@config)
27+
process.nextTick () =>
28+
@plugin.startStatusUpdates @interval * 1000
29+
30+
destroy: () ->
31+
@plugin.removeListener 'statusUpdate', @statusUpdateHandler
32+
super()
33+
34+
_createStatusUpdateHandler: () ->
35+
return (status) =>
36+
@base.debug "Status Update", status
37+
if status.Input?.Select?
38+
@_lastPressedButton = status.Input.Select
39+
@emit 'button', status.Input.Select
40+
41+
buttonPressed: (buttonId) ->
42+
return new Promise (resolve, reject) =>
43+
matched = @config.buttons.some (element, iterator) =>
44+
element.id is buttonId
45+
46+
if matched
47+
@plugin.sendRequest(cmd.command cmd.put, cmd.mainZone cmd.scene cmd.sceneSelect buttonId).then (data) =>
48+
@_lastPressedButton = buttonId
49+
@emit 'button', buttonId
50+
@plugin.emit "statusUpdate",
51+
Scene:
52+
Select: buttonId
53+
resolve()
54+
.catch (errorResult) =>
55+
@base.rejectWithErrorString reject, if errorResult instanceof Error then errorResult else errorResult.error
56+
else
57+
@base.rejectWithErrorString new Error("No button with the id #{buttonId} found")

yamaha-avr-commands.coffee

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ module.exports = {
1313
val: (command) -> "<Val>#{command}</Val><Exp>1</Exp><Unit>dB</Unit>"
1414
mute: (state) -> "<Mute>#{if state then "On" else "Off"}</Mute>"
1515
input: (command) -> "<Input>#{command}</Input>"
16-
inputSelect: (command) -> "<Input_Sel>#{command}</Input_Sel>"
16+
scene: (command) -> "<Scene>#{command}</Scene>"
17+
inputSelect: (parameter) -> "<Input_Sel>#{parameter}</Input_Sel>"
18+
sceneSelect: (parameter) -> "<Scene_Sel>#{parameter}</Scene_Sel>"
1719

1820
query:
1921
resultCode: (object) -> parseInt object.YAMAHA_AV.$.RC

yamaha-avr.coffee

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ module.exports = (env) ->
2929
"name": "Yamaha AVR Input Selector",
3030
"class": "YamahaAvrInputSelector",
3131
}
32+
{
33+
"name": "Yamaha AVR Scene Selector",
34+
"class": "YamahaAvrSceneSelector",
35+
}
3236
]
3337

3438
actionProviders = [
@@ -80,6 +84,7 @@ module.exports = (env) ->
8084
@base.cancelUpdate()
8185
@base.debug "Requesting status update"
8286
@sendRequest(cmd.command cmd.get, cmd.mainZone cmd.basicStatus cmd.getParam).then (data) =>
87+
@base.debug JSON.stringify(data)
8388
status =
8489
Power: query.powerState query.powerControl query.basicStatus query.mainZone query.command data
8590
Mute: query.mute query.volume query.basicStatus query.mainZone query.command data

0 commit comments

Comments
 (0)