Skip to content

Commit

Permalink
Small tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Oct 18, 2024
1 parent 8c69bd8 commit 8f69ab1
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions aiosonos/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class SonosPlayer:
"""Representation of a Sonos Player."""

_active_group: SonosGroup
_active_group: SonosGroup | None

def __init__(self, client: SonosLocalApiClient, data: PlayerData) -> None:
"""Handle initialization."""
Expand All @@ -29,6 +29,8 @@ def __init__(self, client: SonosLocalApiClient, data: PlayerData) -> None:
if group.coordinator_id == self.id or self.id in group.player_ids:
self._active_group = group
break
else:
self._active_group = None

async def async_init(self) -> None:
"""Handle Async initialization."""
Expand Down Expand Up @@ -70,23 +72,29 @@ def has_fixed_volume(self) -> bool | None:
return self._volume_data.get("fixed")

@property
def group(self) -> SonosGroup:
def group(self) -> SonosGroup | None:
"""Return the active group."""
return self._active_group

@property
def is_coordinator(self) -> bool:
"""Return if this player is the coordinator of the active group it belongs to."""
if not self.group:
return False
return self.group.coordinator_id == self.id

@property
def is_passive(self) -> bool:
"""Return if this player is the NOT a coordinator but a passive member of a group."""
if not self.group:
return False
return self.group.coordinator_id != self.id

@property
def group_members(self) -> list[str]:
"""Return the player ids of the group members."""
if not self.group:
return []
return self.group.player_ids

async def set_volume(
Expand All @@ -103,6 +111,8 @@ async def duck(self, duration_millis: int | None = None) -> None:

async def leave_group(self) -> None:
"""Leave the active group this player is joined to (if any)."""
if not self.group:
return
await self.client.api.groups.modify_group_members(
self.group.id,
player_ids_to_add=[],
Expand Down Expand Up @@ -151,7 +161,7 @@ def update_data(self, data: PlayerData) -> None:

def check_active_group(self) -> None:
"""Check/set the active group of this player."""
prev_group_id = self.group.id
prev_group_id = self._active_group.id if self._active_group else None
for group in self.client.groups:
if group.coordinator_id == self.id or self.id in group.player_ids:
self._active_group = group
Expand Down

0 comments on commit 8f69ab1

Please sign in to comment.