From dab3640c6b80d273c92ebda708b658f1b9bb0674 Mon Sep 17 00:00:00 2001 From: Daniel Shokouhi Date: Mon, 5 Sep 2022 07:11:38 -0700 Subject: [PATCH] Fix sensorDao not initialized error after geocoded update (#2772) * Fix sensorDao not initialized error after geocoded update * Switch to sending an intent to update the sensor * Review comments --- .../android/sensors/LocationSensorManager.kt | 15 +++------ .../common/sensors/SensorReceiverBase.kt | 32 ++++++++++++------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt b/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt index c2704b5e5..a36ad35fb 100644 --- a/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt +++ b/app/src/full/java/io/homeassistant/companion/android/sensors/LocationSensorManager.kt @@ -29,6 +29,7 @@ import io.homeassistant.companion.android.common.data.integration.UpdateLocation import io.homeassistant.companion.android.common.data.integration.ZoneAttributes import io.homeassistant.companion.android.common.sensors.LocationSensorManagerBase import io.homeassistant.companion.android.common.sensors.SensorManager +import io.homeassistant.companion.android.common.sensors.SensorReceiverBase import io.homeassistant.companion.android.common.util.DisabledLocationHandler import io.homeassistant.companion.android.database.AppDatabase import io.homeassistant.companion.android.database.sensor.Attribute @@ -765,22 +766,16 @@ class LocationSensorManager : LocationSensorManagerBase() { lastLocationSend = now lastUpdateLocation = updateLocation.gps.contentToString() - val geoSensorManager = SensorReceiver.MANAGERS.firstOrNull { it.getAvailableSensors(latestContext).any { s -> s.name == commonR.string.basic_sensor_name_geolocation } } - val geoSensor = AppDatabase.getInstance(latestContext).sensorDao().getFull(GeocodeSensorManager.geocodedLocation.id) - ioScope.launch { try { integrationUseCase.updateLocation(updateLocation) Log.d(TAG, "Location update sent successfully") // Update Geocoded Location Sensor - SensorReceiver().updateSensor( - latestContext, - integrationUseCase, - geoSensor, - geoSensorManager, - GeocodeSensorManager.geocodedLocation - ) + val intent = Intent(latestContext, SensorReceiver::class.java) + intent.action = SensorReceiverBase.ACTION_UPDATE_SENSOR + intent.putExtra(SensorReceiverBase.EXTRA_SENSOR_ID, GeocodeSensorManager.geocodedLocation.id) + latestContext.sendBroadcast(intent) } catch (e: Exception) { Log.e(TAG, "Could not update location.", e) } diff --git a/common/src/main/java/io/homeassistant/companion/android/common/sensors/SensorReceiverBase.kt b/common/src/main/java/io/homeassistant/companion/android/common/sensors/SensorReceiverBase.kt index 679d4728a..8c4b99530 100644 --- a/common/src/main/java/io/homeassistant/companion/android/common/sensors/SensorReceiverBase.kt +++ b/common/src/main/java/io/homeassistant/companion/android/common/sensors/SensorReceiverBase.kt @@ -30,6 +30,9 @@ import javax.inject.Inject abstract class SensorReceiverBase : BroadcastReceiver() { companion object { + const val ACTION_UPDATE_SENSOR = "io.homeassistant.companion.android.UPDATE_SENSOR" + const val EXTRA_SENSOR_ID = "sensorId" + fun shouldDoFastUpdates(context: Context): Boolean { val settingDao = AppDatabase.getInstance(context).settingsDao().get(0) return when (settingDao?.sensorUpdateFrequency) { @@ -117,12 +120,19 @@ abstract class SensorReceiverBase : BroadcastReceiver() { Log.i(tag, "Skipping faster update because not charging/different preference") return@launch } - updateSensors(context, integrationUseCase, sensorDao, intent) - if (chargingActions.contains(intent.action)) { - // Add a 5 second delay to perform another update so charging state updates completely. - // This is necessary as the system needs a few seconds to verify the charger. - delay(5000L) + if (intent.action == ACTION_UPDATE_SENSOR) { + val sensorId = intent.getStringExtra(EXTRA_SENSOR_ID) + if (sensorId != null) { + updateSensor(context, sensorId) + } + } else { updateSensors(context, integrationUseCase, sensorDao, intent) + if (chargingActions.contains(intent.action)) { + // Add a 5 second delay to perform another update so charging state updates completely. + // This is necessary as the system needs a few seconds to verify the charger. + delay(5000L) + updateSensors(context, integrationUseCase, sensorDao, intent) + } } } } @@ -320,17 +330,17 @@ abstract class SensorReceiverBase : BroadcastReceiver() { integrationUseCase.registerSensor(reg) } - suspend fun updateSensor( + private suspend fun updateSensor( context: Context, - integrationUseCase: IntegrationRepository, - fullSensor: SensorWithAttributes?, - sensorManager: SensorManager?, - basicSensor: SensorManager.BasicSensor + sensorId: String ) { + val sensorManager = managers.firstOrNull { it.getAvailableSensors(context).any { s -> s.id == sensorId } } sensorManager?.requestSensorUpdate(context) + val basicSensor = sensorManager?.getAvailableSensors(context)?.firstOrNull { it.id == sensorId } + val fullSensor = sensorDao.getFull(sensorId) if ( fullSensor != null && fullSensor.sensor.enabled && - fullSensor.sensor.registered == true && + fullSensor.sensor.registered == true && basicSensor != null && ( fullSensor.sensor.state != fullSensor.sensor.lastSentState || fullSensor.sensor.icon != fullSensor.sensor.lastSentIcon