From 68a01562ecfa335ed872ff5d45bc803bb0d04eb1 Mon Sep 17 00:00:00 2001 From: luar123 <49960470+luar123@users.noreply.github.com> Date: Thu, 1 Sep 2022 14:52:06 +0200 Subject: [PATCH] Add and remove Snapcast client/group callbacks properly (#77624) Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --- .../components/snapcast/media_player.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index 703cb41a38f..0e6524c8504 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -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): """