ebaauw / homebridge-zp

Homebridge plugin for Sonos ZonePlayer
Apache License 2.0
243 stars 20 forks source link

Understanding Coordinator Logic #194

Closed rfiorentino1 closed 2 years ago

rfiorentino1 commented 2 years ago

Issue

My apologies for not posting this in the Discord channel, for some reason it won't load any messages etc and won't let me post.

I'm struggling to understand the logic of coordinators within ZP. I've read through the readMe and some references in other issues etc, but still am having trouble getting it to do what I need. Currently, I'm using Sonos Multi-Room to manage Sonos groups from within HomeKit, but that plugin has become unreliable and I'd really like to keep everything inside of ZP at this point.

The multi-room plugin has the ability for you to set priority zones for each individual player, so that it looks to those zones in order of priority to group. I really appreciate this, and I'm trying to figure out how to go about setting this up for ZP. I've tried setting a zone as a coordinator, and turning it's corresponding speaker service on, in that order, in a scene. This seems to work just fine, though there are multiple instances where a zone is playing, but it's speaker service shows as off, and vice-versa, not sure why. However, my problem is when grouping/ungrouping rooms. Ideally, I'd like to just be able to turn the speakers service on or off for a given zone/room, and have the coordinator be dealt with automatically somehow. In conjunction with the Pico Remotes, right now I can just double tap the top button in any room to toggle joining/leaving the current group of Sonos zones, and I'm trying to figure out the best way to accomplish this with ZP coordinators/speakers services. Please let me know if I need to elaborate on anything further. :) Thank you again for all of your time/effort into this plugin, it's much appreciated.

ebaauw commented 2 years ago

Basically, zone groups consist of a coordinator zone and one or more member zones. The member zones set their input stream to the (master zone player of the) coordinator zone. Technically, a member zone joining or leaving a group is just selecting a different audio source for that member zone. A coordinator leaving a group is somewhat more complicated: a member takes over the coordinator role, changing it’s input to the former coordinator’s input (hence the disruption), and the other members set their input to the new coordinator.

There’s no easy HomeKit equivalent for groups, so Homebridge ZP uses the following scheme:

rfiorentino1 commented 2 years ago

Thank you for explaining all of that, that makes total sense. In the use case example above, setting a target coordinator makes sense, because there's only one source. The problem becomes when there are multiple players playing the same source that need to be joined, and/or multiple streams being played by a certain group of players that needs to be joined in order of importance. For example:

Currently, I don't see an easy solution for the last two examples above, or situations like them, using coordinators/scenes. This is where the priority preferences of Sonos Multiroom are helpful, because it essentially decides which coordinator to use with which zones automatically, based on your configuration. The only way I could see doing this with coordinators, would be making conditional rules in Eve for every player state/group combination possible, and then toggling the additional logic for presence detection/Pico Remotes etc. Taking just the second example above might look something like:

Obviously this will get pretty complicated pretty quickly, which begs the question, is it easier to create this type of if-else logic within ZP somehow, or do it through homekit/eve automation.

The multi-room plugin works amazingly for this purpose, the problem is that it's currently suffering from the disappearing/reappearing zones issue you tackled a little while ago. Otherwise, I wouldn't be trying to figure this out in the first place. :) Apologies for the lengthy post, just trying to illustrate what I'm trying to do/possible solutions. Open to any suggestions/thoughts etc.

ebaauw commented 2 years ago

I think you can achieve what you want by using automations to set Sonos Coordinator on play/pause, rather then figuring out what to do when joining a zone. If a higher priority zone starts playing, set Sonos Coordinator for that zone, so any subsequent joins will be to that zone. If a higher priority zone stops, set Sonos Coordinator to the next zone playing. Note that when setting Sonos Coordinator on a zone, it is cleared automatically on the other zones.

So assume you have three potential coordinators, A, B, and C, in order of prio. Your automations would be: A turns on -> set coordinator on A B turns on and A is off -> set coordinator on B C turns on and A is off and B I’d off -> set coordinator on C A turns off and B is on -> set coordinator on B A turn off and B is off and C is on -> set coordinator on C B turns off and B coordinator and C is on -> set coordinator on C C turns off and C is coordinator -> clear coordinator on C

If coordinator isn’t set on any zone, turning on a Speaker service does nothing. So from your remote, you’d simply turn on the Speaker.

ebaauw commented 2 years ago

Damn, I disabled turning Sonos Coordinator off, to make sure turning a Speaker service on would do something (except before setting Sonos Coordinator on the first time after installing Homebridge ZP.

rfiorentino1 commented 2 years ago

But I'm assuming that's ok, because I don't need to turn off coordinators, I just need to set new ones, right? I see what you mean by making the play state the deciding factor, which makes sense. But I'd still have to create automations of coordinators based on every player state combination. In my case I have 5 zones, which would mean I'd need to create at least 60 automations haha. But if that's the only way, that's what I'll do as a better longterm solution :)

ebaauw commented 2 years ago

The automation logic needs to be implemented somewhere. In my view, a Homebridge plugin should just expose devices to HomeKit; any automation logic should either be implemented by the device or in HomeKit, but not by the plugin. For really complex logic you’d probably need something like Home Assistant or Domoticz, but I still manage to do everything I need in deCONZ (for my Zigbee devices) and in HomeKit (for the non-Zigbee devices, like Sonos), not needing yet another component.

rfiorentino1 commented 2 years ago

Yeah, that makes sense too. But I think it would be more practical to set coordinators based on a single zone's priority list, than to check for every other combination of zones playing and then set coordinators accordingly. If setting based on a list, it doesn't matter what other players are doing, as long as one that is listed in the priorities is active. In HomeKit/eve, there's no way to set up this type of logic without accounting for all other player states.

rfiorentino1 commented 2 years ago

Not sure if this would be helpful, but here is the related script file that Multiroom uses to group zones. Check out the lines that start with "// Cycles over the priority list and checks the play state"

sonos-zone.txt

rwgunther commented 1 year ago

This thread was really helpful for me. I'd recommend adding instructions in the read.me that address using the additional speaker service and the coordinator property to add devices to a group. I'd been using this for months and couldn't figure out how to do it until I read through this thread.