Add deadband setting for last reboot sensor (#951)

* Add offset setting for last reboot sensor

* Review comments
This commit is contained in:
Daniel Shokouhi 2020-09-19 11:20:48 -07:00 committed by GitHub
parent e874977320
commit 9682b5d341
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View file

@ -4,15 +4,21 @@ import android.content.Context
import android.os.SystemClock
import android.util.Log
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.sensor.Setting
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.GregorianCalendar
import java.util.TimeZone
import kotlin.math.absoluteValue
class LastRebootSensorManager : SensorManager {
companion object {
private const val TAG = "LastReboot"
private const val LOCAL_TIME = "Local Time"
private const val DEADBAND = "deadband"
private const val TIME_MILLISECONDS = "Time in Milliseconds"
private val lastRebootSensor = SensorManager.BasicSensor(
"last_reboot",
@ -49,8 +55,17 @@ class LastRebootSensorManager : SensorManager {
var local = ""
var utc = "unavailable"
val sensorDao = AppDatabase.getInstance(context).sensorDao()
val fullSensor = sensorDao.getFull(lastRebootSensor.id)
val sensorSetting = sensorDao.getSettings(lastRebootSensor.id)
val lastTimeMillis = fullSensor?.attributes?.firstOrNull { it.name == TIME_MILLISECONDS }?.value?.toLongOrNull() ?: 0L
val settingDeadband = sensorSetting?.firstOrNull { it.name == DEADBAND }?.value?.toIntOrNull() ?: 60000
sensorDao.add(Setting(lastRebootSensor.id, DEADBAND, settingDeadband.toString(), "number"))
try {
timeInMillis = System.currentTimeMillis() - SystemClock.elapsedRealtime()
val diffMillis = (timeInMillis - lastTimeMillis).absoluteValue
if (lastTimeMillis != 0L && settingDeadband > diffMillis)
return
val cal: Calendar = GregorianCalendar()
cal.timeInMillis = timeInMillis
local = cal.time.toString()
@ -71,8 +86,8 @@ class LastRebootSensorManager : SensorManager {
utc,
icon,
mapOf(
"Local Time" to local,
"Time in Milliseconds" to timeInMillis
LOCAL_TIME to local,
TIME_MILLISECONDS to timeInMillis
)
)
}

View file

@ -186,7 +186,7 @@ like to connect to:</string>
<string name="sensor_description_headphone">Whether or not headphones are plugged into the device</string>
<string name="sensor_description_interactive">Whether or not the device is in an interactive state</string>
<string name="sensor_description_internal_storage">Information about the total and available storage space internally</string>
<string name="sensor_description_last_reboot">The date and time of the devices last reboot</string>
<string name="sensor_description_last_reboot">The date and time of the devices last reboot. The setting below will allow you to adjust the deadband in milliseconds, if you still find the value to jump incorrectly. The default value is 60000 (1 minute).</string>
<string name="sensor_description_light_sensor">The current level of illuminance</string>
<string name="sensor_description_location_background">Update your location behind the scenes, periodically.</string>
<string name="sensor_description_location_zone">Import existing Home Assistant zones as geofences for zone based tracking.</string>