Breaking Change: Allow attributes to send over their proper data format (#889)

* Allow attributes to send over their proper data format

* Review comments

* Lint
This commit is contained in:
Daniel Shokouhi 2020-09-07 10:38:31 -07:00 committed by GitHub
parent 567e60a14f
commit f9f1f6f0d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 10 deletions

View file

@ -71,7 +71,8 @@ class GeocodeSensorManager : SensorManager {
"Country" to it.countryName, "Country" to it.countryName,
"ISO Country Code" to it.countryCode, "ISO Country Code" to it.countryCode,
"Locality" to it.locality, "Locality" to it.locality,
"Location" to listOf(it.latitude, it.longitude), "Latitude" to it.latitude,
"Longitude" to it.longitude,
"Postal Code" to it.postalCode, "Postal Code" to it.postalCode,
"Sub Administrative Area" to it.subAdminArea, "Sub Administrative Area" to it.subAdminArea,
"Sub Locality" to it.subLocality, "Sub Locality" to it.subLocality,

View file

@ -308,7 +308,7 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
Log.d(TAG, "Not requesting accurate location, last accurate location was too recent") Log.d(TAG, "Not requesting accurate location, last accurate location was too recent")
return return
} }
sensorDao.add(Attribute(backgroundLocation.id, "lastAccurateLocationRequest", now.toString())) sensorDao.add(Attribute(backgroundLocation.id, "lastAccurateLocationRequest", now.toString(), "string"))
val maxRetries = 5 val maxRetries = 5
val request = createLocationRequest() val request = createLocationRequest()

View file

@ -30,7 +30,7 @@ import io.homeassistant.companion.android.database.widget.TemplateWidgetEntity
StaticWidgetEntity::class, StaticWidgetEntity::class,
TemplateWidgetEntity::class TemplateWidgetEntity::class
], ],
version = 7 version = 8
) )
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
abstract fun authenticationDao(): AuthenticationDao abstract fun authenticationDao(): AuthenticationDao
@ -62,7 +62,8 @@ abstract class AppDatabase : RoomDatabase() {
MIGRATION_3_4, MIGRATION_3_4,
MIGRATION_4_5, MIGRATION_4_5,
MIGRATION_5_6, MIGRATION_5_6,
MIGRATION_6_7 MIGRATION_6_7,
MIGRATION_7_8
) )
.build() .build()
} }
@ -156,5 +157,10 @@ abstract class AppDatabase : RoomDatabase() {
database.execSQL("CREATE TABLE IF NOT EXISTS `sensor_attributes` (`sensor_id` TEXT NOT NULL, `name` TEXT NOT NULL, `value` TEXT NOT NULL, PRIMARY KEY(`sensor_id`, `name`))") database.execSQL("CREATE TABLE IF NOT EXISTS `sensor_attributes` (`sensor_id` TEXT NOT NULL, `name` TEXT NOT NULL, `value` TEXT NOT NULL, PRIMARY KEY(`sensor_id`, `name`))")
} }
} }
private val MIGRATION_7_8 = object : Migration(7, 8) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE `sensor_attributes` ADD `value_type` TEXT NOT NULL DEFAULT 'string'")
}
}
} }
} }

View file

@ -10,5 +10,7 @@ data class Attribute(
@ColumnInfo(name = "name") @ColumnInfo(name = "name")
val name: String, val name: String,
@ColumnInfo(name = "value") @ColumnInfo(name = "value")
val value: String var value: String,
@ColumnInfo(name = "value_type")
var valueType: String
) )

View file

@ -14,7 +14,16 @@ data class SensorWithAttributes(
val attributes: List<Attribute> val attributes: List<Attribute>
) { ) {
fun toSensorRegistration(): SensorRegistration<Any> { fun toSensorRegistration(): SensorRegistration<Any> {
val attributes = attributes.map { it.name to it.value }.toMap() val attributes = attributes.map {
val attributeValue = when (it.valueType) {
"boolean" -> it.value.toBoolean()
"float" -> it.value.toFloat()
"int" -> it.value.toInt()
"string" -> it.value
else -> throw IllegalArgumentException("Attribute: ${it.name} is of unknown type: ${it.valueType}")
}
it.name to attributeValue
}.toMap()
val state = when (sensor.stateType) { val state = when (sensor.stateType) {
"" -> "" "" -> ""
"boolean" -> sensor.state.toBoolean() "boolean" -> sensor.state.toBoolean()

View file

@ -67,7 +67,6 @@ interface SensorManager {
) { ) {
val sensorDao = AppDatabase.getInstance(context).sensorDao() val sensorDao = AppDatabase.getInstance(context).sensorDao()
val sensor = sensorDao.get(basicSensor.id) ?: return val sensor = sensorDao.get(basicSensor.id) ?: return
sensor.id = basicSensor.id sensor.id = basicSensor.id
sensor.state = state.toString() sensor.state = state.toString()
sensor.stateType = when (state) { sensor.stateType = when (state) {
@ -84,10 +83,24 @@ interface SensorManager {
sensor.unitOfMeasurement = basicSensor.unitOfMeasurement sensor.unitOfMeasurement = basicSensor.unitOfMeasurement
sensorDao.update(sensor) sensorDao.update(sensor)
sensorDao.clearAttributes(basicSensor.id) sensorDao.clearAttributes(basicSensor.id)
attributes.entries.forEach { entry ->
sensorDao.add(Attribute(basicSensor.id, entry.key, entry.value.toString())) for (item in attributes) {
val valueType = when (item.value) {
is Boolean -> "boolean"
is Int -> "int"
is Number -> "float"
else -> "string" // Always default to String for attributes
}
sensorDao.add(
Attribute(
basicSensor.id,
item.key,
item.value.toString(),
valueType
)
)
} }
} }
} }