diff --git a/app/src/full/java/io/homeassistant/companion/android/thread/ThreadManagerImpl.kt b/app/src/full/java/io/homeassistant/companion/android/thread/ThreadManagerImpl.kt index 969d938c517..f7ab54279f8 100644 --- a/app/src/full/java/io/homeassistant/companion/android/thread/ThreadManagerImpl.kt +++ b/app/src/full/java/io/homeassistant/companion/android/thread/ThreadManagerImpl.kt @@ -65,7 +65,9 @@ class ThreadManagerImpl @Inject constructor( return if (deviceThreadIntent == null && coreThreadDataset != null) { try { importDatasetFromServer(context, coreThreadDataset.datasetId, coreThreadDataset.preferredBorderAgentId, serverId) - serverManager.integrationRepository(serverId).setThreadBorderAgentIds(listOf((coreThreadDataset.preferredBorderAgentId ?: BORDER_AGENT_ID))) + coreThreadDataset.preferredBorderAgentId?.let { + serverManager.integrationRepository(serverId).setThreadBorderAgentIds(listOf(it)) + } // else added using placeholder, will be removed when core is updated Log.d(TAG, "Thread import to device completed") ThreadManager.SyncResult.OnlyOnServer(imported = true) } catch (e: Exception) { @@ -93,11 +95,7 @@ class ThreadManagerImpl @Inject constructor( try { val localIds = serverManager.defaultServers.flatMap { serverManager.integrationRepository(it.id).getThreadBorderAgentIds() - }.toMutableList() - if (localIds.isEmpty()) { // Prefers something from HA, must've been added before BA ID logic - localIds += BORDER_AGENT_ID } - updated = if (coreThreadDataset.source != "Google") { // Credential from HA, update localIds.filter { it != coreThreadDataset.preferredBorderAgentId }.forEach { baId -> try { @@ -109,13 +107,14 @@ class ThreadManagerImpl @Inject constructor( importDatasetFromServer(context, coreThreadDataset.datasetId, coreThreadDataset.preferredBorderAgentId, serverId) serverManager.defaultServers.forEach { serverManager.integrationRepository(it.id).setThreadBorderAgentIds( - if (it.id == serverId) { - listOf(coreThreadDataset.preferredBorderAgentId ?: BORDER_AGENT_ID) + if (it.id == serverId && coreThreadDataset.preferredBorderAgentId != null) { + listOf(coreThreadDataset.preferredBorderAgentId!!) } else { emptyList() } ) } + Log.d(TAG, "Thread update device completed: deleted ${localIds.size} datasets, updated 1") true } else { // Core prefers imported from other app, this shouldn't be managed by HA localIds.forEach { baId -> @@ -128,9 +127,9 @@ class ThreadManagerImpl @Inject constructor( serverManager.defaultServers.forEach { serverManager.integrationRepository(it.id).setThreadBorderAgentIds(emptyList()) } + Log.d(TAG, "Thread update device completed: deleted ${localIds.size} datasets") false } - Log.d(TAG, "Thread update device completed") } catch (e: Exception) { Log.e(TAG, "Thread update device failed", e) } @@ -211,7 +210,13 @@ class ThreadManagerImpl @Inject constructor( .addOnFailureListener { cont.resume(null) } } return try { - appCredentials?.any { isPreferredCredentials(context, it) } ?: false + appCredentials?.any { + val isPreferred = isPreferredCredentials(context, it) + if (isPreferred) { + Log.d(TAG, "Thread device prefers app added dataset: ${it.networkName} (PAN ${it.panId}, EXTPAN ${it.extendedPanId})") + } + isPreferred + } ?: false } catch (e: Exception) { Log.e(TAG, "Thread app added credentials preferred check failed", e) false @@ -239,7 +244,15 @@ class ThreadManagerImpl @Inject constructor( } private suspend fun deleteOrphanedThreadCredentials(context: Context, serverId: Int) { - val orphanedCredentials = serverManager.integrationRepository(serverId).getThreadBorderAgentIds() + if (serverManager.defaultServers.all { it.version?.isAtLeast(2023, 9) == true }) { + try { + deleteThreadCredential(context, BORDER_AGENT_ID) + } catch (e: Exception) { + // Expected, it may not exist + } + } + + val orphanedCredentials = serverManager.integrationRepository(serverId).getOrphanedThreadBorderAgentIds() if (orphanedCredentials.isEmpty()) return orphanedCredentials.forEach {