pimatic-denon-avr
Version:
Pimatic Plugin to monitor and control a Denon or Marantz AV Receiver
75 lines (65 loc) • 2.52 kB
text/coffeescript
module.exports = (env) ->
Promise = env.require 'bluebird'
_ = env.require 'lodash'
commons = require('pimatic-plugin-commons')(env)
# Device class representing the power switch of the Denon AVR
class DenonAvrMuteSwitch extends env.devices.PowerSwitch
# Create a new DenonAvrMuteSwitch device
# [Object] config device configuration
# [DenonAvrPlugin] plugin plugin instance
# [Object] lastState state information stored in database
constructor: (, , lastState) ->
= commons.base @, .class
= .id
= .name
= 'MU'
switch .zone
when 'ZONE2' then (
= 'Z2MU'
)
when 'ZONE3' then (
= 'Z3MU'
)
=null
= .normalize .interval, 2
= .debug || false
=
.protocolHandler.on 'response',
super()
= false
process.nextTick () =>
true
destroy: () ->
.cancelUpdate()
.protocolHandler.removeListener 'response',
super()
_requestUpdate: (immediate=false) ->
.cancelUpdate()
.debug "Requesting update"
.protocolHandler.sendRequest , '?', immediate
.catch (error) =>
.error "Error:", error
.finally () =>
.scheduleUpdate , * 1000, true
_createResponseHandler: () ->
return (response) =>
.debug "Response", response.matchedResults
switch response.command
when then (
if response.param is 'ON' then true else false
)
when 'PW' then (
powerState = response.param is 'ON'
if powerState isnt
= powerState
)
changeStateTo: (newState) ->
return new Promise (resolve, reject) =>
.protocolHandler.sendRequest(, if newState then 'ON' else 'OFF').then =>
newState
resolve()
.catch (err) =>
.rejectWithErrorString reject, err
getState: () ->
return Promise.resolve