Skip to content

Commit

Permalink
Set Loading State for Entities
Browse files Browse the repository at this point in the history
Fixing Ktlin LInt.

Stash CHan

Squashed commit of the following:

commit 496cd10
Merge: 7dcbcb8 d81913b
Author: googlvalenzuela <[email protected]>
Date:   Mon May 15 11:28:22 2023 -0700

    Merge branch 'master' into HA_Entity_Loading

commit 7dcbcb8
Author: Diego Valenzuela <[email protected]>
Date:   Mon May 15 11:27:47 2023 -0700

    Adding arming

commit 5da9518
Author: Diego Valenzuela <[email protected]>
Date:   Sun May 14 20:09:55 2023 -0700

    Adding buffering and disarming.

commit 73164e0
Author: Diego Valenzuela <[email protected]>
Date:   Sun May 14 20:07:24 2023 -0700

    Entity to executing take 2

commit d81913b
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 05:00:56 2023 +0200

    Add throttling/debouncing on Wear OS for registry updates (home-assistant#3517)

commit 7d6f11a
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:51:47 2023 +0200

    Add support for Assist pipeline, update Wear implementation (home-assistant#3526)

    * Group incoming messages by subscription to prevent out-of-order delivery

     - Messages received on the websocket are processed asynchronously, which is usually fine but can cause issues if messages need to be received in a specific order for a subscription. To fix this, process messages in order for the same subscription.

    * Implement Assist pipeline API

     - Add basic support for the Assist pipeline API
     - Update conversation function to use the Assist pipeline when on the minimum required version
     - Update UI to refer to Assist pipeline requirement

commit 57024e1
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:45:26 2023 +0200

    Restore reading service data for configured button widgets (home-assistant#3514)

commit 7e1dc20
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:43:38 2023 +0200

    Fix expanded zone id for high accuracy zone constraint (home-assistant#3509)

    - When checking if an expanded zone geofence should be created include the server ID in the comparison as that is what is used everywhere else. Otherwise expanded zones are never added, and as a result high accuracy mode won't work as expected.

commit 97634e3
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:43:01 2023 +0200

    Fix dialog options for high accuracy zone constraint entity id (home-assistant#3508)

    Fix dialog for high accuracy zone constraint entity id

     - Zone entity ids can contain underscores, only split on underscores once to keep those that belong to the entity id (string is serverid_entityid). The data was being stored correctly.

commit 8704213
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:39:56 2023 +0200

    Fix app lock 'retry' unlocking app (home-assistant#3507)

    - Only change the time for when the session expires if the app is in use, otherwise the new session expires timestamp might unlock the app

commit 975eaf1
Author: Diego Valenzuela <[email protected]>
Date:   Sat Apr 29 11:48:54 2023 -0700

    Fixing Ktlin LInt.

commit 6b33cd7
Author: Diego Valenzuela <[email protected]>
Date:   Sat Apr 29 11:06:32 2023 -0700

    Set Loading State for Entities

Set Loading State for Entities

Fixing Ktlin LInt.

Stash CHan

Squashed commit of the following:

commit 496cd10
Merge: 7dcbcb8 d81913b
Author: googlvalenzuela <[email protected]>
Date:   Mon May 15 11:28:22 2023 -0700

    Merge branch 'master' into HA_Entity_Loading

commit 7dcbcb8
Author: Diego Valenzuela <[email protected]>
Date:   Mon May 15 11:27:47 2023 -0700

    Adding arming

commit 5da9518
Author: Diego Valenzuela <[email protected]>
Date:   Sun May 14 20:09:55 2023 -0700

    Adding buffering and disarming.

commit 73164e0
Author: Diego Valenzuela <[email protected]>
Date:   Sun May 14 20:07:24 2023 -0700

    Entity to executing take 2

commit d81913b
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 05:00:56 2023 +0200

    Add throttling/debouncing on Wear OS for registry updates (home-assistant#3517)

commit 7d6f11a
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:51:47 2023 +0200

    Add support for Assist pipeline, update Wear implementation (home-assistant#3526)

    * Group incoming messages by subscription to prevent out-of-order delivery

     - Messages received on the websocket are processed asynchronously, which is usually fine but can cause issues if messages need to be received in a specific order for a subscription. To fix this, process messages in order for the same subscription.

    * Implement Assist pipeline API

     - Add basic support for the Assist pipeline API
     - Update conversation function to use the Assist pipeline when on the minimum required version
     - Update UI to refer to Assist pipeline requirement

commit 57024e1
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:45:26 2023 +0200

    Restore reading service data for configured button widgets (home-assistant#3514)

commit 7e1dc20
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:43:38 2023 +0200

    Fix expanded zone id for high accuracy zone constraint (home-assistant#3509)

    - When checking if an expanded zone geofence should be created include the server ID in the comparison as that is what is used everywhere else. Otherwise expanded zones are never added, and as a result high accuracy mode won't work as expected.

commit 97634e3
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:43:01 2023 +0200

    Fix dialog options for high accuracy zone constraint entity id (home-assistant#3508)

    Fix dialog for high accuracy zone constraint entity id

     - Zone entity ids can contain underscores, only split on underscores once to keep those that belong to the entity id (string is serverid_entityid). The data was being stored correctly.

commit 8704213
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:39:56 2023 +0200

    Fix app lock 'retry' unlocking app (home-assistant#3507)

    - Only change the time for when the session expires if the app is in use, otherwise the new session expires timestamp might unlock the app

commit 975eaf1
Author: Diego Valenzuela <[email protected]>
Date:   Sat Apr 29 11:48:54 2023 -0700

    Fixing Ktlin LInt.

commit 6b33cd7
Author: Diego Valenzuela <[email protected]>
Date:   Sat Apr 29 11:06:32 2023 -0700

    Set Loading State for Entities

Set Loading State for Entities

Fixing Ktlin LInt.

Stash CHan

Revert "Adding Missing Elements for the Automotive XML"

This reverts commit 04148ee.

Squashed commit of the following:

commit 496cd10
Merge: 7dcbcb8 d81913b
Author: googlvalenzuela <[email protected]>
Date:   Mon May 15 11:28:22 2023 -0700

    Merge branch 'master' into HA_Entity_Loading

commit 7dcbcb8
Author: Diego Valenzuela <[email protected]>
Date:   Mon May 15 11:27:47 2023 -0700

    Adding arming

commit 5da9518
Author: Diego Valenzuela <[email protected]>
Date:   Sun May 14 20:09:55 2023 -0700

    Adding buffering and disarming.

commit 73164e0
Author: Diego Valenzuela <[email protected]>
Date:   Sun May 14 20:07:24 2023 -0700

    Entity to executing take 2

commit d81913b
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 05:00:56 2023 +0200

    Add throttling/debouncing on Wear OS for registry updates (home-assistant#3517)

commit 7d6f11a
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:51:47 2023 +0200

    Add support for Assist pipeline, update Wear implementation (home-assistant#3526)

    * Group incoming messages by subscription to prevent out-of-order delivery

     - Messages received on the websocket are processed asynchronously, which is usually fine but can cause issues if messages need to be received in a specific order for a subscription. To fix this, process messages in order for the same subscription.

    * Implement Assist pipeline API

     - Add basic support for the Assist pipeline API
     - Update conversation function to use the Assist pipeline when on the minimum required version
     - Update UI to refer to Assist pipeline requirement

commit 57024e1
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:45:26 2023 +0200

    Restore reading service data for configured button widgets (home-assistant#3514)

commit 7e1dc20
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:43:38 2023 +0200

    Fix expanded zone id for high accuracy zone constraint (home-assistant#3509)

    - When checking if an expanded zone geofence should be created include the server ID in the comparison as that is what is used everywhere else. Otherwise expanded zones are never added, and as a result high accuracy mode won't work as expected.

commit 97634e3
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:43:01 2023 +0200

    Fix dialog options for high accuracy zone constraint entity id (home-assistant#3508)

    Fix dialog for high accuracy zone constraint entity id

     - Zone entity ids can contain underscores, only split on underscores once to keep those that belong to the entity id (string is serverid_entityid). The data was being stored correctly.

commit 8704213
Author: Joris Pelgröm <[email protected]>
Date:   Sat May 13 04:39:56 2023 +0200

    Fix app lock 'retry' unlocking app (home-assistant#3507)

    - Only change the time for when the session expires if the app is in use, otherwise the new session expires timestamp might unlock the app

commit 975eaf1
Author: Diego Valenzuela <[email protected]>
Date:   Sat Apr 29 11:48:54 2023 -0700

    Fixing Ktlin LInt.

commit 6b33cd7
Author: Diego Valenzuela <[email protected]>
Date:   Sat Apr 29 11:06:32 2023 -0700

    Set Loading State for Entities
  • Loading branch information
googlvalenzuela committed May 24, 2023
1 parent a2869ff commit ddc5348
Show file tree
Hide file tree
Showing 6 changed files with 421 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.util.Log
import androidx.annotation.RequiresApi
import androidx.car.app.CarAppService
import androidx.car.app.Screen
import androidx.car.app.ScreenManager
import androidx.car.app.Session
import androidx.car.app.SessionInfo
import androidx.car.app.validation.HostValidator
Expand All @@ -15,6 +16,7 @@ import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.servers.ServerManager
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -23,82 +25,108 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.launch
import okhttp3.internal.toImmutableMap
import javax.inject.Inject

@RequiresApi(Build.VERSION_CODES.O)
@AndroidEntryPoint
class HaCarAppService : CarAppService() {

companion object {
private const val TAG = "HaCarAppService"
}
companion object {
private const val TAG = "HaCarAppService"
}

@Inject
lateinit var serverManager: ServerManager
@Inject
lateinit var serverManager: ServerManager

private val serverId = MutableStateFlow(0)
private val allEntities = MutableStateFlow<Map<String, Entity<*>>>(emptyMap())
private var allEntitiesJob: Job? = null
private val serverId = MutableStateFlow(0)
private val allEntities = MutableStateFlow<Map<String, Entity<*>>>(emptyMap())
private var allEntitiesJob: Job? = null

override fun createHostValidator(): HostValidator {
return if (applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0) {
HostValidator.ALLOW_ALL_HOSTS_VALIDATOR
} else {
HostValidator.Builder(applicationContext)
.addAllowedHosts(R.array.hosts_allowlist)
.build()
}
override fun createHostValidator(): HostValidator {
return if (applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0) {
HostValidator.ALLOW_ALL_HOSTS_VALIDATOR
} else {
HostValidator.Builder(applicationContext)
.addAllowedHosts(R.array.hosts_allowlist)
.build()
}
}

override fun onCreateSession(sessionInfo: SessionInfo): Session {
return object : Session() {
init {
serverManager.getServer()?.let {
loadEntities(lifecycleScope, it.id)
}
}
override fun onCreateSession(sessionInfo: SessionInfo): Session {
return object : Session() {
init {
serverManager.getServer()?.let {
loadEntities(lifecycleScope, it.id)
}
}

val serverIdFlow = serverId.asStateFlow()
val entityFlow = allEntities.shareIn(
lifecycleScope,
SharingStarted.WhileSubscribed(10_000),
1
)
val serverIdFlow = serverId.asStateFlow()
val entityFlow = allEntities.shareIn(
lifecycleScope,
SharingStarted.WhileSubscribed(10_000),
1
)

override fun onCreateScreen(intent: Intent): Screen {
return MainVehicleScreen(
carContext,
serverManager,
serverIdFlow,
entityFlow
) { loadEntities(lifecycleScope, it) }
override fun onCreateScreen(intent: Intent): Screen {
if (intent.getBooleanExtra("TRANSITION_LAUNCH", false)) {
carContext
.getCarService(ScreenManager::class.java).run {
push(MainVehicleScreen(
carContext,
serverManager,
serverIdFlow,
entityFlow
) { loadEntities(lifecycleScope, it) })

push(
LoginScreen(
carContext,
serverManager,
)
)
}
return SwitchToDrivingOptimizedScreen(carContext)
} else {
carContext
.getCarService(ScreenManager::class.java).run {
push(MainVehicleScreen(
carContext,
serverManager,
serverIdFlow,
entityFlow
) { loadEntities(lifecycleScope, it) })
}
return LoginScreen(
carContext,
serverManager,
)
}
}
}
}

private fun loadEntities(scope: CoroutineScope, id: Int) {
allEntitiesJob?.cancel()
allEntitiesJob = scope.launch {
allEntities.emit(emptyMap())
serverId.value = id
val entities: MutableMap<String, Entity<*>>? =
if (serverManager.getServer(id) != null) {
serverManager.integrationRepository(id).getEntities()
?.associate { it.entityId to it }
?.toMutableMap()
} else {
null
}
if (entities != null) {
allEntities.emit(entities.toImmutableMap())
serverManager.integrationRepository(id).getEntityUpdates()?.collect { entity ->
entities[entity.entityId] = entity
allEntities.emit(entities.toImmutableMap())
}
} else {
Log.w(TAG, "No entities found?")
allEntities.emit(emptyMap())
}
private fun loadEntities(scope: CoroutineScope, id: Int) {
allEntitiesJob?.cancel()
allEntitiesJob = scope.launch {
allEntities.emit(emptyMap())
serverId.value = id
val entities: MutableMap<String, Entity<*>>? =
if (serverManager.getServer(id) != null) {
serverManager.integrationRepository(id).getEntities()
?.associate { it.entityId to it }
?.toMutableMap()
} else {
null
}
if (entities != null) {
allEntities.emit(entities.toImmutableMap())
serverManager.integrationRepository(id).getEntityUpdates()?.collect { entity ->
entities[entity.entityId] = entity
allEntities.emit(entities.toImmutableMap())
}
} else {
Log.w(TAG, "No entities found?")
allEntities.emit(emptyMap())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.homeassistant.companion.android.vehicle

import android.content.Intent
import android.content.pm.PackageManager
import android.util.Log
import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.model.Action
import androidx.car.app.model.MessageTemplate
import androidx.car.app.model.ParkedOnlyOnClickListener
import androidx.car.app.model.Template
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import io.homeassistant.companion.android.common.R
import io.homeassistant.companion.android.common.data.authentication.SessionState
import io.homeassistant.companion.android.common.data.integration.domain
import io.homeassistant.companion.android.common.data.servers.ServerManager
import io.homeassistant.companion.android.launch.LaunchActivity
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class LoginScreen(context: CarContext, val serverManager: ServerManager) : Screen(context) {
private var isLoggedIn: Boolean? = null

init {
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
isLoggedIn = serverManager.isRegistered() &&
serverManager.authenticationRepository()
.getSessionState() == SessionState.CONNECTED
invalidate()
while (isLoggedIn != true) {
delay(1000)
isLoggedIn = serverManager.isRegistered() &&
serverManager.authenticationRepository()
.getSessionState() == SessionState.CONNECTED

}
if (isLoggedIn == true) {
screenManager.pop()
}
}
}
}

override fun onGetTemplate(): Template {

return MessageTemplate.Builder(carContext.getString(R.string.aa_app_not_logged_in))
.setTitle(carContext.getString(R.string.app_name))
.setHeaderAction(Action.APP_ICON)
.addAction(
Action.Builder()
.setTitle(carContext.getString(R.string.login))
.setOnClickListener(
ParkedOnlyOnClickListener.create {
startNativeActivity()
}
)
.build()
)
.build()
}

private val isAutomotive get() = carContext.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)

private fun startNativeActivity() {
with(carContext) {
startActivity(
Intent(
carContext,
LaunchActivity::class.java
).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
)
if (isAutomotive) {
finishCarApp()
}
}
}

}
Loading

0 comments on commit ddc5348

Please sign in to comment.