├── .gitignore ├── zp.png ├── homebridge-zp.png ├── jsdoc.json ├── cli └── zp.js ├── index.js ├── lib ├── ZpService │ ├── index.js │ ├── Alarm.js │ ├── Led.js │ ├── Speaker.js │ ├── Tv.js │ └── Sonos.js ├── ZpAccessory │ ├── Tv.js │ ├── Slave.js │ ├── Master.js │ └── index.js ├── ZpHousehold.js └── ZpPlatform.js ├── .github └── FUNDING.yml ├── ISSUE_TEMPLATE.md ├── package.json ├── config.schema.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | out 3 | npm-debug.log 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /zp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ebaauw/homebridge-zp/HEAD/zp.png -------------------------------------------------------------------------------- /homebridge-zp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ebaauw/homebridge-zp/HEAD/homebridge-zp.png -------------------------------------------------------------------------------- /jsdoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "plugins/markdown" 4 | ], 5 | "rescurseDepth": 10, 6 | "source": { 7 | "include": [ 8 | "README.md", 9 | "index.js", 10 | "lib", 11 | "node_modules/hb-lib-tools/lib/HttpClient.js" 12 | ] 13 | }, 14 | "templates": { 15 | "monospaceLinks": true 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /cli/zp.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | // homebridge-zp/cli/zp.js 4 | // Copyright © 2019-2025 Erik Baauw. All rights reserved. 5 | // 6 | // Homebridge plugin for Sonos ZonePlayer. 7 | 8 | import { createRequire } from 'node:module' 9 | 10 | import { ZpTool } from 'hb-zp-tools/ZpTool' 11 | 12 | const require = createRequire(import.meta.url) 13 | const packageJson = require('../package.json') 14 | 15 | new ZpTool(packageJson).main() 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // homebridge-zp/index.js 2 | // Copyright © 2016-2025 Erik Baauw. All rights reserved. 3 | // 4 | // Homebridge plugin for Sonos ZonePlayer. 5 | 6 | import { createRequire } from 'node:module' 7 | 8 | import { ZpPlatform } from './lib/ZpPlatform.js' 9 | 10 | const require = createRequire(import.meta.url) 11 | const packageJson = require('./package.json') 12 | 13 | function main (homebridge) { 14 | ZpPlatform.loadPlatform(homebridge, packageJson, 'ZP', ZpPlatform) 15 | } 16 | 17 | export { main as default } 18 | -------------------------------------------------------------------------------- /lib/ZpService/index.js: -------------------------------------------------------------------------------- 1 | // homebridge-zp/lib/ZpService/index.js 2 | // Copyright © 2016-2025 Erik Baauw. All rights reserved. 3 | // 4 | // Homebridge plugin for Sonos ZonePlayer. 5 | 6 | import { ServiceDelegate } from 'homebridge-lib/ServiceDelegate' 7 | 8 | class ZpService extends ServiceDelegate { 9 | constructor (zpAccessory, params) { 10 | super(zpAccessory, params) 11 | this.zpAccessory = zpAccessory 12 | this.zpClient = this.zpAccessory.zpClient 13 | this.zpHousehold = this.zpAccessory.zpHousehold 14 | } 15 | } 16 | 17 | export { ZpService } 18 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [ebaauw] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ["https://www.paypal.me/ebaauw/EUR"] 13 | -------------------------------------------------------------------------------- /ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 9 | #### Issue 10 | 11 | 12 | #### Log Messages 13 | 14 | ``` 15 | ``` 16 | 17 | #### Debug Files 18 | 19 | -------------------------------------------------------------------------------- /lib/ZpAccessory/Tv.js: -------------------------------------------------------------------------------- 1 | // homebridge-zp/lib/ZpAccessory/Tv.js 2 | // Copyright © 2016-2025 Erik Baauw. All rights reserved. 3 | // 4 | // Homebridge plugin for Sonos ZonePlayer. 5 | 6 | import { ZpAccessory } from './index.js' 7 | import { ZpService } from '../ZpService/index.js' 8 | 9 | class Tv extends ZpAccessory { 10 | constructor (platform, params) { 11 | params.id = platform.config.tvIdPrefix + params.id.slice(6) 12 | params.category = platform.Accessory.Categories.SPEAKER 13 | params.externalAccessory = true 14 | super(platform, params) 15 | this.inheritLogLevel(params.master) 16 | // params.master.context.tv = this._context 17 | this.debug('TV accessory') 18 | this.tvService = new ZpService.Tv(this, params) 19 | this.zpClient.on('lastSeenUpdated', () => { 20 | this.tvService.values.statusFault = 21 | this.Characteristics.hap.StatusFault.NO_FAULT 22 | }) 23 | setImmediate(() => { 24 | this.emit('initialised') 25 | }) 26 | } 27 | 28 | topologyUpdated () { 29 | this.tvService.updateGroupInputSource() 30 | } 31 | } 32 | 33 | ZpAccessory.Tv = Tv 34 | -------------------------------------------------------------------------------- /lib/ZpAccessory/Slave.js: -------------------------------------------------------------------------------- 1 | // homebridge-zp/lib/ZpAccessory/Slave.js 2 | // Copyright © 2016-2025 Erik Baauw. All rights reserved. 3 | // 4 | // Homebridge plugin for Sonos ZonePlayer. 5 | 6 | import { ZpAccessory } from './index.js' 7 | import { ZpService } from '../ZpService/index.js' 8 | 9 | class Slave extends ZpAccessory { 10 | constructor (platform, params) { 11 | params.category = platform.Accessory.Categories.SPEAKER 12 | super(platform, params) 13 | this.inheritLogLevel(params.master) 14 | this.debug('LED accessory') 15 | this.context.master = params.master.id 16 | this.ledService = new ZpService.Led(this, this.zpClient) 17 | this.attachZpClient() 18 | setImmediate(() => { 19 | this.emit('initialised') 20 | }) 21 | } 22 | 23 | updateLastSeen () { 24 | this.ledService.values.lastSeen = this.zpClient.lastSeen 25 | this.ledService.values.statusFault = 26 | this.Characteristics.hap.StatusFault.NO_FAULT 27 | } 28 | 29 | topologyUpdated () { 30 | if (this.zpClient.battery != null) { 31 | this.checkBattery(this.zpClient.battery) 32 | } 33 | } 34 | } 35 | 36 | ZpAccessory.Slave = Slave 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "homebridge-zp", 3 | "description": "Homebridge plugin for Sonos Zone Players", 4 | "displayName": "Homebridge ZP", 5 | "author": "Erik Baauw", 6 | "maintainers": [ 7 | "ebaauw" 8 | ], 9 | "license": "Apache-2.0", 10 | "version": "1.4.60", 11 | "keywords": [ 12 | "homebridge-plugin", 13 | "homekit", 14 | "sonos", 15 | "zoneplayer", 16 | "sonos-zp", 17 | "sonos-zoneplayer" 18 | ], 19 | "type": "module", 20 | "main": "index.js", 21 | "bin": { 22 | "zp": "cli/zp.js" 23 | }, 24 | "engines": { 25 | "homebridge": "^1.11.1||^2.0.0-beta", 26 | "node": "^24||^22||^20" 27 | }, 28 | "dependencies": { 29 | "hb-zp-tools": "~1.0.15", 30 | "he": "^1.2.0", 31 | "homebridge-lib": "~7.1.13" 32 | }, 33 | "scripts": { 34 | "prepare": "standard", 35 | "test": "standard && echo \"Error: no test specified\" && exit 1" 36 | }, 37 | "repository": { 38 | "type": "git", 39 | "url": "git+https://github.com/ebaauw/homebridge-zp.git" 40 | }, 41 | "bugs": { 42 | "url": "https://github.com/ebaauw/homebridge-zp/issues" 43 | }, 44 | "homepage": "https://github.com/ebaauw/homebridge-zp#readme", 45 | "funding": [ 46 | { 47 | "type": "github", 48 | "url": "https://github.com/sponsors/ebaauw" 49 | }, 50 | { 51 | "type": "paypal", 52 | "url": "https://www.paypal.me/ebaauw/EUR" 53 | } 54 | ] 55 | } 56 | -------------------------------------------------------------------------------- /lib/ZpService/Alarm.js: -------------------------------------------------------------------------------- 1 | // homebridge-zp/lib/ZpService/Alarm.js 2 | // Copyright © 2016-2025 Erik Baauw. All rights reserved. 3 | // 4 | // Homebridge plugin for Sonos ZonePlayer. 5 | 6 | import { ZpService } from './index.js' 7 | 8 | class Alarm extends ZpService { 9 | constructor (zpAccessory, alarm) { 10 | const params = { 11 | id: alarm.id, 12 | name: zpAccessory.zpClient.zoneName + ' Sonos Alarm ' + alarm.id, 13 | Service: zpAccessory.Services.hap.Switch, 14 | subtype: 'alarm' + alarm.id 15 | } 16 | super(zpAccessory, params) 17 | this.debug('Alarm service') 18 | this.addCharacteristicDelegate({ 19 | key: 'on', 20 | Characteristic: this.Characteristics.hap.On, 21 | setter: async (value) => { 22 | const alarm = Object.assign({}, this._alarm) 23 | alarm.enabled = value ? 1 : 0 24 | return this.zpClient.updateAlarm(alarm) 25 | } 26 | }) 27 | this.addCharacteristicDelegate({ 28 | key: 'currentTrack', 29 | Characteristic: this.Characteristics.my.CurrentTrack 30 | }) 31 | this.addCharacteristicDelegate({ 32 | key: 'time', 33 | Characteristic: this.Characteristics.my.Time 34 | }) 35 | this.emit('initialised') 36 | this._alarm = alarm 37 | } 38 | 39 | get alarm () { return this._alarm } 40 | 41 | set alarm (alarm) { 42 | this._alarm = alarm 43 | this.values.on = alarm.enabled === 1 44 | this.values.currentTrack = alarm.programUri === 'x-rincon-buzzer:0' 45 | ? 'Sonos Chime' 46 | : alarm.programMetaData != null && alarm.programMetaData.title != null 47 | ? alarm.programMetaData.title.slice(0, 64) 48 | : 'unknown' 49 | this.values.time = alarm.startTime 50 | } 51 | } 52 | 53 | ZpService.Alarm = Alarm 54 | -------------------------------------------------------------------------------- /lib/ZpService/Led.js: -------------------------------------------------------------------------------- 1 | // homebridge-zp/lib/ZpService/Led.js 2 | // Copyright © 2016-2025 Erik Baauw. All rights reserved. 3 | // 4 | // Homebridge plugin for Sonos ZonePlayer. 5 | 6 | import { ZpService } from './index.js' 7 | 8 | class Led extends ZpService { 9 | constructor (zpAccessory, zpClient) { 10 | const params = { 11 | name: zpClient.zoneName + ' Sonos LED', 12 | Service: zpAccessory.Services.hap.Lightbulb, 13 | subtype: 'led' + (zpClient.channel == null ? '' : zpClient.channel) 14 | } 15 | if (zpClient.role !== 'master' && zpClient.channel != null) { 16 | params.name = zpClient.zoneName + ' Sonos ' + zpClient.channel + ' LED' 17 | } 18 | super(zpAccessory, params) 19 | this.debug('LED service') 20 | const paramsOn = { 21 | key: 'on', 22 | Characteristic: this.Characteristics.hap.On, 23 | setter: this.zpClient.setLedState.bind(this.zpClient) 24 | } 25 | const paramsLocked = { 26 | key: 'locked', 27 | Characteristic: this.Characteristics.hap.LockPhysicalControls, 28 | props: { adminOnlyAccess: [this.Characteristic.Access.WRITE] }, 29 | setter: this.zpClient.setButtonLockState.bind(this.zpClient) 30 | } 31 | if (!(this.platform.config.heartrate > 0)) { 32 | paramsOn.getter = this.zpClient.getLedState.bind(this.zpClient) 33 | paramsLocked.getter = async (value) => { 34 | return (await this.zpClient.getButtonLockState()) 35 | ? this.Characteristics.hap.LockPhysicalControls.CONTROL_LOCK_ENABLED 36 | : this.Characteristics.hap.LockPhysicalControls.CONTROL_LOCK_DISABLED 37 | } 38 | } 39 | this.addCharacteristicDelegate(paramsOn) 40 | this.addCharacteristicDelegate(paramsLocked) 41 | if (zpClient.role !== 'master') { 42 | this.addCharacteristicDelegate({ 43 | key: 'lastSeen', 44 | Characteristic: this.Characteristics.my.LastSeen, 45 | value: 'n/a', 46 | silent: true 47 | }) 48 | this.addCharacteristicDelegate({ 49 | key: 'statusFault', 50 | Charactertistic: this.Characteristics.hap.StatusFault, 51 | value: this.Characteristics.hap.StatusFault.GENERAL_FAULT 52 | }) 53 | this.values.statusFault = 54 | this.Characteristics.hap.StatusFault.GENERAL_FAULT 55 | } 56 | 57 | if (this.platform.config.heartrate > 0) { 58 | this.zpAccessory.on('heartbeat', async (beat) => { 59 | try { 60 | if (beat % this.platform.config.heartrate === 0) { 61 | if (!this.zpAccessory.blinking) { 62 | this.values.on = await this.zpClient.getLedState() 63 | } 64 | this.values.locked = (await this.zpClient.getButtonLockState()) 65 | ? this.Characteristics.hap.LockPhysicalControls.CONTROL_LOCK_ENABLED 66 | : this.Characteristics.hap.LockPhysicalControls.CONTROL_LOCK_DISABLED 67 | } 68 | } catch (error) { 69 | this.error(error) 70 | } 71 | }) 72 | } 73 | this.emit('initialised') 74 | } 75 | } 76 | 77 | ZpService.Led = Led 78 | -------------------------------------------------------------------------------- /lib/ZpAccessory/Master.js: -------------------------------------------------------------------------------- 1 | // homebridge-zp/lib/ZpAccessory/Master.js 2 | // Copyright © 2016-2025 Erik Baauw. All rights reserved. 3 | // 4 | // Homebridge plugin for Sonos ZonePlayer. 5 | 6 | import { ZpAccessory } from './index.js' 7 | import { ZpService } from '../ZpService/index.js' 8 | 9 | class Master extends ZpAccessory { 10 | constructor (platform, params) { 11 | params.category = platform.Accessory.Categories.SPEAKER 12 | super(platform, params) 13 | this.debug('Sonos accessory') 14 | this.alarmServices = {} 15 | this.sonosService = new ZpService.Sonos(this) 16 | this.manageLogLevel(this.sonosService.characteristicDelegate('logLevel')) 17 | if (this.platform.config.speakers) { 18 | this.speakerService = new ZpService.Speaker(this) 19 | } 20 | if (this.platform.config.leds) { 21 | this.ledService = new ZpService.Led(this, this.zpClient) 22 | } 23 | this.attachZpClient() 24 | this.topologyUpdated() 25 | if (this.platform.config.alarms) { 26 | this.notYetInitialised = true 27 | this.zpHousehold.on('alarmListUpdated', this.alarmListUpdated.bind(this)) 28 | this.alarmListUpdated() 29 | return // 'initialised' will be emitted by alarmListUpdated() 30 | } 31 | setImmediate(() => { 32 | this.emit('initialised') 33 | }) 34 | } 35 | 36 | updateLastSeen () { 37 | this.sonosService.values.lastSeen = this.zpClient.lastSeen 38 | this.sonosService.values.statusFault = 39 | this.Characteristics.hap.StatusFault.NO_FAULT 40 | } 41 | 42 | topologyUpdated () { 43 | this.sonosService.values.sonosGroup = this.zpClient.zoneGroupShortName 44 | this.isCoordinator = this.zpClient.zoneGroup === this.zpClient.id 45 | if (this.isCoordinator) { 46 | this.coordinator = this 47 | if (this.speakerService != null) { 48 | this.speakerService.values.on = false 49 | } 50 | this.leaving = false 51 | } else { 52 | if (this.speakerService != null) { 53 | this.speakerService.values.on = true 54 | } 55 | this.coordinator = this.platform.groupCoordinator(this.zpClient.zoneGroup) 56 | this.copyCoordinator() 57 | } 58 | if (this.zpClient.battery != null) { 59 | this.checkBattery(this.zpClient.battery) 60 | } 61 | } 62 | 63 | alarmListUpdated () { 64 | const alarms = this.zpHousehold.alarmList 65 | if (alarms == null) { 66 | return 67 | } 68 | const keys = {} 69 | for (const alarm of alarms) { 70 | if (alarm.roomUuid === this.zpClient.id) { 71 | keys[alarm.id] = true 72 | const service = this.alarmServices[alarm.id] 73 | if (service == null) { 74 | this.alarmServices[alarm.id] = new ZpService.Alarm(this, alarm) 75 | } else { 76 | service.alarm = alarm 77 | } 78 | } 79 | } 80 | for (const key in this.alarmServices) { 81 | if (!keys[key]) { 82 | this.log('remove Alarm %s', key) 83 | this.alarmServices[key].destroy() 84 | delete this.alarmServices[key] 85 | } 86 | } 87 | if (this.notYetInitialised) { 88 | delete this.notYetInitialised 89 | this.emit('initialised') 90 | } 91 | } 92 | 93 | // Copy group characteristic values from group coordinator. 94 | copyCoordinator () { 95 | const coordinator = this.coordinator 96 | if (coordinator && coordinator !== this && !this.leaving) { 97 | this.debug( 98 | 'copy coordinator %s [%s]', this.coordinator.zpClient.id, 99 | this.coordinator.zpClient.zoneGroupName 100 | ) 101 | const src = coordinator.sonosService.values 102 | const dst = this.sonosService.values 103 | dst.sonosGroup = src.sonosGroup 104 | dst.on = src.on 105 | dst.volume = src.volume 106 | dst.mute = src.mute 107 | dst.currentTrack = src.currentTrack 108 | dst.currentTransportActions = src.currentTransportActions 109 | dst.uri = src.uri 110 | } 111 | } 112 | 113 | // Return array of members. 114 | members () { 115 | if (!this.isCoordinator) { 116 | return [] 117 | } 118 | return this.platform.groupMembers(this.zpClient.id) 119 | } 120 | } 121 | 122 | ZpAccessory.Master = Master 123 | -------------------------------------------------------------------------------- /config.schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "pluginAlias": "ZP", 3 | "pluginType": "platform", 4 | "singular": true, 5 | "headerDisplay": "Homebridge plugin for Sonos ZonePlayer", 6 | "footerDisplay": "", 7 | "schema": { 8 | "type": "object", 9 | "properties": { 10 | "name": { 11 | "description": "Plugin name as displayed in the Homebridge log.", 12 | "type": "string", 13 | "required": true, 14 | "default": "Sonos" 15 | }, 16 | "alarms": { 17 | "description": "Create an additional Switch service for each alarm.", 18 | "type": "boolean" 19 | }, 20 | "brightness": { 21 | "description": "Expose volume as Brightness.", 22 | "type": "boolean" 23 | }, 24 | "excludeAirPlay": { 25 | "title": "Exclude AirPlay 2", 26 | "description": "Exclude AirPlay 2 zone players that are already exposed to Apple's Home app.", 27 | "type": "boolean" 28 | }, 29 | "heartrate": { 30 | "description": "Interval (in seconds) to poll zone player. Default: disabled.", 31 | "type": "integer" 32 | }, 33 | "leds": { 34 | "title": "LEDs", 35 | "description": "Create an additional Lightbulb service for the LED. Also create an accessory for each slave zone player.", 36 | "type": "boolean" 37 | }, 38 | "maxFavourites": { 39 | "description": "Maximum number of favourites supported, between 16 and 96. Default: 96.", 40 | "type": "integer" 41 | }, 42 | "mdns": { 43 | "description": "Enable mDNS discovery of Sonos ZonePlayers. Default: true.", 44 | "type": "boolean", 45 | "default": true 46 | }, 47 | "port": { 48 | "description": "Port to use for webserver receiving zone player notifications. Default: random.", 49 | "type": "integer", 50 | "maximum": 65535 51 | }, 52 | "resetTimeout": { 53 | "description": "Timeout (in milliseconds) to reset input. Default: 500.", 54 | "type": "integer" 55 | }, 56 | "service": { 57 | "description": "HomeKit service type for Sonos and Switch services.", 58 | "type": "string", 59 | "required": true, 60 | "default": "switch", 61 | "oneOf": [ 62 | { 63 | "title": "Switch", 64 | "enum": [ 65 | "switch" 66 | ] 67 | }, 68 | { 69 | "title": "Lightbulb", 70 | "enum": [ 71 | "light" 72 | ] 73 | }, 74 | { 75 | "title": "Speaker", 76 | "enum": [ 77 | "speaker" 78 | ] 79 | }, 80 | { 81 | "title": "Fan", 82 | "enum": [ 83 | "fan" 84 | ] 85 | } 86 | ] 87 | }, 88 | "speakers": { 89 | "description": "Create an additional Speaker service for each zone.", 90 | "type": "boolean" 91 | }, 92 | "subscriptionTimeout": { 93 | "description": "Duration (in minutes) of subscriptions to ZonePlayer notifications. Default: 30.", 94 | "type": "integer" 95 | }, 96 | "timeout": { 97 | "description": "Timeout (in seconds) to wait for a response from a Sonos ZonePlayer. Default: 15.", 98 | "type": "integer" 99 | }, 100 | "tv": { 101 | "title": "TV", 102 | "description": "Create an additional, non-bridged TV accessory for each zone.", 103 | "type": "boolean" 104 | }, 105 | "tvIdPrefix": { 106 | "title": "TV ID Prefix", 107 | "description": "Prefix for serial number of TV accessories. Default: 'TV'", 108 | "type": "string" 109 | } 110 | } 111 | }, 112 | "form": [ 113 | "name", 114 | { 115 | "type": "fieldset", 116 | "expandable": true, 117 | "title": "What", 118 | "description": "Select what to expose to HomeKit for each zone.", 119 | "items": [ 120 | "speakers", 121 | "alarms", 122 | "leds", 123 | { 124 | "key": "heartrate", 125 | "condition": { 126 | "functionBody": "return model.leds" 127 | } 128 | }, 129 | "excludeAirPlay" 130 | ] 131 | }, 132 | { 133 | "type": "fieldset", 134 | "expandable": true, 135 | "title": "How", 136 | "description": "Select how to expose a zone to HomeKit.", 137 | "items": [ 138 | "service", 139 | { 140 | "key": "brightness", 141 | "condition": { 142 | "functionBody": "return model.service === 'switch' || model.service === 'speaker'" 143 | } 144 | }, 145 | "tv", 146 | { 147 | "key": "maxFavourites", 148 | "condition": { 149 | "functionBody": "return model.tv" 150 | } 151 | } 152 | ] 153 | }, 154 | { 155 | "type": "fieldset", 156 | "expandable": true, 157 | "title": "Advanced Settings", 158 | "description": "Don't change these, unless you understand what you're doing.", 159 | "items": [ 160 | "resetTimeout", 161 | "subscriptionTimeout", 162 | "timeout", 163 | "mdns", 164 | "port", 165 | "tvIdPrefix" 166 | ] 167 | } 168 | ] 169 | } 170 | -------------------------------------------------------------------------------- /lib/ZpHousehold.js: -------------------------------------------------------------------------------- 1 | // homebridge-zp/lib/ZpHousehold.js 2 | // Copyright © 2016-2025 Erik Baauw. All rights reserved. 3 | // 4 | // Homebridge plugin for Sonos ZonePlayer. 5 | 6 | import { once } from 'node:events' 7 | 8 | import { Delegate } from 'homebridge-lib/Delegate' 9 | 10 | const ZONEPLAYERS_PER_HOUSEHOLD = 32 11 | 12 | class ZpHousehold extends Delegate { 13 | constructor (platform, zpClient) { 14 | super(platform, zpClient.household) 15 | this.household = zpClient.household 16 | this.setMaxListeners(ZONEPLAYERS_PER_HOUSEHOLD) 17 | } 18 | 19 | error (format, ...args) { 20 | if (typeof format !== 'object' || format.request == null) { 21 | super.error(format, ...args) 22 | } 23 | } 24 | 25 | async setAssociated (zpClient) { 26 | if (zpClient.household !== this.household) { 27 | throw new SyntaxError(`${zpClient.id} not in ${this.household}`) 28 | } 29 | if (this.zpClient != null && zpClient.id !== this.zpClient.id) { 30 | try { 31 | await this.zpClient.unsubscribe('/ZoneGroupTopology/Event') 32 | } catch (error) { this.error(error) } 33 | if (this.platform.config.alarms) { 34 | try { 35 | await this.zpClient.unsubscribe('/AlarmClock/Event') 36 | } catch (error) { this.error(error) } 37 | } 38 | if (this.platform.config.tv) { 39 | try { 40 | await this.zpClient.unsubscribe('/MediaServer/ContentDirectory/Event') 41 | } catch (error) { this.error(error) } 42 | } 43 | } 44 | this.zpClient.removeListener('message', this.onMessage.bind(this)) 45 | this.log( 46 | '%s [%s]: associated %s zoneplayer', 47 | this.zpClient.id, this.zpClient.name, this.zpClient.sonosOs 48 | ) 49 | this.zpClient = zpClient 50 | this.zpClient.on('message', this.onMessage.bind(this)) 51 | const timeout = this.platform.config.timeout 52 | await this.zpClient.subscribe('/ZoneGroupTopology/Event') 53 | const timer = setTimeout(() => { 54 | this.warn( 55 | '%s [%s]: no ZoneGroupTopology event received in %ds', 56 | this.zpClient.id, this.zpClient.name, timeout 57 | ) 58 | this.emit('error', new Error('timeout')) 59 | }, timeout * 1000) 60 | try { 61 | await once(this, 'topologyUpdated') 62 | } catch (error) {} 63 | clearTimeout(timer) 64 | if (this.platform.config.alarms) { 65 | await this.zpClient.subscribe('/AlarmClock/Event') 66 | const timer = setTimeout(() => { 67 | this.warn( 68 | '%s [%s]: no AlarmClock event received in %ds', 69 | this.zpClient.id, this.zpClient.name, timeout 70 | ) 71 | this.emit('error', new Error('timeout')) 72 | }, timeout * 1000) 73 | try { 74 | await once(this, 'alarmListUpdated') 75 | } catch (error) { 76 | this.alarmList = [] 77 | } 78 | clearTimeout(timer) 79 | } 80 | if (this.platform.config.tv) { 81 | await this.zpClient.subscribe('/MediaServer/ContentDirectory/Event') 82 | const timer = setTimeout(() => { 83 | this.warn( 84 | '%s [%s]: no ContentDirectory event received in %ds', 85 | this.zpClient.id, this.zpClient.name, timeout 86 | ) 87 | this.emit('error', new Error('timeout')) 88 | }, timeout * 1000) 89 | try { 90 | await once(this, 'favouritesUpdated') 91 | } catch (error) {} 92 | clearTimeout(timer) 93 | } 94 | } 95 | 96 | async onMessage (message) { 97 | try { 98 | const f = `handle${message.device}${message.service}Event` 99 | if (this[f] != null) { 100 | await this[f](message.parsedBody) 101 | } 102 | } catch (error) { 103 | this.error(error) 104 | } 105 | } 106 | 107 | async handleZonePlayerZoneGroupTopologyEvent (body) { 108 | try { 109 | if (body.zoneGroups != null) { 110 | for (const group of body.zoneGroups) { 111 | const coordinator = this.platform.zpClients[group.coordinator] 112 | if (coordinator == null) { 113 | continue 114 | } 115 | if (coordinator.id !== this.zpClient.id) { 116 | try { 117 | await coordinator.initTopology(this.zpClient) 118 | } catch (error) { this.error(error) } 119 | } 120 | for (const member of group.zoneGroupMembers) { 121 | const zone = this.platform.zpClients[member.uuid] 122 | if (zone == null) { 123 | continue 124 | } 125 | if (zone.id !== coordinator.id && zone.id !== this.zpClient.id) { 126 | try { 127 | await zone.initTopology(this.zpClient) 128 | } catch (error) { this.error(error) } 129 | } 130 | } 131 | } 132 | } 133 | if (body.vanishedDevices != null) { 134 | for (const device of body.vanishedDevices) { 135 | this.platform.lostZonePlayer(device.uuid) 136 | } 137 | } 138 | this.emit('topologyUpdated') 139 | } catch (error) { this.error(error) } 140 | } 141 | 142 | async handleZonePlayerAlarmClockEvent (body) { 143 | try { 144 | this.alarmList = (await this.zpClient.listAlarms()).currentAlarmList 145 | this.emit('alarmListUpdated') 146 | } catch (error) { this.error(error) } 147 | } 148 | 149 | async handleMediaServerContentDirectoryEvent (body) { 150 | try { 151 | if ( 152 | body.favoritesUpdateId == null || 153 | body.favoritesUpdateId === this.favoritesUpdateId 154 | ) { 155 | return 156 | } 157 | this.favoritesUpdateId = body.favoritesUpdateId 158 | this.favourites = await this.zpClient.browse('FV:2') 159 | this.emit('favouritesUpdated') 160 | } catch (error) { this.error(error) } 161 | } 162 | } 163 | 164 | export { ZpHousehold } 165 | -------------------------------------------------------------------------------- /lib/ZpAccessory/index.js: -------------------------------------------------------------------------------- 1 | // homebridge-zp/lib/ZpAccessory/index.js 2 | // Copyright © 2016-2025 Erik Baauw. All rights reserved. 3 | // 4 | // Homebridge plugin for Sonos ZonePlayer. 5 | 6 | import { once } from 'node:events' 7 | 8 | import { AccessoryDelegate } from 'homebridge-lib/AccessoryDelegate' 9 | import { ServiceDelegate } from 'homebridge-lib/ServiceDelegate' 10 | import 'homebridge-lib/ServiceDelegate/Battery' // TODO: dynamic import 11 | 12 | class ZpAccessory extends AccessoryDelegate { 13 | constructor (platform, params) { 14 | super(platform, params) 15 | this.context.name = params.name 16 | this.context.id = params.id 17 | this.context.address = params.address 18 | this.context.household = params.household 19 | this.heartbeatEnabled = true 20 | this.zpClient = params.zpClient 21 | this.zpHousehold = params.zpHousehold 22 | 23 | this.on('identify', this.identify) 24 | this.zpHousehold.on('topologyUpdated', this.topologyUpdated.bind(this)) 25 | } 26 | 27 | // Adopt ownership of the ZpClient instance, taking over all event handling. 28 | // This implies that any 'message` handling by ZpHousehold or ZpAccessory.TV 29 | // needs to be setup after the ZpAccessory.Master has been created. 30 | attachZpClient () { 31 | // if (this.zpClient == null) { 32 | // this.zpClient = new ZpClient({ 33 | // host: this.context.address, 34 | // id: this.context.id, 35 | // household: this.context.household, 36 | // listener: this.platform.listener, 37 | // timeout: this.platform.config.timeout 38 | // }) 39 | // } 40 | this.zpClient 41 | .removeAllListeners('request') 42 | .removeAllListeners('response') 43 | .removeAllListeners('error') 44 | // .removeAllListeners('message') 45 | .removeAllListeners('rebooted') 46 | .removeAllListeners('addressChanged') 47 | // .removeAllListeners() 48 | .on('request', (request) => { 49 | this.debug( 50 | 'request %s: %s %s%s', 51 | request.id, request.method, request.resource, 52 | request.action == null ? '' : ' ' + request.action 53 | ) 54 | if (request.parsedBody != null) { 55 | this.vdebug( 56 | 'request %s: %s %s %j', 57 | request.id, request.method, request.url, request.parsedBody 58 | ) 59 | this.vvdebug( 60 | 'request %s: %s %s (headers: %j) %s', 61 | request.id, request.method, request.url, 62 | request.headers, request.body 63 | ) 64 | } else { 65 | this.vdebug( 66 | 'request %s: %s %s', 67 | request.id, request.method, request.url 68 | ) 69 | this.vvdebug( 70 | 'request %s: %s %s (headers: %j)', 71 | request.id, request.method, request.url, request.headers 72 | ) 73 | } 74 | }) 75 | .on('response', (response) => { 76 | if (response.parsedBody != null) { 77 | this.vvdebug( 78 | 'request %s: response (headers: %j): %j', response.request.id, 79 | response.headers, response.body 80 | ) 81 | this.vdebug( 82 | 'request %s: response: %j', response.request.id, response.parsedBody 83 | ) 84 | } 85 | this.debug( 86 | 'request %s: http status %d %s', 87 | response.request.id, response.statusCode, response.statusMessage 88 | ) 89 | }) 90 | .on('error', (error) => { 91 | if (error.request == null) { 92 | this.error(error) 93 | return 94 | } 95 | if (error.request.id !== this.requestId) { 96 | if (error.request.body == null) { 97 | this.log( 98 | 'request %d: %s %s', error.request.id, 99 | error.request.method, error.request.resource 100 | ) 101 | } else { 102 | this.log( 103 | 'request %d: %s %s', error.request.id, 104 | error.request.method, error.request.resource, error.request.action 105 | ) 106 | } 107 | this.requestId = error.request.id 108 | } 109 | this.warn( 110 | 'request %d: %s', error.request.id, error 111 | ) 112 | }) 113 | .on('message', (message) => { 114 | const notify = message.device === 'ZonePlayer' 115 | ? message.service 116 | : message.device + '/' + message.service 117 | this.vvdebug('notify %s/Event: %s', notify, message.body) 118 | this.vdebug('notify %s/Event: %j', notify, message.parsedBody) 119 | this.debug('notify %s/Event', notify) 120 | try { 121 | const f = `handle${message.device}${message.service}Event` 122 | if (this[f] != null) { 123 | this[f](message.parsedBody) 124 | } 125 | } catch (error) { 126 | this.error(error) 127 | } 128 | }) 129 | .on('rebooted', (oldBootSeq) => { 130 | this.warn('rebooted (%d -> %d)', oldBootSeq, this.zpClient.bootSeq) 131 | }) 132 | .on('addressChanged', (oldAddress) => { 133 | this.warn( 134 | 'address changed from %s to %s', oldAddress, this.zpClient.address 135 | ) 136 | }) 137 | .on('lastSeenUpdated', () => { 138 | this.updateLastSeen() 139 | }) 140 | } 141 | 142 | checkBattery () { 143 | try { 144 | const battery = this.zpClient.battery 145 | if (battery.percentage == null || battery.charging == null) { 146 | return 147 | } 148 | this.debug('battery: %j', battery) 149 | if (this.batteryService == null) { 150 | this.batteryService = new ServiceDelegate.Battery(this, { 151 | batteryLevel: battery.percentage, 152 | chargingState: battery.charging 153 | ? this.Characteristics.hap.ChargingState.CHARGING 154 | : this.Characteristics.hap.ChargingState.NOT_CHARGING, 155 | lowBatteryThreshold: 20 156 | }) 157 | } 158 | this.batteryService.values.batteryLevel = battery.percentage 159 | this.batteryService.values.chargingState = battery.charging 160 | ? this.Characteristics.hap.ChargingState.CHARGING 161 | : this.Characteristics.hap.ChargingState.NOT_CHARGING 162 | } catch (error) { 163 | this.error(error) 164 | } 165 | } 166 | 167 | async identify () { 168 | try { 169 | if (this.blinking) { 170 | return 171 | } 172 | this.blinking = true 173 | const on = await this.zpClient.getLedState() 174 | for (let n = 0; n < 10; n++) { 175 | this.zpClient.setLedState(n % 2 === 0) 176 | await once(this, 'heartbeat') 177 | } 178 | await this.zpClient.setLedState(on) 179 | this.blinking = false 180 | } catch (error) { 181 | this.error(error) 182 | } 183 | } 184 | } 185 | 186 | export { ZpAccessory } 187 | -------------------------------------------------------------------------------- /lib/ZpService/Speaker.js: -------------------------------------------------------------------------------- 1 | // homebridge-zp/lib/ZpService/Speaker.js 2 | // Copyright © 2016-2025 Erik Baauw. All rights reserved. 3 | // 4 | // Homebridge plugin for Sonos ZonePlayer. 5 | 6 | import { ZpService } from './index.js' 7 | 8 | class Speaker extends ZpService { 9 | constructor (zpAccessory, params = {}) { 10 | params.name = zpAccessory.zpClient.zoneName + ' Speakers' 11 | params.Service = zpAccessory.platform.config.SpeakerService 12 | params.subtype = 'zone' 13 | super(zpAccessory, params) 14 | this.debug('Speaker service') 15 | this.zpClient.on('message', (message) => { 16 | try { 17 | const f = `handle${message.device}${message.service}Event` 18 | if (this[f] != null) { 19 | this[f](message.parsedBody) 20 | } 21 | } catch (error) { 22 | this.error(error) 23 | } 24 | }) 25 | this.addCharacteristicDelegate({ 26 | key: 'on', 27 | Characteristic: this.Characteristics.hap.On, 28 | setter: async (value) => { 29 | try { 30 | if (value === this.values.on) { 31 | return 32 | } 33 | const platformCoordinator = this.platform.coordinators[this.zpClient.household] 34 | if (platformCoordinator === this.zpAccessory) { 35 | setTimeout(() => { 36 | this.values.on = false 37 | }, this.platform.config.resetTimeout) 38 | return 39 | } 40 | if (value) { 41 | const coordinator = platformCoordinator 42 | if (coordinator != null) { 43 | return this.zpClient.setAvTransportGroup(coordinator.zpClient.id) 44 | } else { 45 | // No coordinator yet. 46 | setTimeout(() => { 47 | this.values.on = false 48 | }, this.platform.config.resetTimeout) 49 | } 50 | } else { 51 | return this.zpClient.becomeCoordinatorOfStandaloneGroup() 52 | } 53 | } catch (error) { 54 | this.error(error) 55 | } 56 | }, 57 | timeout: 5000 58 | }) 59 | this.addCharacteristicDelegate({ 60 | key: 'volume', 61 | Characteristic: this.platform.config.VolumeCharacteristic, 62 | unit: '%', 63 | setter: this.zpClient.setVolume.bind(this.zpClient) 64 | }) 65 | this.addCharacteristicDelegate({ 66 | key: 'changeVolume', 67 | Characteristic: this.Characteristics.my.ChangeVolume, 68 | value: 0, 69 | setter: async (value) => { 70 | try { 71 | await this.zpClient.setRelativeVolume(value) 72 | } catch (error) { 73 | this.error(error) 74 | } 75 | setTimeout(() => { 76 | this.values.changeVolume = 0 77 | }, this.platform.config.resetTimeout) 78 | } 79 | }) 80 | this.addCharacteristicDelegate({ 81 | key: 'mute', 82 | Characteristic: this.Characteristics.hap.Mute, 83 | setter: this.zpClient.setMute.bind(this.zpClient) 84 | }) 85 | this.addCharacteristicDelegate({ 86 | key: 'loudness', 87 | Characteristic: this.Characteristics.my.Loudness, 88 | setter: this.zpClient.setLoudness.bind(this.zpClient) 89 | }) 90 | this.addCharacteristicDelegate({ 91 | key: 'bass', 92 | Characteristic: this.Characteristics.my.Bass, 93 | setter: this.zpClient.setBass.bind(this.zpClient) 94 | }) 95 | this.addCharacteristicDelegate({ 96 | key: 'treble', 97 | Characteristic: this.Characteristics.my.Treble, 98 | setter: this.zpClient.setTreble.bind(this.zpClient) 99 | }) 100 | if (this.zpClient.balance) { 101 | this.addCharacteristicDelegate({ 102 | key: 'balance', 103 | Characteristic: this.Characteristics.my.Balance, 104 | unit: '%', 105 | setter: this.zpClient.setBalance.bind(this.zpClient) 106 | }) 107 | } 108 | if (this.zpClient.tvIn) { 109 | this.addCharacteristicDelegate({ 110 | key: 'nightSound', 111 | Characteristic: this.Characteristics.my.NightSound, 112 | setter: this.zpClient.setNightSound.bind(this.zpClient) 113 | }) 114 | this.addCharacteristicDelegate({ 115 | key: 'speechEnhancement', 116 | Characteristic: this.Characteristics.my.SpeechEnhancement, 117 | setter: this.zpClient.setSpeechEnhancement.bind(this.zpClient) 118 | }) 119 | } 120 | if (/\(.*LS\+RS.*\)/.test(this.zpClient.zoneDisplayName)) { 121 | this.addCharacteristicDelegate({ 122 | key: 'surroundEnabled', 123 | Characteristic: this.Characteristics.my.SurroundEnabled, 124 | setter: this.zpClient.setSurroundEnable.bind(this.zpClient) 125 | }) 126 | this.addCharacteristicDelegate({ 127 | key: 'tvLevel', 128 | Characteristic: this.Characteristics.my.TvLevel, 129 | setter: this.zpClient.setTvLevel.bind(this.zpClient) 130 | }) 131 | this.addCharacteristicDelegate({ 132 | key: 'musicLevel', 133 | Characteristic: this.Characteristics.my.MusicLevel, 134 | setter: this.zpClient.setMusicLevel.bind(this.zpClient) 135 | }) 136 | this.addCharacteristicDelegate({ 137 | key: 'musicPlaybackFull', 138 | Characteristic: this.Characteristics.my.MusicPlaybackFull, 139 | setter: this.zpClient.setMusicPlaybackFull.bind(this.zpClient) 140 | }) 141 | this.addCharacteristicDelegate({ 142 | key: 'heightLevel', 143 | Characteristic: this.Characteristics.my.HeightLevel, 144 | setter: this.zpClient.setHeightLevel.bind(this.zpClient) 145 | }) 146 | } 147 | if (/\(.*Sub.*\)/.test(this.zpClient.zoneDisplayName)) { 148 | this.addCharacteristicDelegate({ 149 | key: 'subEnabled', 150 | Characteristic: this.Characteristics.my.SubEnabled, 151 | setter: this.zpClient.setSubEnable.bind(this.zpClient) 152 | }) 153 | this.addCharacteristicDelegate({ 154 | key: 'subLevel', 155 | Characteristic: this.Characteristics.my.SubLevel, 156 | setter: this.zpClient.setSubLevel.bind(this.zpClient) 157 | }) 158 | } 159 | 160 | this.emit('initialised') 161 | 162 | zpAccessory.once('initialised', async () => { 163 | try { 164 | await this.zpClient.subscribe('/MediaRenderer/RenderingControl/Event') 165 | } catch (error) { 166 | this.error(error) 167 | } 168 | }) 169 | } 170 | 171 | handleMediaRendererRenderingControlEvent (message) { 172 | if ( 173 | message.lastChange == null || 174 | !Array.isArray(message.lastChange) || 175 | message.lastChange[0] == null 176 | ) { 177 | return 178 | } 179 | const event = message.lastChange[0] 180 | if (event.volume != null && event.volume.master != null) { 181 | this.values.volume = event.volume.master 182 | if ( 183 | this.zpClient.balance && 184 | event.volume.lf != null && event.volume.rf != null 185 | ) { 186 | this.values.balance = event.volume.rf - event.volume.lf 187 | } 188 | } 189 | if (event.mute != null && event.mute.master != null) { 190 | this.values.mute = !!event.mute.master 191 | } 192 | if (event.loudness != null && event.loudness.master != null) { 193 | this.values.loudness = !!event.loudness.master 194 | } 195 | if (event.bass != null) { 196 | this.values.bass = event.bass 197 | } 198 | if (event.treble != null) { 199 | this.values.treble = event.treble 200 | } 201 | if (event.nightMode != null) { 202 | this.values.nightSound = !!event.nightMode 203 | } 204 | if (event.dialogLevel != null) { 205 | this.values.speechEnhancement = !!event.dialogLevel 206 | } 207 | if (event.surroundEnabled != null) { 208 | this.values.surroundEnabled = !!event.surroundEnabled 209 | } 210 | if (event.surroundLevel != null) { 211 | this.values.tvLevel = event.surroundLevel 212 | } 213 | if (event.musicSurroundLevel != null) { 214 | this.values.musicLevel = event.musicSurroundLevel 215 | } 216 | if (event.surroundMode != null) { 217 | this.values.musicPlaybackFull = !!event.surroundMode 218 | } 219 | if (event.heightChannelLevel != null) { 220 | this.values.heightLevel = event.heightChannelLevel 221 | } 222 | if (event.subEnabled != null) { 223 | this.values.subEnabled = !!event.subEnabled 224 | } 225 | if (event.subGain != null) { 226 | this.values.subLevel = event.subGain 227 | } 228 | } 229 | } 230 | 231 | ZpService.Speaker = Speaker 232 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /lib/ZpService/Tv.js: -------------------------------------------------------------------------------- 1 | // homebridge-zp/lib/ZpService/Tv.js 2 | // Copyright © 2016-2025 Erik Baauw. All rights reserved. 3 | // 4 | // Homebridge plugin for Sonos ZonePlayer. 5 | 6 | import { ZpService } from './index.js' 7 | 8 | const remoteKeys = {} 9 | const volumeSelectors = {} 10 | 11 | function initRemoteKeys (characteristicHap) { 12 | if (Object.keys(remoteKeys).length > 0) { 13 | return 14 | } 15 | remoteKeys[characteristicHap.RemoteKey.REWIND] = 'Rewind' 16 | remoteKeys[characteristicHap.RemoteKey.FAST_FORWARD] = 'Fast Forward' 17 | remoteKeys[characteristicHap.RemoteKey.NEXT_TRACK] = 'Next Track' 18 | remoteKeys[characteristicHap.RemoteKey.PREVIOUS_TRACK] = 'Previous Track' 19 | remoteKeys[characteristicHap.RemoteKey.ARROW_UP] = 'Up' 20 | remoteKeys[characteristicHap.RemoteKey.ARROW_DOWN] = 'Down' 21 | remoteKeys[characteristicHap.RemoteKey.ARROW_LEFT] = 'Left' 22 | remoteKeys[characteristicHap.RemoteKey.ARROW_RIGHT] = 'Right' 23 | remoteKeys[characteristicHap.RemoteKey.SELECT] = 'Select' 24 | remoteKeys[characteristicHap.RemoteKey.BACK] = 'Back' 25 | remoteKeys[characteristicHap.RemoteKey.EXIT] = 'Exit' 26 | remoteKeys[characteristicHap.RemoteKey.PLAY_PAUSE] = 'Play/Pause' 27 | remoteKeys[characteristicHap.RemoteKey.INFORMATION] = 'Info' 28 | volumeSelectors[characteristicHap.VolumeSelector.INCREMENT] = 'Up' 29 | volumeSelectors[characteristicHap.VolumeSelector.DECREMENT] = 'Down' 30 | } 31 | 32 | class Tv extends ZpService { 33 | static get Speaker () { return TvSpeaker } 34 | static get InputSource () { return TvInputSource } 35 | 36 | constructor (zpAccessory, params = {}) { 37 | params.name = params.master.sonosService.values.configuredName 38 | params.Service = zpAccessory.Services.hap.Television 39 | params.subtype = 'tv' 40 | params.primaryService = true 41 | super(zpAccessory, params) 42 | this.debug('TV service') 43 | initRemoteKeys(this.Characteristics.hap) 44 | 45 | this.zpMaster = params.master 46 | this.sonosService = this.zpMaster.sonosService 47 | this.sonosValues = this.sonosService.values 48 | this.sonosService.characteristicDelegate('configuredName') 49 | .on('didSet', (value) => { 50 | this.values.configuredName = value 51 | }) 52 | this.characteristicDelegate('configuredName') 53 | .on('didSet', (value) => { 54 | this.sonosService.values.configuredName = value 55 | }) 56 | 57 | this.speaker = new ZpService.Tv.Speaker(this.zpAccessory, { 58 | master: params.master 59 | }) 60 | 61 | // HomeKit doesn't like changes to service or characteristic properties, 62 | // so we create a static set of (disabled, hidden) InputSource services 63 | // to be configured later. 64 | this.sources = [] 65 | this.inputSources = [] 66 | this.displayOrder = [] 67 | for ( 68 | let identifier = 1; 69 | identifier <= this.platform.config.maxFavourites; 70 | identifier++ 71 | ) { 72 | const inputSource = new ZpService.Tv.InputSource(this.zpAccessory, { 73 | configuredName: 'Input ' + identifier, 74 | identifier, 75 | tvService: this 76 | }) 77 | this.inputSources.push(inputSource) 78 | this.displayOrder.push(0x01, 0x04, identifier & 0xff, 0x00, 0x00, 0x00) 79 | } 80 | this.displayOrder.push(0x00, 0x00) 81 | this.once('initialised', () => { 82 | this.sonosService.characteristicDelegate('platformCoordinatorId') 83 | .on('didSet', (value) => { 84 | this.updateGroupInputSource() 85 | }) 86 | }) 87 | 88 | this.addCharacteristicDelegate({ 89 | key: 'active', 90 | Characteristic: this.Characteristics.hap.Active, 91 | value: this.sonosValues.on 92 | ? this.Characteristics.hap.Active.ACTIVE 93 | : this.Characteristics.hap.Active.INACTIVE 94 | }).on('didSet', (value) => { 95 | this.sonosService.characteristicDelegate('on').setValue(value) 96 | }) 97 | this.sonosService.characteristicDelegate('on').on('didSet', (value) => { 98 | this.values.active = value ? 1 : 0 99 | }) 100 | const activeIdentifier = this.addCharacteristicDelegate({ 101 | key: 'activeIdentifier', 102 | Characteristic: this.Characteristics.hap.ActiveIdentifier, 103 | props: { maxValue: this.platform.config.maxFavourites }, 104 | silent: true, 105 | setter: async (value) => { 106 | try { 107 | if (value < 1 || value > this.platform.config.maxFavourites) { 108 | return 109 | } 110 | const source = this.sources[value - 1] 111 | this.log( 112 | 'set %s to %j', activeIdentifier.displayName, source.configuredName 113 | ) 114 | if (value === 1 && source.uri == null) { 115 | await this.zpClient.becomeCoordinatorOfStandaloneGroup() 116 | } else if (source.uri != null) { 117 | const zp = value <= 4 ? this.zpMaster : this.zpMaster.coordinator 118 | if (source.container) { 119 | await zp.zpClient.setAvTransportQueue( 120 | source.uri, source.meta 121 | ) 122 | } else { 123 | await zp.zpClient.setAvTransportUri( 124 | source.uri, source.meta 125 | ) 126 | } 127 | zp.zpClient.play().catch((error) => { this.error(error) }) 128 | if (value === 1) { 129 | // Joined a group 130 | setTimeout(() => { 131 | this.values.activeIdentifier = 132 | zp.sonosService.values.activeIdentifier 133 | }, this.platform.config.resetTimeout) 134 | } 135 | } 136 | } catch (error) { 137 | this.error(error) 138 | } 139 | this.ignoreDidSet = true 140 | } 141 | }).on('didSet', (value) => { 142 | this.sonosValues.activeIdentifier = value 143 | if (this.ignoreDidSet) { 144 | delete this.ignoreDidSet 145 | return 146 | } 147 | if (value > 0) { 148 | const source = this.sources[value - 1] 149 | this.log('set %s to %j', activeIdentifier.displayName, source.configuredName) 150 | } 151 | }) 152 | this.sonosService.characteristicDelegate('changeInput') 153 | .on('didSet', (value) => { 154 | if (value !== 0) { 155 | activeIdentifier.setValue(this.nextIdentifier(value)) 156 | } 157 | }) 158 | this.sonosService.characteristicDelegate('uri') 159 | .on('didSet', (value) => { 160 | const identifier = this.activeIdentifier(this.sonosValues.uri) 161 | this.values.activeIdentifier = identifier 162 | }) 163 | this.addCharacteristicDelegate({ 164 | key: 'sleepDiscoveryMode', 165 | Characteristic: this.Characteristics.hap.SleepDiscoveryMode, 166 | silent: true, 167 | value: this.Characteristics.hap.SleepDiscoveryMode.ALWAYS_DISCOVERABLE 168 | }) 169 | this.addCharacteristicDelegate({ 170 | key: 'displayOrder', 171 | Characteristic: this.Characteristics.hap.DisplayOrder, 172 | silent: true, 173 | value: Buffer.from(this.displayOrder).toString('base64') 174 | }) 175 | const remoteKey = this.addCharacteristicDelegate({ 176 | key: 'remoteKey', 177 | Characteristic: this.Characteristics.hap.RemoteKey, 178 | silent: true 179 | }).on('didSet', (value) => { 180 | this.log('%s: %s', remoteKey.displayName, remoteKeys[value]) 181 | switch (value) { 182 | case this.Characteristics.hap.RemoteKey.PLAY_PAUSE: 183 | { 184 | const value = 1 - this.values.active 185 | this.sonosService.characteristicDelegate('on').setValue(value) 186 | } 187 | break 188 | case this.Characteristics.hap.RemoteKey.ARROW_LEFT: 189 | this.sonosService.characteristicDelegate('changeTrack').setValue(-1) 190 | break 191 | case this.Characteristics.hap.RemoteKey.ARROW_RIGHT: 192 | this.sonosService.characteristicDelegate('changeTrack').setValue(1) 193 | break 194 | case this.Characteristics.hap.RemoteKey.ARROW_UP: 195 | activeIdentifier.setValue(this.nextIdentifier(-1)) 196 | break 197 | case this.Characteristics.hap.RemoteKey.ARROW_DOWN: 198 | activeIdentifier.setValue(this.nextIdentifier(1)) 199 | break 200 | default: 201 | break 202 | } 203 | }) 204 | this.addCharacteristicDelegate({ 205 | key: 'powerModeSelection', 206 | Characteristic: this.Characteristics.hap.PowerModeSelection 207 | }).on('didSet', (value) => { 208 | this.sonosService.characteristicDelegate('sonosCoordinator') 209 | .setValue(true) 210 | }) 211 | this.addCharacteristicDelegate({ 212 | key: 'statusFault', 213 | Characteristic: this.Characteristics.hap.StatusFault, 214 | value: this.Characteristics.hap.StatusFault.GENERAL_FAULT 215 | }) 216 | 217 | this.values.statusFault = this.Characteristics.hap.StatusFault.GENERAL_FAULT 218 | 219 | this.notYetInitialised = true 220 | this.favouritesUpdated() 221 | this.zpHousehold.on('favouritesUpdated', this.favouritesUpdated.bind(this)) 222 | } 223 | 224 | activeIdentifier (uri) { 225 | for (let i = 0; i < this.sources.length; i++) { 226 | if (this.sources[i].uri === uri) { 227 | return i + 1 228 | } 229 | } 230 | return 0 231 | } 232 | 233 | nextIdentifier (value) { 234 | let identifier = this.values.activeIdentifier 235 | const oldIdentifier = identifier 236 | do { 237 | identifier += value 238 | if (identifier < 2) { 239 | identifier = this.platform.config.maxFavourites - 1 240 | } 241 | if (identifier > this.platform.config.maxFavourites - 1) { 242 | identifier = 2 243 | } 244 | } while ( 245 | this.inputSources[identifier - 1].values.currentVisibilityState !== 246 | this.Characteristics.hap.CurrentVisibilityState.SHOWN && 247 | identifier !== oldIdentifier 248 | ) 249 | return identifier 250 | } 251 | 252 | favouritesUpdated () { 253 | const favs = this.zpHousehold.favourites 254 | if (favs == null) { 255 | return 256 | } 257 | this.sources = [] 258 | this.configureInputSource('none', null, false) 259 | this.updateGroupInputSource(true) 260 | this.configureInputSource( 261 | 'AirPlay', 'x-sonos-vli:' + this.zpClient.id + ':1', false 262 | ) 263 | this.configureInputSource( 264 | 'Audio In', 'x-rincon-stream:' + this.zpClient.id, this.zpClient.audioIn 265 | ) 266 | this.configureInputSource( 267 | 'TV', 'x-sonos-htastream:' + this.zpClient.id + ':spdif', 268 | this.zpClient.tvIn 269 | ) 270 | for (const key in favs) { 271 | const fav = favs[key] 272 | this.configureInputSource( 273 | key.slice(0, 64), fav.uri, true, fav.container, fav.meta 274 | ) 275 | } 276 | for ( 277 | let index = this.sources.length; 278 | index < this.platform.config.maxFavourites - 1; 279 | index++ 280 | ) { 281 | this.configureInputSource(`Input ${index + 1}`, null, false) 282 | } 283 | this.configureInputSource('Sonos Chime', 'x-rincon-buzzer:0', true) 284 | this.log( 285 | 'input sources: %j', 286 | this.sources.filter((source) => { 287 | return source.visible 288 | }).map((source) => { 289 | return source.configuredName 290 | }) 291 | ) 292 | if (this.notYetInitialised) { 293 | delete this.notYetInitialised 294 | this.emit('initialised') 295 | } 296 | this.values.activeIdentifier = this.activeIdentifier(this.sonosValues.uri) 297 | } 298 | 299 | updateGroupInputSource (silent = false) { 300 | const index = 0 301 | const source = this.sources[index] 302 | const inputSource = this.inputSources[index] 303 | if (source == null || inputSource == null) { 304 | return 305 | } 306 | 307 | const platformCoordinatorId = this.sonosValues.platformCoordinatorId 308 | const zpClient = this.platform.zpClients[platformCoordinatorId] 309 | let configuredName = 'none' 310 | let uri 311 | let visible = false 312 | if ( 313 | this.sonosValues.sonosGroup != null && 314 | this.sonosValues.sonosGroup !== this.zpClient.zoneName 315 | ) { 316 | configuredName = 'Leave ' + this.sonosValues.sonosGroup 317 | visible = true 318 | } else if ( 319 | platformCoordinatorId != null && 320 | platformCoordinatorId !== this.zpClient.id && 321 | zpClient != null 322 | ) { 323 | configuredName = 'Join ' + zpClient.zoneGroupShortName 324 | uri = 'x-rincon:' + platformCoordinatorId 325 | visible = true 326 | } 327 | source.configuredName = configuredName 328 | source.uri = uri 329 | source.visible = visible 330 | inputSource.values.configuredName = configuredName 331 | inputSource.values.isConfigured = visible 332 | ? this.Characteristics.hap.IsConfigured.CONFIGURED 333 | : this.Characteristics.hap.IsConfigured.NOT_CONFIGURED 334 | inputSource.values.targetVisibilityState = visible 335 | ? this.Characteristics.hap.TargetVisibilityState.SHOWN 336 | : this.Characteristics.hap.TargetVisibilityState.HIDDEN 337 | if (!silent) { 338 | this.log( 339 | 'Input Sources: %j', 340 | this.sources.filter((source) => { 341 | return source.visible 342 | }).map((source) => { 343 | return source.configuredName 344 | }) 345 | ) 346 | } 347 | } 348 | 349 | configureInputSource (configuredName, uri, visible, container, meta) { 350 | this.sources.push({ configuredName, uri, visible, container, meta }) 351 | const identifier = this.sources.length 352 | if (identifier <= this.platform.config.maxFavourites) { 353 | const inputSource = this.inputSources[identifier - 1] 354 | inputSource.values.configuredName = configuredName 355 | inputSource.values.isConfigured = visible 356 | ? this.Characteristics.hap.IsConfigured.CONFIGURED 357 | : this.Characteristics.hap.IsConfigured.NOT_CONFIGURED 358 | if (configuredName === 'Sonos Chime') { 359 | visible = false 360 | } 361 | inputSource.values.targetVisibilityState = visible 362 | ? this.Characteristics.hap.TargetVisibilityState.SHOWN 363 | : this.Characteristics.hap.TargetVisibilityState.HIDDEN 364 | if (configuredName === 'AirPlay') { 365 | inputSource.values.inputSourceType = 366 | this.Characteristics.hap.InputSourceType.OTHER 367 | } else if (configuredName === 'TV') { 368 | inputSource.values.inputSourceType = 369 | this.Characteristics.hap.InputSourceType.HDMI 370 | } else if (uri != null && uri.startsWith('x-sonosapi-stream:')) { 371 | inputSource.values.inputSourceType = 372 | this.Characteristics.hap.InputSourceType.TUNER 373 | } 374 | } 375 | } 376 | } 377 | 378 | class TvSpeaker extends ZpService { 379 | constructor (zpAccessory, params = {}) { 380 | params.name = zpAccessory.zpClient.zoneName + ' TV Speaker' 381 | params.Service = zpAccessory.Services.hap.TelevisionSpeaker 382 | params.subtype = 'tvSpeaker' 383 | super(zpAccessory, params) 384 | const service = params.master.speakerService == null 385 | ? params.master.sonosService 386 | : params.master.speakerService 387 | this.addCharacteristicDelegate({ 388 | key: 'volumeControlType', 389 | Characteristic: this.Characteristics.hap.VolumeControlType, 390 | silent: true, 391 | value: this.Characteristics.hap.VolumeControlType.ABSOLUTE 392 | }) 393 | const volumeSelector = this.addCharacteristicDelegate({ 394 | key: 'volumeSelector', 395 | Characteristic: this.Characteristics.hap.VolumeSelector, 396 | silent: true 397 | }).on('didSet', (value) => { 398 | this.log('%s: %s', volumeSelector.displayName, volumeSelectors[value]) 399 | service.characteristicDelegate('changeVolume').setValue( 400 | value === this.Characteristics.hap.VolumeSelector.INCREMENT ? 1 : -1 401 | ) 402 | }) 403 | this.addCharacteristicDelegate({ 404 | key: 'mute', 405 | Characteristic: this.Characteristics.hap.Mute 406 | }).on('didSet', (value) => { 407 | service.characteristicDelegate('mute').setValue(value) 408 | }) 409 | this.emit('initialised') 410 | } 411 | } 412 | 413 | class TvInputSource extends ZpService { 414 | constructor (zpAccessory, params = {}) { 415 | params.name = params.configuredName 416 | params.Service = zpAccessory.Services.hap.InputSource 417 | params.subtype = 'tvInput' + params.identifier 418 | params.linkedServiceDelegate = params.tvService 419 | super(zpAccessory, params) 420 | this.tvService = params.tvService 421 | this.values.configuredName = params.configuredName 422 | this.addCharacteristicDelegate({ 423 | key: 'identifier', 424 | Characteristic: this.Characteristics.hap.Identifier, 425 | silent: true, 426 | value: params.identifier 427 | }) 428 | this.addCharacteristicDelegate({ 429 | key: 'inputSourceType', 430 | Characteristic: this.Characteristics.hap.InputSourceType, 431 | silent: true, 432 | value: this.Characteristics.hap.InputSourceType.OTHER 433 | }) 434 | this.addCharacteristicDelegate({ 435 | key: 'inputDeviceType', 436 | Characteristic: this.Characteristics.hap.InputDeviceType, 437 | silent: true, 438 | value: this.Characteristics.hap.InputDeviceType.AUDIO_SYSTEM 439 | }) 440 | this.addCharacteristicDelegate({ 441 | key: 'isConfigured', 442 | Characteristic: this.Characteristics.hap.IsConfigured, 443 | silent: true, 444 | value: this.Characteristics.hap.IsConfigured.NOT_CONFIGURED 445 | }) 446 | this.addCharacteristicDelegate({ 447 | key: 'currentVisibilityState', 448 | Characteristic: this.Characteristics.hap.CurrentVisibilityState, 449 | silent: true, 450 | value: this.Characteristics.hap.CurrentVisibilityState.HIDDEN 451 | }) 452 | this.addCharacteristicDelegate({ 453 | key: 'targetVisibilityState', 454 | Characteristic: this.Characteristics.hap.TargetVisibilityState, 455 | silent: true, 456 | value: this.Characteristics.hap.TargetVisibilityState.HIDDEN 457 | }).on('didSet', (value) => { 458 | this.values.currentVisibilityState = 459 | value === this.Characteristics.hap.TargetVisibilityState.SHOWN 460 | ? this.Characteristics.hap.CurrentVisibilityState.SHOWN 461 | : this.Characteristics.hap.CurrentVisibilityState.HIDDEN 462 | }) 463 | this.emit('initialised') 464 | } 465 | } 466 | 467 | ZpService.Tv = Tv 468 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |
3 |