diff --git a/custom_components/nvidia_shield/media_player.py b/custom_components/nvidia_shield/media_player.py index 8396b9d..9a8ee94 100644 --- a/custom_components/nvidia_shield/media_player.py +++ b/custom_components/nvidia_shield/media_player.py @@ -192,8 +192,13 @@ class NvidiaShieldMediaPlayer(MediaPlayerEntity): # Registered at setup; unregistered at unload when the last # participating entity goes away. if self._entry.options.get(CONF_ADB_COMMAND_ENABLED, False): - entity_id = getattr(self, "entity_id", None) or self._attr_unique_id - _ADB_COMMAND_TARGETS[entity_id] = self._manager + # F4: the registry key is computed once and stored on the + # entity, so add/remove always agree even when entity_id is + # assigned by HA core after the first read. + self._adb_command_key = ( + getattr(self, "entity_id", None) or self._attr_unique_id + ) + _ADB_COMMAND_TARGETS[self._adb_command_key] = self._manager _adb_command_service_ensure(self.hass) # Discover power sensor siblings from the power switch @@ -231,9 +236,12 @@ class NvidiaShieldMediaPlayer(MediaPlayerEntity): for task in self._entity_tasks: task.cancel() self._entity_tasks.clear() - # P4-2a: drop this entity's service participation - entity_id = getattr(self, "entity_id", None) or self._attr_unique_id - if _ADB_COMMAND_TARGETS.pop(entity_id, None) is not None and self.hass: + # P4-2a: drop this entity's service participation (same key as add) + if ( + (key := getattr(self, "_adb_command_key", None)) is not None + and _ADB_COMMAND_TARGETS.pop(key, None) is not None + and self.hass + ): _adb_command_service_maybe_remove(self.hass)