Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow binary sensors and sensors to be added to driving favorites #3732

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -12,10 +12,9 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.domain
import io.homeassistant.companion.android.common.data.prefs.PrefsRepository
import io.homeassistant.companion.android.common.data.servers.ServerManager
import io.homeassistant.companion.android.vehicle.MainVehicleScreen
import io.homeassistant.companion.android.util.vehicle.isVehicleDomain
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -46,12 +45,7 @@ class ManageAndroidAutoViewModel @Inject constructor(
entities[it.id] = try {
serverManager.integrationRepository(it.id).getEntities().orEmpty()
.filter {
it.domain in MainVehicleScreen.SUPPORTED_DOMAINS ||
(
it.domain in MainVehicleScreen.MAP_DOMAINS &&
((it.attributes as? Map<*, *>)?.get("latitude") as? Double != null) &&
((it.attributes as? Map<*, *>)?.get("longitude") as? Double != null)
)
isVehicleDomain(it)
}
} catch (e: Exception) {
Log.e(TAG, "Couldn't load entities for server", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.domain
import io.homeassistant.companion.android.common.data.integration.friendlyName
import io.homeassistant.companion.android.database.server.Server
import io.homeassistant.companion.android.settings.vehicle.ManageAndroidAutoViewModel
import io.homeassistant.companion.android.util.compose.FavoriteEntityRow
import io.homeassistant.companion.android.util.compose.ServerExposedDropdownMenu
import io.homeassistant.companion.android.util.compose.SingleEntityPicker
import io.homeassistant.companion.android.vehicle.MainVehicleScreen
import io.homeassistant.companion.android.util.vehicle.isVehicleDomain
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.burnoutcrew.reorderable.ReorderableItem
Expand Down Expand Up @@ -55,14 +54,7 @@ fun AndroidAutoFavoritesSettings(
androidAutoViewModel.sortedEntities
.filter {
!favoriteEntities.contains("$selectedServer-${it.entityId}") &&
(
it.domain in MainVehicleScreen.SUPPORTED_DOMAINS ||
(
it.domain in MainVehicleScreen.MAP_DOMAINS &&
((it.attributes as? Map<*, *>)?.get("latitude") as? Double != null) &&
((it.attributes as? Map<*, *>)?.get("longitude") as? Double != null)
)
)
isVehicleDomain(it)
}
.toList()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.homeassistant.companion.android.util.vehicle

import android.os.Build
import androidx.annotation.RequiresApi
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.domain
import io.homeassistant.companion.android.vehicle.MainVehicleScreen

@RequiresApi(Build.VERSION_CODES.O)
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
fun isVehicleDomain(entity: Entity<*>): Boolean {
return entity.domain in MainVehicleScreen.SUPPORTED_DOMAINS ||
entity.domain in MainVehicleScreen.NOT_ACTIONABLE_DOMAINS ||
(
entity.domain in MainVehicleScreen.MAP_DOMAINS &&
((entity.attributes as? Map<*, *>)?.get("latitude") as? Double != null) &&
((entity.attributes as? Map<*, *>)?.get("longitude") as? Double != null)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +160,40 @@ class EntityGridVehicleScreen(
if (entity.isExecuting()) {
gridItem.setLoading(entity.isExecuting())
} else {
gridItem
.setOnClickListener {
Log.i(TAG, "${entity.entityId} clicked")
if (entity.domain in MainVehicleScreen.MAP_DOMAINS) {
val attrs = entity.attributes as? Map<*, *>
if (attrs != null) {
val lat = attrs["latitude"] as? Double
val lon = attrs["longitude"] as? Double
if (lat != null && lon != null) {
val intent = Intent(
CarContext.ACTION_NAVIGATE,
Uri.parse("geo:$lat,$lon")
)
carContext.startCarApp(intent)
if (entity.domain !in MainVehicleScreen.NOT_ACTIONABLE_DOMAINS) {
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
gridItem
.setOnClickListener {
Log.i(TAG, "${entity.entityId} clicked")
when (entity.domain) {
in MainVehicleScreen.MAP_DOMAINS -> {
val attrs = entity.attributes as? Map<*, *>
if (attrs != null) {
val lat = attrs["latitude"] as? Double
val lon = attrs["longitude"] as? Double
if (lat != null && lon != null) {
val intent = Intent(
CarContext.ACTION_NAVIGATE,
Uri.parse("geo:$lat,$lon")
)
carContext.startCarApp(intent)
}
}
}

in MainVehicleScreen.SUPPORTED_DOMAINS -> {
lifecycleScope.launch {
entity.onPressed(integrationRepository)
}
}

else -> {
// No op
jpelgrom marked this conversation as resolved.
Show resolved Hide resolved
}
}
} else {
lifecycleScope.launch {
entity.onPressed(integrationRepository)
}
}
}
}

gridItem
.setImage(
CarIcon.Builder(
IconicsDrawable(carContext, icon).apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class MainVehicleScreen(
"sensor",
"zone"
)

val NOT_ACTIONABLE_DOMAINS = listOf(
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
"binary_sensor",
"sensor"
)
}

private var favoriteEntities = flowOf<List<Entity<*>>>()
Expand Down