Skip to content

Commit

Permalink
Update ZpPlatform.js
Browse files Browse the repository at this point in the history
- Add `ZpS1Platform` for S1 zone players in split Sonos system;
- Add `forceS2` config.json key;
- Shared listener for `ZP` and `ZPS1`.
  • Loading branch information
ebaauw committed Jun 11, 2020
1 parent f2664de commit 2931471
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions lib/ZpPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const ZpAccessory = require('./ZpAccessory')
const ZpClient = require('./ZpClient')
const ZpListener = require('./ZpListener')

let zpListener

// Constructor for ZpPlatform. Called by homebridge on load time.
class ZpPlatform extends homebridgeLib.Platform {
constructor (log, configJson, homebridge) {
Expand Down Expand Up @@ -40,14 +42,16 @@ class ZpPlatform extends homebridgeLib.Platform {
this.on('upnpDeviceFound', this.handleUpnpMessage)

// Setup listener for zoneplayer events.
this.zpListener = new ZpListener(this.config.port, this.config.address)
this.zpListener.on('listening', (url) => {
this.log('listening on %s', url)
})
this.zpListener.on('close', (url) => {
this.log('closed %s', url)
})
this.zpListener.on('error', (error) => { this.error(error) })
if (zpListener == null) {
zpListener = new ZpListener(this.config.port, this.config.address)
zpListener.on('listening', (url) => {
this.log('listening on %s', url)
})
zpListener.on('close', (url) => {
this.log('closed %s', url)
})
zpListener.on('error', (error) => { this.error(error) })
}

const jsonOptions = { noWhiteSpace: false, sortKeys: true }
this.jsonFormatter = new homebridgeLib.JsonFormatter(jsonOptions)
Expand Down Expand Up @@ -79,6 +83,7 @@ class ZpPlatform extends homebridgeLib.Platform {
optionParser.boolKey('alarms')
optionParser.boolKey('brightness')
optionParser.boolKey('excludeAirPlay')
optionParser.boolKey('forceS2')
optionParser.intKey('heartrate', 1, 60)
optionParser.boolKey('leds')
optionParser.stringKey('nameScheme')
Expand Down Expand Up @@ -269,15 +274,24 @@ class ZpPlatform extends homebridgeLib.Platform {
'%s [%s]: %s: %s (%s) v%s', id, address, zpClient.name,
zpClient.modelName, zpClient.modelNumber, zpClient.version
)
this.topologyChanged = true
await this.parseZones(zpClient)
await zpClient.open(this.zpListener)
if (this.associatedZpClient == null && !zpClient.invisible) {
this.associatedZpClient = zpClient
this.log(
'%s [%s]: associated zone player', zpClient.id, zpClient.name
)
await zpClient.subscribe('/ZoneGroupTopology/Event')
if (
(
this.config.platform === 'ZP' &&
(zpClient.sonosOs === 'S2' || !this.config.forceS2)
) ||
this.config.platform === 'ZP' + zpClient.sonosOs
) {
this.topologyChanged = true
await this.parseZones(zpClient)
await zpClient.open(zpListener)
if (this.associatedZpClient == null && !zpClient.invisible) {
this.associatedZpClient = zpClient
this.log(
'%s [%s]: associated %s zone player',
zpClient.id, zpClient.name, zpClient.sonosOs
)
await zpClient.subscribe('/ZoneGroupTopology/Event')
}
}
zpClient.initialised = true
this.unInitialisedZpClients--
Expand Down Expand Up @@ -368,8 +382,8 @@ class ZpPlatform extends homebridgeLib.Platform {
const zones = ZpClient.unflatten(zonePlayers)
const nZones = Object.keys(zones).length
this.log(
'found %d zone players in %d zones',
Object.keys(zonePlayers).length, nZones
'found %d %s zone players in %d zones',
Object.keys(zonePlayers).length, this.associatedZpClient.sonosOs, nZones
)
let i = 0
let j = 0
Expand Down Expand Up @@ -487,4 +501,9 @@ class ZpPlatform extends homebridgeLib.Platform {
}
}

module.exports = ZpPlatform
class ZpS1Platform extends ZpPlatform {}

module.exports = {
ZpPlatform: ZpPlatform,
ZpS1Platform: ZpS1Platform
}

0 comments on commit 2931471

Please sign in to comment.