pimatic-denon-avr
Version:
Pimatic Plugin to monitor and control a Denon or Marantz AV Receiver
96 lines (83 loc) • 3.18 kB
text/coffeescript
module.exports = (env) ->
Promise = env.require 'bluebird'
_ = env.require 'lodash'
commons = require('pimatic-plugin-commons')(env)
# Device class representing an the power state of the Denon AVR
class DenonAvrPresenceSensor extends env.devices.PresenceSensor
# Create a new DenonAvrPresenceSensor device
# [Object] config device configuration
# [DenonAvrPlugin] plugin plugin instance
# [Object] lastState state information stored in database
constructor: (, , lastState) ->
= commons.base @, .class
= .id
= .name
= .normalize .interval, 2
= .volumeDecibel
= .debug || false
=
.protocolHandler.on 'response',
= _.cloneDeep()
.volume = {
description: "Volume"
type: "number"
acronym: 'VOL'
}
.volume.unit = 'dB' if
.input = {
description: "Input Source"
type: "string"
acronym: 'INPUT'
}
= false
= 0
= ""
super()
process.nextTick () =>
true
destroy: () ->
.cancelUpdate()
.protocolHandler.removeListener 'response',
super()
_requestUpdate: (immediate=false) ->
.cancelUpdate()
.debug "Requesting update"
Promise.all([
.protocolHandler.sendRequest 'PW', '?', immediate
.protocolHandler.sendRequest 'SI', '?', immediate
.protocolHandler.sendRequest 'MV', '?', immediate
])
.catch (error) =>
.error "Error:", error
.finally () =>
.scheduleUpdate , * 1000, true
_createResponseHandler: () ->
return (response) =>
.debug "Response", response.matchedResults
switch response.command
when 'PW' then (
if response.param is 'ON' then true else false
)
when 'SI' then (
.setAttribute 'input', response.param, true
)
when 'MV' then (
if
.setAttribute 'volume', response.param
else
.setAttribute 'volume', response.param
)
_volumeToDecibel: (volume, zeroDB=80) ->
return - zeroDB
_volumeToNumber: (volume) ->
if _.isString volume
decimal = if volume.length is 3 then 0.5 else 0
return decimal + parseInt volume.substring(0, 2)
else
return volume
getPresence: () ->
return new Promise.resolve
getVolume: () ->
return new Promise.resolve
getInput: () ->
return new Promise.resolve