1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00

Fix the unregistration of Capability based MusicCast Entities (#63665)

This commit is contained in:
micha91 2022-01-08 19:42:40 +01:00 committed by GitHub
parent a754584284
commit 073a080bb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 13 deletions

View File

@ -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."""

View File

@ -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
)