Skip to content

Commit

Permalink
Set car sensors state appropriately if sensor registers with unit of …
Browse files Browse the repository at this point in the history
…measurement (#3764)

* Set car sensor state appropriately if sensor registers with unit of measurement

* Stick to unavailable for better consistency
  • Loading branch information
dshokouhi committed Aug 7, 2023
1 parent 0425d7c commit 2d439f1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class CarSensorManager :
onSensorUpdated(
context,
it,
context.getString(R.string.car_data_unavailable),
"unavailable",
it.statelessIcon,
mapOf()
)
Expand Down Expand Up @@ -257,19 +257,25 @@ class CarSensorManager :
onSensorUpdated(
context,
fuelLevel,
fuelStatus ?: data.fuelPercent.value!!,
if (fuelStatus == "success") data.fuelPercent.value!! else "unknown",
fuelLevel.statelessIcon,
mapOf()
mapOf(
"status" to fuelStatus
),
forceUpdate = true
)
}
val batteryStatus = carValueStatus(data.batteryPercent.status)
if (isEnabled(context, batteryLevel)) {
onSensorUpdated(
context,
batteryLevel,
batteryStatus ?: data.batteryPercent.value!!,
if (batteryStatus == "success") data.batteryPercent.value!! else "unknown",
batteryLevel.statelessIcon,
mapOf()
mapOf(
"status" to batteryStatus
),
forceUpdate = true
)
}
setListener(Listener.ENERGY, false)
Expand All @@ -282,12 +288,14 @@ class CarSensorManager :
onSensorUpdated(
context,
carName,
status ?: data.name.value!!,
if (status == "success") data.name.value!! else "unknown",
carName.statelessIcon,
mapOf(
"car_manufacturer" to data.manufacturer.value,
"car_manufactured_year" to data.year.value
)
"car_manufactured_year" to data.year.value,
"status" to status
),
forceUpdate = true
)
}
setListener(Listener.MODEL, false)
Expand All @@ -301,11 +309,13 @@ class CarSensorManager :
onSensorUpdated(
context,
carStatus,
status ?: (data.evChargePortConnected.value == true),
if (status == "success") (data.evChargePortConnected.value == true) else "unknown",
carStatus.statelessIcon,
mapOf(
"car_charge_port_open" to (data.evChargePortOpen.value == true)
)
"car_charge_port_open" to (data.evChargePortOpen.value == true),
"status" to status
),
forceUpdate = true
)
}
setListener(Listener.STATUS, false)
Expand All @@ -319,9 +329,12 @@ class CarSensorManager :
onSensorUpdated(
context,
odometerValue,
status ?: data.odometerMeters.value!!,
if (status == "success") data.odometerMeters.value!! else "unknown",
odometerValue.statelessIcon,
mapOf()
mapOf(
"status" to status
),
forceUpdate = true
)
}
setListener(Listener.MILEAGE, false)
Expand All @@ -335,29 +348,35 @@ class CarSensorManager :
onSensorUpdated(
context,
fuelType,
fuelTypeStatus ?: getFuelType(data.fuelTypes.value!!),
if (fuelTypeStatus == "success") getFuelType(data.fuelTypes.value!!) else "unknown",
fuelType.statelessIcon,
mapOf()
mapOf(
"status" to fuelTypeStatus
),
forceUpdate = true
)
}
if (isEnabled(context, evConnector)) {
onSensorUpdated(
context,
evConnector,
evConnectorTypeStatus ?: getEvConnectorType(data.evConnectorTypes.value!!),
if (evConnectorTypeStatus == "success") getEvConnectorType(data.evConnectorTypes.value!!) else "unknown",
evConnector.statelessIcon,
mapOf()
mapOf(
"status" to evConnectorTypeStatus
),
forceUpdate = true
)
}
}

private fun carValueStatus(value: Int): String? {
return when (value) {
CarValue.STATUS_SUCCESS -> null
CarValue.STATUS_SUCCESS -> "success"
CarValue.STATUS_UNAVAILABLE -> "unavailable"
CarValue.STATUS_UNKNOWN -> "unknown"
CarValue.STATUS_UNIMPLEMENTED -> "unimplemented"
else -> "unavailable"
else -> null
}
}

Expand Down
1 change: 0 additions & 1 deletion common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,6 @@
<string name="ha_assist">HA: Assist</string>
<string name="only_favorites">Only Show Favorites</string>
<string name="beacon_scanning">Beacon Monitor Scanning</string>
<string name="car_data_unavailable">Open Home Assistant app to activate the sensor</string>
<string name="basic_sensor_name_car_fuel_type">Car Fuel Type</string>
<string name="sensor_description_car_fuel_type">List of available fuel types for the connected car</string>
<string name="basic_sensor_name_car_ev_connector_type">Car EV Connector Type</string>
Expand Down

0 comments on commit 2d439f1

Please sign in to comment.