From 1b5cbf1712ae7c3072cd41d30c5269da468ca03e Mon Sep 17 00:00:00 2001 From: Oleksandr Kapshuk Date: Fri, 4 Sep 2020 15:28:11 +0300 Subject: [PATCH] Update sensors bugfix (#879) * Update sensors fix * Include unregistering into the condition check --- .../android/sensors/SensorReceiver.kt | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt b/app/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt index 8a650ec63..e7d835841 100644 --- a/app/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt +++ b/app/src/main/java/io/homeassistant/companion/android/sensors/SensorReceiver.kt @@ -101,28 +101,30 @@ class SensorReceiver : BroadcastReceiver() { Log.e(TAG, "Issue registering sensor: ${reg.uniqueId}", e) } } - if (fullSensor != null && sensor?.registered == true) { + if (sensor?.enabled == true && fullSensor != null && sensor?.registered) { enabledRegistrations.add(fullSensor.toSensorRegistration()) } } } - var success = false - try { - success = integrationUseCase.updateSensors(enabledRegistrations.toTypedArray()) - } catch (e: Exception) { - Log.e(TAG, "Exception while updating sensors.", e) - } + if (enabledRegistrations.isNotEmpty()) { + var success = false + try { + success = integrationUseCase.updateSensors(enabledRegistrations.toTypedArray()) + } catch (e: Exception) { + Log.e(TAG, "Exception while updating sensors.", e) + } - // We failed to update a sensor, we should re register next time - if (!success) { - enabledRegistrations.forEach { - val sensor = sensorDao.get(it.uniqueId) - if (sensor != null) { - sensor.registered = false - sensorDao.update(sensor) + // We failed to update a sensor, we should re register next time + if (!success) { + enabledRegistrations.forEach { + val sensor = sensorDao.get(it.uniqueId) + if (sensor != null) { + sensor.registered = false + sensorDao.update(sensor) + } } } - } + } else Log.d(TAG, "Nothing to update") } }