mirror of
https://github.com/home-assistant/android
synced 2024-10-15 12:32:54 +00:00
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:
parent
d0016e530e
commit
dab3640c6b
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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,6 +120,12 @@ abstract class SensorReceiverBase : BroadcastReceiver() {
|
|||
Log.i(tag, "Skipping faster update because not charging/different preference")
|
||||
return@launch
|
||||
}
|
||||
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.
|
||||
|
@ -126,6 +135,7 @@ abstract class SensorReceiverBase : BroadcastReceiver() {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun isSensorEnabled(id: String): Boolean {
|
||||
return sensorDao.get(id)?.enabled == true
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue