Add and remove Snapcast client/group callbacks properly (#77624)

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
luar123 2022-09-01 14:52:06 +02:00 committed by GitHub
parent d1ecd74a1a
commit 8afcde4ea9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -125,10 +125,17 @@ class SnapcastGroupDevice(MediaPlayerEntity):
def __init__(self, group, uid_part):
"""Initialize the Snapcast group device."""
group.set_callback(self.schedule_update_ha_state)
self._group = group
self._uid = f"{GROUP_PREFIX}{uid_part}_{self._group.identifier}"
async def async_added_to_hass(self) -> None:
"""Subscribe to group events."""
self._group.set_callback(self.schedule_update_ha_state)
async def async_will_remove_from_hass(self) -> None:
"""Disconnect group object when removed."""
self._group.set_callback(None)
@property
def state(self):
"""Return the state of the player."""
@ -213,10 +220,17 @@ class SnapcastClientDevice(MediaPlayerEntity):
def __init__(self, client, uid_part):
"""Initialize the Snapcast client device."""
client.set_callback(self.schedule_update_ha_state)
self._client = client
self._uid = f"{CLIENT_PREFIX}{uid_part}_{self._client.identifier}"
async def async_added_to_hass(self) -> None:
"""Subscribe to client events."""
self._client.set_callback(self.schedule_update_ha_state)
async def async_will_remove_from_hass(self) -> None:
"""Disconnect client object when removed."""
self._client.set_callback(None)
@property
def unique_id(self):
"""