Chore: Minor fix for sonos in airplay mode
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Sat, 1 Feb 2025 18:20:24 +0000 (19:20 +0100)
committerMarcel van der Veldt <m.vanderveldt@outlook.com>
Sat, 1 Feb 2025 18:20:24 +0000 (19:20 +0100)
music_assistant/controllers/players.py
music_assistant/providers/sonos/provider.py

index c72fde85b55d8ab8bc1cf1b69698aeb4ee0bdc08..6c637c7718afa2008be512654f46cec613f9dcea 100644 (file)
@@ -1290,7 +1290,7 @@ class PlayerController(CoreController):
             player.available = False
         # if the PlayerQueue was playing, restart playback
         # TODO: add property to ConfigEntry if it requires a restart of playback on change
-        elif not player_disabled and resume_queue.state == PlayerState.PLAYING:
+        elif not player_disabled and resume_queue and resume_queue.state == PlayerState.PLAYING:
             self.mass.call_later(1, self.mass.player_queues.resume, resume_queue.queue_id)
         # check for group memberships that need to be updated
         if player_disabled and player.active_group and player_provider:
index a9327f894910918d336c21af5b72ac1b06d228aa..40e1dfdc437f1c58c2d2bf91a8bf59172cccc826 100644 (file)
@@ -200,15 +200,14 @@ class SonosPlayerProvider(PlayerProvider):
         if "values/airplay_mode" in changed_keys and (
             (sonos_player := self.sonos_players.get(config.player_id))
             and (airplay_player := sonos_player.get_linked_airplay_player(False))
-            and airplay_player.active_source == sonos_player.mass_player.active_source
+            and airplay_player.state in (PlayerState.PLAYING, PlayerState.PAUSED)
         ):
             # edge case: we switched from airplay mode to sonos mode (or vice versa)
             # we need to make sure that playback gets stopped on the airplay player
             if airplay_prov := self.mass.get_provider(airplay_player.provider):
+                airplay_player.active_source = None
                 await airplay_prov.cmd_stop(airplay_player.player_id)
                 airplay_player.active_source = None
-            if not sonos_player.airplay_mode_enabled:
-                await self.mass.players.cmd_stop(config.player_id)
 
     async def cmd_stop(self, player_id: str) -> None:
         """Send STOP command to given player."""
@@ -260,15 +259,15 @@ class SonosPlayerProvider(PlayerProvider):
         if airplay_player := sonos_player.get_linked_airplay_player(False):
             # if airplay mode is enabled, we could possibly receive child player id's that are
             # not Sonos players, but Airplay players. We redirect those.
-            if (
-                airplay_player.active_source == sonos_player.mass_player.active_source
-                and airplay_player.state == PlayerState.PLAYING
-            ):
-                # edge case player is not playing a MA queue - fail this request
-                raise PlayerCommandFailed("Player is not playing a Music Assistant queue.")
             airplay_child_ids = [x for x in child_player_ids if x.startswith("ap")]
             child_player_ids = [x for x in child_player_ids if x not in airplay_child_ids]
             if airplay_child_ids:
+                if (
+                    airplay_player.active_source != sonos_player.mass_player.active_source
+                    and airplay_player.state == PlayerState.PLAYING
+                ):
+                    # edge case player is not playing a MA queue - fail this request
+                    raise PlayerCommandFailed("Player is not playing a Music Assistant queue.")
                 await self.mass.players.cmd_group_many(airplay_player.player_id, airplay_child_ids)
         if child_player_ids:
             await sonos_player.client.player.group.modify_group_members(