pimatic-denon-avr
Version:
Pimatic Plugin to monitor and control a Denon or Marantz AV Receiver
70 lines (61 loc) • 2.37 kB
text/coffeescript
module.exports = (env) ->
Promise = env.require 'bluebird'
_ = env.require 'lodash'
commons = require('pimatic-plugin-commons')(env)
# Device class representing the zone switch of the Denon AVR,
# i.e., a switch to switch a zone on and off
class DenonAvrZoneSwitch extends env.devices.PowerSwitch
# Create a new DenonAvrZoneSwitch device
# [Object] config device configuration
# [DenonAvrPlugin] plugin plugin instance
# [Object] lastState state information stored in database
constructor: (, , lastState) ->
= commons.base @, .class
= .id
= .name
= 'ZM'
switch .zone
when 'ZONE2' then (
= 'Z2'
)
when 'ZONE3' then (
= 'Z3'
)
= .normalize .interval, 2
= .debug || false
=
.protocolHandler.on 'response',
= false
super()
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
if response.command is
if response.param is 'ON'
true
else if response.param is 'OFF'
false
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