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
This commit is contained in:
Daniel Shokouhi 2022-09-05 07:11:38 -07:00 committed by GitHub
parent d0016e530e
commit dab3640c6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 21 deletions

View file

@ -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)
}

View file

@ -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