Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
!packageManager.isAutomotive()

override suspend fun coreSupportsThread(serverId: Int): Boolean {
if (!serverManager.isRegistered() || serverManager.getServer(serverId)?.user?.isAdmin != true) return false
if (!serverManager.isRegistered() || serverManager.getServer(serverId) == null) return false
val config = serverManager.webSocketRepository(serverId).getConfig()
return config != null &&
config.components.contains("thread") &&
HomeAssistantVersion.fromString(config.version)?.isAtLeast(2023, 3, 0) == true
}

private fun userIsAdmin(serverId: Int): Boolean =

Check failure

Code scanning / ktlint

First line of body expression fits on same line as function signature Error

First line of body expression fits on same line as function signature

Check failure

Code scanning / ktlint

First line of body expression fits on same line as function signature Error

First line of body expression fits on same line as function signature
serverManager.getServer(serverId)?.user?.isAdmin == true

private suspend fun getDatasetsFromServer(serverId: Int): List<ThreadDatasetResponse>? =
serverManager.webSocketRepository(serverId).getThreadDatasets()

Expand All @@ -55,6 +58,7 @@
): ThreadManager.SyncResult {
if (!appSupportsThread()) return ThreadManager.SyncResult.AppUnsupported
if (!coreSupportsThread(serverId)) return ThreadManager.SyncResult.ServerUnsupported
if (!userIsAdmin(serverId)) return ThreadManager.SyncResult.ServerUserNotAdmin

return if (exportOnly) { // Limited sync, only export non-app dataset
exportSyncPreferredDataset(context)
Expand Down Expand Up @@ -296,7 +300,7 @@
}

override suspend fun sendThreadDatasetExportResult(result: ActivityResult, serverId: Int): String? {
if (result.resultCode == Activity.RESULT_OK && coreSupportsThread(serverId)) {
if (result.resultCode == Activity.RESULT_OK && coreSupportsThread(serverId) && userIsAdmin(serverId)) {
val threadNetworkCredentials = ThreadNetworkCredentials.fromIntentSenderResultData(result.data!!)
try {
val added = serverManager.webSocketRepository(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class DeveloperSettingsPresenterImpl @Inject constructor(
context.getString(commonR.string.thread_debug_result_unsupported_server),
false,
)
is ThreadManager.SyncResult.ServerUserNotAdmin ->
view.onThreadDebugResult(
context.getString(commonR.string.thread_debug_result_user_not_admin),
false,
)
is ThreadManager.SyncResult.OnlyOnServer -> {
if (syncResult.imported) {
view.onThreadDebugResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface ThreadManager {
sealed class SyncResult {
object AppUnsupported : SyncResult()
object ServerUnsupported : SyncResult()
object ServerUserNotAdmin : SyncResult()
object NotConnected : SyncResult()
Comment thread
agners marked this conversation as resolved.
class OnlyOnServer(val imported: Boolean) : SyncResult()
class OnlyOnDevice(val exportIntent: IntentSender?) : SyncResult()
Expand All @@ -29,7 +30,9 @@ interface ThreadManager {
fun appSupportsThread(): Boolean

/**
* Indicates if the server supports Thread credential management.
* Indicates if the server has the Thread component installed and is on a recent enough
* Home Assistant version for credential management. This does not consider whether the
* signed-in user is allowed to manage Thread credentials (which requires admin privileges).
*/
suspend fun coreSupportsThread(serverId: Int): Boolean

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ enum class MatterThreadStep {
ERROR_MATTER_CANCELLED,
ERROR_MATTER_OTHER,
ERROR_THREAD_LOCAL_NETWORK,
ERROR_THREAD_USER_NOT_ADMIN,
ERROR_THREAD_OTHER,
}
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,15 @@ class WebViewActivity :
presenter.finishMatterThreadFlow()
}

MatterThreadStep.ERROR_THREAD_USER_NOT_ADMIN -> {
alertDialog?.cancel()
AlertDialog.Builder(this@WebViewActivity)
.setMessage(commonR.string.thread_export_user_not_admin)
.setPositiveButton(commonR.string.ok, null)
.show()
presenter.finishMatterThreadFlow()
}

else -> {} // Do nothing
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@ class WebViewPresenterImpl @Inject constructor(
mutableMatterThreadStep.tryEmit(MatterThreadStep.ERROR_THREAD_LOCAL_NETWORK)
}

is ThreadManager.SyncResult.ServerUserNotAdmin -> {
mutableMatterThreadStep.tryEmit(MatterThreadStep.ERROR_THREAD_USER_NOT_ADMIN)
}

else -> {
mutableMatterThreadStep.tryEmit(MatterThreadStep.ERROR_THREAD_OTHER)
}
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1177,11 +1177,13 @@
<string name="thread_debug_result_none">No credentials to sync</string>
<string name="thread_debug_result_removed">Removed old network from Home Assistant on this device</string>
<string name="thread_debug_result_unsupported_server">The Home Assistant server does not support Thread</string>
<string name="thread_debug_result_user_not_admin">Managing Thread credentials requires an administrator account on the Home Assistant server</string>
<string name="thread_debug_result_updated">Updated network from Home Assistant on this device</string>
<string name="thread_debug_summary">Manually update device and server Thread credentials and verify results</string>
<string name="thread_export_success">Imported credential</string>
<string name="thread_export_none">You don\'t have any credentials to import.</string>
<string name="thread_export_not_connected">You are not connected to a local network. Connect to Wi-Fi or ethernet to import Thread credentials.</string>
<string name="thread_export_user_not_admin">Managing Thread credentials requires an administrator account on the Home Assistant server.</string>
<string name="thread_export_unavailable">Thread is currently unavailable</string>
<string name="tile_vibrate">Vibrate when selected</string>
<string name="tile_auth_required">Requires unlocked device</string>
Expand Down
Loading