From 073a080bb2ed4d53151fecb41113ca3df5d5fecf Mon Sep 17 00:00:00 2001 From: micha91 Date: Sat, 8 Jan 2022 19:42:40 +0100 Subject: [PATCH] Fix the unregistration of Capability based MusicCast Entities (#63665) --- .../components/yamaha_musiccast/__init__.py | 22 +++++++++---------- .../yamaha_musiccast/media_player.py | 2 -- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/yamaha_musiccast/__init__.py b/homeassistant/components/yamaha_musiccast/__init__.py index 98fed4d0f63..d984aaceb96 100644 --- a/homeassistant/components/yamaha_musiccast/__init__.py +++ b/homeassistant/components/yamaha_musiccast/__init__.py @@ -199,6 +199,17 @@ class MusicCastDeviceEntity(MusicCastEntity): return device_info + async def async_added_to_hass(self): + """Run when this Entity has been added to HA.""" + await super().async_added_to_hass() + # All entities should register callbacks to update HA when their state changes + self.coordinator.musiccast.register_callback(self.async_write_ha_state) + + async def async_will_remove_from_hass(self): + """Entity being removed from hass.""" + await super().async_will_remove_from_hass() + self.coordinator.musiccast.remove_callback(self.async_write_ha_state) + class MusicCastCapabilityEntity(MusicCastDeviceEntity): """Base Entity type for all capabilities.""" @@ -216,17 +227,6 @@ class MusicCastCapabilityEntity(MusicCastDeviceEntity): super().__init__(name=capability.name, icon="", coordinator=coordinator) self._attr_entity_category = ENTITY_CATEGORY_MAPPING.get(capability.entity_type) - async def async_added_to_hass(self): - """Run when this Entity has been added to HA.""" - await super().async_added_to_hass() - # All capability based entities should register callbacks to update HA when their state changes - self.coordinator.musiccast.register_callback(self.async_write_ha_state) - - async def async_will_remove_from_hass(self): - """Entity being removed from hass.""" - await super().async_added_to_hass() - self.coordinator.musiccast.remove_callback(self.async_write_ha_state) - @property def unique_id(self) -> str: """Return the unique ID for this entity.""" diff --git a/homeassistant/components/yamaha_musiccast/media_player.py b/homeassistant/components/yamaha_musiccast/media_player.py index b1d0bdcd2e9..9407036fcd9 100644 --- a/homeassistant/components/yamaha_musiccast/media_player.py +++ b/homeassistant/components/yamaha_musiccast/media_player.py @@ -162,7 +162,6 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity): await super().async_added_to_hass() self.coordinator.entities.append(self) # Sensors should also register callbacks to HA when their state changes - self.coordinator.musiccast.register_callback(self.async_write_ha_state) self.coordinator.musiccast.register_group_update_callback( self.update_all_mc_entities ) @@ -173,7 +172,6 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity): await super().async_will_remove_from_hass() self.coordinator.entities.remove(self) # The opposite of async_added_to_hass. Remove any registered call backs here. - self.coordinator.musiccast.remove_callback(self.async_write_ha_state) self.coordinator.musiccast.remove_group_update_callback( self.update_all_mc_entities )