Skip to content

Commit

Permalink
Add car speed sensor (#4722)
Browse files Browse the repository at this point in the history
* Add car speed sensor

* Remove raw_speed attribute

* Remove display units as its not necessary
  • Loading branch information
dshokouhi authored Oct 16, 2024
1 parent 82391cf commit 6ed4c09
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/src/full/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-sdk tools:overrideLibrary="com.google.android.gms.threadnetwork" />
<uses-permission android:name="com.google.android.gms.permission.CAR_FUEL" />
<uses-permission android:name="com.google.android.gms.permission.CAR_MILEAGE" />
<uses-permission android:name="com.google.android.gms.permission.CAR_SPEED" />
<!-- <uses-permission android:name="android.permission.health.READ_ACTIVE_CALORIES_BURNED" />-->
<!-- <uses-permission android:name="android.permission.health.READ_TOTAL_CALORIES_BURNED" />-->
<!-- <uses-permission android:name="android.permission.health.READ_WEIGHT" />-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.car.app.hardware.info.EnergyProfile
import androidx.car.app.hardware.info.EvStatus
import androidx.car.app.hardware.info.Mileage
import androidx.car.app.hardware.info.Model
import androidx.car.app.hardware.info.Speed
import androidx.core.content.ContextCompat
import androidx.lifecycle.DefaultLifecycleObserver
import io.homeassistant.companion.android.BuildConfig
Expand Down Expand Up @@ -138,6 +139,21 @@ class CarSensorManager :
autoPermissions = listOf("com.google.android.gms.permission.CAR_FUEL"),
automotivePermissions = listOf("android.car.permission.CAR_INFO")
)
private val carSpeed = CarSensor(
SensorManager.BasicSensor(
"car_speed",
"sensor",
R.string.basic_sensor_name_car_speed,
R.string.sensor_description_car_speed,
"mdi:speedometer",
unitOfMeasurement = "m/s",
deviceClass = "speed",
stateClass = SensorManager.STATE_CLASS_MEASUREMENT,
entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC
),
autoPermissions = listOf("com.google.android.gms.permission.CAR_SPEED"),
automotivePermissions = listOf("android.car.permission.CAR_SPEED", "android.car.permission.READ_CAR_DISPLAY_UNITS")
)

private val allSensorsList = listOf(
batteryLevel,
Expand All @@ -146,30 +162,34 @@ class CarSensorManager :
evConnector,
fuelLevel,
fuelType,
odometerValue
odometerValue,
carSpeed
)

private enum class Listener {
ENERGY,
MODEL,
MILEAGE,
STATUS,
PROFILE
PROFILE,
SPEED
}

private val listenerSensors = mapOf(
Listener.ENERGY to listOf(batteryLevel, fuelLevel),
Listener.MODEL to listOf(carName),
Listener.STATUS to listOf(carChargingStatus),
Listener.MILEAGE to listOf(odometerValue),
Listener.PROFILE to listOf(evConnector, fuelType)
Listener.PROFILE to listOf(evConnector, fuelType),
Listener.SPEED to listOf(carSpeed)
)
private val listenerLastRegistered = mutableMapOf(
Listener.ENERGY to -1L,
Listener.MODEL to -1L,
Listener.STATUS to -1L,
Listener.MILEAGE to -1L,
Listener.PROFILE to -1L
Listener.PROFILE to -1L,
Listener.SPEED to -1L
)
}

Expand Down Expand Up @@ -336,6 +356,13 @@ class CarSensorManager :
car.fetchEnergyProfile(executor, ::onProfileAvailable)
}
}
Listener.SPEED -> {
if (enable) {
car.addSpeedListener(executor, ::onSpeedAvailable)
} else {
car.removeSpeedListener(::onSpeedAvailable)
}
}
}

if (enable) {
Expand Down Expand Up @@ -482,6 +509,24 @@ class CarSensorManager :
}
}

private fun onSpeedAvailable(data: Speed) {
val speedStatus = carValueStatus(data.displaySpeedMetersPerSecond.status)
Log.d(TAG, "Received speed: $data")

if (isEnabled(latestContext, carSpeed)) {
onSensorUpdated(
latestContext,
carSpeed.sensor,
if (speedStatus == "success") data.displaySpeedMetersPerSecond.value!! else STATE_UNKNOWN,
carSpeed.sensor.statelessIcon,
mapOf(
"status" to speedStatus
),
forceUpdate = true
)
}
}

private fun carValueStatus(value: Int): String? {
return when (value) {
CarValue.STATUS_SUCCESS -> "success"
Expand Down
1 change: 1 addition & 0 deletions automotive/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<uses-permission android:name="android.car.permission.CAR_ENERGY" />
<uses-permission android:name="android.car.permission.CAR_ENERGY_PORTS" />
<uses-permission android:name="android.car.permission.READ_CAR_DISPLAY_UNITS" />
<uses-permission android:name="android.car.permission.CAR_SPEED" />

<uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
<uses-feature android:name="android.hardware.telephony" android:required="false"/>
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1303,4 +1303,6 @@
<string name="sensor_description_screen_orientation">Overall orientation of the screen</string>
<string name="sensor_name_screen_rotation">Screen rotation</string>
<string name="sensor_description_screen_rotation">The rotation of the screen from its \"natural\" orientation</string>
<string name="basic_sensor_name_car_speed">Car speed</string>
<string name="sensor_description_car_speed">The current speed of the car</string>
</resources>

0 comments on commit 6ed4c09

Please sign in to comment.