Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unavailable and unknown states consistently when dealing with sensors and entities #3821

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.google.android.gms.location.SleepSegmentRequest
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.sensors.SensorManager
import io.homeassistant.companion.android.common.sensors.SensorReceiverBase
import io.homeassistant.companion.android.common.util.STATE_UNKNOWN
import java.util.concurrent.TimeUnit
import io.homeassistant.companion.android.common.R as commonR

Expand Down Expand Up @@ -168,8 +169,8 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
DetectedActivity.STILL -> "still"
DetectedActivity.TILTING -> "tilting"
DetectedActivity.WALKING -> "walking"
DetectedActivity.UNKNOWN -> "unknown"
else -> "unknown"
DetectedActivity.UNKNOWN -> STATE_UNKNOWN
else -> STATE_UNKNOWN
}
}

Expand All @@ -184,7 +185,7 @@ class ActivitySensorManager : BroadcastReceiver(), SensorManager {
SleepSegmentEvent.STATUS_SUCCESSFUL -> "successful"
SleepSegmentEvent.STATUS_MISSING_DATA -> "missing data"
SleepSegmentEvent.STATUS_NOT_DETECTED -> "not detected"
else -> "unknown"
else -> STATE_UNKNOWN
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.os.Build.VERSION.SDK_INT
import android.util.Log
import com.google.android.gms.location.LocationServices
import io.homeassistant.companion.android.common.sensors.SensorManager
import io.homeassistant.companion.android.common.util.STATE_UNKNOWN
import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.sensor.SensorSetting
import io.homeassistant.companion.android.database.sensor.SensorSettingType
Expand Down Expand Up @@ -132,7 +133,7 @@ class GeocodeSensorManager : SensorManager {
onSensorUpdated(
context,
geocodedLocation,
if (!prettyAddress.isNullOrEmpty()) prettyAddress else "Unknown",
if (!prettyAddress.isNullOrEmpty()) prettyAddress else STATE_UNKNOWN,
geocodedLocation.statelessIcon,
attributes
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
import io.homeassistant.companion.android.common.data.websocket.impl.entities.AreaRegistryResponse
import io.homeassistant.companion.android.common.util.STATE_UNAVAILABLE
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -47,7 +48,7 @@ object CameraControl : HaControl {
control.setControlTemplate(
ThumbnailTemplate(
entity.entityId,
entity.state != "unavailable" && image != null,
entity.state != STATE_UNAVAILABLE && image != null,
icon,
context.getString(commonR.string.widget_camera_contentdescription)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import androidx.lifecycle.DefaultLifecycleObserver
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.common.R
import io.homeassistant.companion.android.common.sensors.SensorManager
import io.homeassistant.companion.android.common.util.STATE_UNAVAILABLE
import io.homeassistant.companion.android.common.util.STATE_UNKNOWN
import io.homeassistant.companion.android.vehicle.HaCarAppService

@RequiresApi(Build.VERSION_CODES.O)
Expand Down Expand Up @@ -173,7 +175,7 @@ class CarSensorManager :
onSensorUpdated(
context,
it,
"unavailable",
STATE_UNAVAILABLE,
it.statelessIcon,
mapOf()
)
Expand Down Expand Up @@ -257,7 +259,7 @@ class CarSensorManager :
onSensorUpdated(
context,
fuelLevel,
if (fuelStatus == "success") data.fuelPercent.value!! else "unknown",
if (fuelStatus == "success") data.fuelPercent.value!! else STATE_UNKNOWN,
fuelLevel.statelessIcon,
mapOf(
"status" to fuelStatus
Expand All @@ -270,7 +272,7 @@ class CarSensorManager :
onSensorUpdated(
context,
batteryLevel,
if (batteryStatus == "success") data.batteryPercent.value!! else "unknown",
if (batteryStatus == "success") data.batteryPercent.value!! else STATE_UNKNOWN,
batteryLevel.statelessIcon,
mapOf(
"status" to batteryStatus
Expand All @@ -288,7 +290,7 @@ class CarSensorManager :
onSensorUpdated(
context,
carName,
if (status == "success") data.name.value!! else "unknown",
if (status == "success") data.name.value!! else STATE_UNKNOWN,
carName.statelessIcon,
mapOf(
"car_manufacturer" to data.manufacturer.value,
Expand All @@ -309,7 +311,7 @@ class CarSensorManager :
onSensorUpdated(
context,
carStatus,
if (status == "success") (data.evChargePortConnected.value == true) else "unknown",
if (status == "success") (data.evChargePortConnected.value == true) else STATE_UNKNOWN,
carStatus.statelessIcon,
mapOf(
"car_charge_port_open" to (data.evChargePortOpen.value == true),
Expand All @@ -329,7 +331,7 @@ class CarSensorManager :
onSensorUpdated(
context,
odometerValue,
if (status == "success") data.odometerMeters.value!! else "unknown",
if (status == "success") data.odometerMeters.value!! else STATE_UNKNOWN,
odometerValue.statelessIcon,
mapOf(
"status" to status
Expand All @@ -348,7 +350,7 @@ class CarSensorManager :
onSensorUpdated(
context,
fuelType,
if (fuelTypeStatus == "success") getFuelType(data.fuelTypes.value!!) else "unknown",
if (fuelTypeStatus == "success") getFuelType(data.fuelTypes.value!!) else STATE_UNKNOWN,
fuelType.statelessIcon,
mapOf(
"status" to fuelTypeStatus
Expand All @@ -360,7 +362,7 @@ class CarSensorManager :
onSensorUpdated(
context,
evConnector,
if (evConnectorTypeStatus == "success") getEvConnectorType(data.evConnectorTypes.value!!) else "unknown",
if (evConnectorTypeStatus == "success") getEvConnectorType(data.evConnectorTypes.value!!) else STATE_UNKNOWN,
evConnector.statelessIcon,
mapOf(
"status" to evConnectorTypeStatus
Expand All @@ -373,8 +375,8 @@ class CarSensorManager :
private fun carValueStatus(value: Int): String? {
return when (value) {
CarValue.STATUS_SUCCESS -> "success"
CarValue.STATUS_UNAVAILABLE -> "unavailable"
CarValue.STATUS_UNKNOWN -> "unknown"
CarValue.STATUS_UNAVAILABLE -> STATE_UNAVAILABLE
CarValue.STATUS_UNKNOWN -> STATE_UNKNOWN
CarValue.STATUS_UNIMPLEMENTED -> "unimplemented"
else -> null
}
Expand All @@ -395,9 +397,9 @@ class CarSensorManager :
EnergyProfile.FUEL_TYPE_LNG -> "Liquified natural gas"
EnergyProfile.FUEL_TYPE_LPG -> "Liquified petroleum gas"
EnergyProfile.FUEL_TYPE_OTHER -> "Other"
EnergyProfile.FUEL_TYPE_UNKNOWN -> "unknown"
EnergyProfile.FUEL_TYPE_UNKNOWN -> STATE_UNKNOWN
EnergyProfile.FUEL_TYPE_UNLEADED -> "Unleaded gasoline"
else -> "unknown"
else -> STATE_UNKNOWN
}
}
return fuelTypeList.toString()
Expand All @@ -419,8 +421,8 @@ class CarSensorManager :
EnergyProfile.EVCONNECTOR_TYPE_TESLA_HPWC -> "High Power Wall Charger of Tesla"
EnergyProfile.EVCONNECTOR_TYPE_TESLA_ROADSTER -> "Connector of Tesla Roadster"
EnergyProfile.EVCONNECTOR_TYPE_TESLA_SUPERCHARGER -> "Supercharger of Tesla"
EnergyProfile.EVCONNECTOR_TYPE_UNKNOWN -> "unknown"
else -> "unknown"
EnergyProfile.EVCONNECTOR_TYPE_UNKNOWN -> STATE_UNKNOWN
else -> STATE_UNKNOWN
}
}
return evConnectorList.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.common.sensors.SensorManager
import io.homeassistant.companion.android.common.util.STATE_UNKNOWN
import io.homeassistant.companion.android.common.R as commonR

class LastAppSensorManager : SensorManager {
Expand Down Expand Up @@ -64,7 +65,7 @@ class LastAppSensorManager : SensorManager {
val current = System.currentTimeMillis()
val lastApp = usageStats.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, current - 1000 * 1000, current).maxByOrNull { it.lastTimeUsed }?.packageName ?: "none"

var appLabel = "unknown"
var appLabel = STATE_UNKNOWN

try {
val pm = context.packageManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import android.util.Log
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.common.sensors.SensorManager
import io.homeassistant.companion.android.common.util.STATE_UNAVAILABLE
import io.homeassistant.companion.android.common.util.STATE_UNKNOWN
import io.homeassistant.companion.android.database.sensor.SensorSettingType
import io.homeassistant.companion.android.common.R as commonR

Expand Down Expand Up @@ -266,7 +268,7 @@ class NotificationSensorManager : NotificationListenerService(), SensorManager {
val mediaSessionManager = context.getSystemService<MediaSessionManager>()!!
val mediaList = mediaSessionManager.getActiveSessions(ComponentName(context, NotificationSensorManager::class.java))
val sessionCount = mediaList.size
val primaryPlaybackState = if (sessionCount > 0) getPlaybackState(mediaList[0].playbackState?.state) else "Unavailable"
val primaryPlaybackState = if (sessionCount > 0) getPlaybackState(mediaList[0].playbackState?.state) else STATE_UNAVAILABLE
val attr: MutableMap<String, Any?> = mutableMapOf()
if (mediaList.size > 0) {
for (item in mediaList) {
Expand Down Expand Up @@ -306,7 +308,7 @@ class NotificationSensorManager : NotificationListenerService(), SensorManager {
PlaybackState.STATE_SKIPPING_TO_NEXT -> "Skip to Next"
PlaybackState.STATE_SKIPPING_TO_PREVIOUS -> "Skip to Previous"
PlaybackState.STATE_SKIPPING_TO_QUEUE_ITEM -> "Skip to Queue Item"
else -> "Unknown"
else -> STATE_UNKNOWN
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.homeassistant.companion.android.common.sensors

import android.content.Context
import android.os.Build
import io.homeassistant.companion.android.common.util.STATE_UNKNOWN
import io.homeassistant.companion.android.common.R as commonR

class AndroidOsSensorManager : SensorManager {
Expand Down Expand Up @@ -67,10 +68,10 @@ class AndroidOsSensorManager : SensorManager {
osSecurityPatch.id -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Build.VERSION.SECURITY_PATCH
} else {
"unknown"
STATE_UNKNOWN
}
else -> {
"unknown"
STATE_UNKNOWN
}
},
sensor.statelessIcon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.media.AudioManager
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.common.util.STATE_UNKNOWN
import io.homeassistant.companion.android.common.R as commonR

class AudioSensorManager : SensorManager {
Expand Down Expand Up @@ -187,7 +188,7 @@ class AudioSensorManager : SensorManager {
AudioManager.RINGER_MODE_NORMAL -> "normal"
AudioManager.RINGER_MODE_SILENT -> "silent"
AudioManager.RINGER_MODE_VIBRATE -> "vibrate"
else -> "unknown"
else -> STATE_UNKNOWN
}

val icon = when (audioManager.ringerMode) {
Expand Down Expand Up @@ -216,7 +217,7 @@ class AudioSensorManager : SensorManager {
AudioManager.MODE_IN_CALL -> "in_call"
AudioManager.MODE_IN_COMMUNICATION -> "in_communication"
AudioManager.MODE_CALL_SCREENING -> "call_screening"
else -> "unknown"
else -> STATE_UNKNOWN
}

val icon = when (audioManager.mode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import io.homeassistant.companion.android.common.util.STATE_UNKNOWN
import java.math.RoundingMode
import io.homeassistant.companion.android.common.R as commonR

Expand Down Expand Up @@ -314,7 +315,7 @@ class BatterySensorManager : SensorManager {
BatteryManager.BATTERY_STATUS_CHARGING -> "charging"
BatteryManager.BATTERY_STATUS_DISCHARGING -> "discharging"
BatteryManager.BATTERY_STATUS_NOT_CHARGING -> "not_charging"
else -> "unknown"
else -> STATE_UNKNOWN
}
}

Expand All @@ -326,7 +327,7 @@ class BatterySensorManager : SensorManager {
BatteryManager.BATTERY_HEALTH_OVERHEAT -> "overheated"
BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE -> "over_voltage"
BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE -> "failed"
else -> "unknown"
else -> STATE_UNKNOWN
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.homeassistant.companion.android.common.bluetooth.ble.KalmanFilter
import io.homeassistant.companion.android.common.bluetooth.ble.MonitoringManager
import io.homeassistant.companion.android.common.bluetooth.ble.TransmitterManager
import io.homeassistant.companion.android.common.bluetooth.ble.name
import io.homeassistant.companion.android.common.util.STATE_UNKNOWN
import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.sensor.SensorSetting
import io.homeassistant.companion.android.database.sensor.SensorSettingType
Expand Down Expand Up @@ -353,7 +354,7 @@ class BluetoothSensorManager : SensorManager {
TransmitterManager.stopTransmitting(bleTransmitterDevice)
}

val lastState = AppDatabase.getInstance(context).sensorDao().get(bleTransmitter.id).firstOrNull()?.state ?: "unknown"
val lastState = AppDatabase.getInstance(context).sensorDao().get(bleTransmitter.id).firstOrNull()?.state ?: STATE_UNKNOWN
val state = if (isBtOn(context)) bleTransmitterDevice.state else "Bluetooth is turned off"
val icon = if (bleTransmitterDevice.transmitting) "mdi:bluetooth" else "mdi:bluetooth-off"
onSensorUpdated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Build
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService
import io.homeassistant.companion.android.common.util.STATE_UNKNOWN
import io.homeassistant.companion.android.common.R as commonR

@RequiresApi(Build.VERSION_CODES.M)
Expand Down Expand Up @@ -67,8 +68,8 @@ class DNDSensorManager : SensorManager {
NotificationManager.INTERRUPTION_FILTER_ALL -> "off"
NotificationManager.INTERRUPTION_FILTER_NONE -> "total_silence"
NotificationManager.INTERRUPTION_FILTER_PRIORITY -> "priority_only"
NotificationManager.INTERRUPTION_FILTER_UNKNOWN -> "unknown"
else -> "unknown"
NotificationManager.INTERRUPTION_FILTER_UNKNOWN -> STATE_UNKNOWN
else -> STATE_UNKNOWN
}

onSensorUpdated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.os.SystemClock
import android.util.Log
import io.homeassistant.companion.android.common.util.STATE_UNAVAILABLE
import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.sensor.SensorSetting
import io.homeassistant.companion.android.database.sensor.SensorSettingType
Expand Down Expand Up @@ -62,7 +63,7 @@ class LastRebootSensorManager : SensorManager {

var timeInMillis = 0L
var local = ""
var utc = "unavailable"
var utc = STATE_UNAVAILABLE

val sensorDao = AppDatabase.getInstance(context).sensorDao()
val fullSensor = sensorDao.getFull(lastRebootSensor.id).toSensorWithAttributes()
Expand Down
Loading