Skip to content

Commit

Permalink
Add battery cycle count sensor (#4992)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpelgrom authored Jan 17, 2025
1 parent ad1be9d commit adcb9f8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ class BatterySensorManager : SensorManager {
entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC
)

private val batteryCycles = SensorManager.BasicSensor(
"battery_cycles",
"sensor",
commonR.string.basic_sensor_name_battery_cycles,
commonR.string.sensor_description_battery_cycles,
"mdi:battery-sync",
stateClass = SensorManager.STATE_CLASS_TOTAL_INCREASING,
entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC
)

fun getIsCharging(intent: Intent): Boolean {
val status: Int = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)

Expand All @@ -136,7 +146,9 @@ class BatterySensorManager : SensorManager {
)

override suspend fun getAvailableSensors(context: Context): List<SensorManager.BasicSensor> {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
defaultSensorList.plus(listOf(remainingChargeTime, batteryCycles))
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
defaultSensorList.plus(remainingChargeTime)
} else {
defaultSensorList
Expand Down Expand Up @@ -167,6 +179,9 @@ class BatterySensorManager : SensorManager {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
updateRemainingChargeTime(context)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
updateBatteryCycles(context, intent)
}
}
}

Expand Down Expand Up @@ -362,6 +377,23 @@ class BatterySensorManager : SensorManager {
)
}

@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
private suspend fun updateBatteryCycles(context: Context, intent: Intent) {
if (!isEnabled(context, batteryCycles)) {
return
}

val cycles = intent.getIntExtra(BatteryManager.EXTRA_CYCLE_COUNT, -1)

onSensorUpdated(
context,
batteryCycles,
if (cycles != -1) cycles else STATE_UNAVAILABLE,
batteryCycles.statelessIcon,
mapOf()
)
}

private fun getChargerType(intent: Intent): String {
return when (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)) {
BatteryManager.BATTERY_PLUGGED_AC -> "ac"
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 @@ -57,6 +57,7 @@
<string name="basic_sensor_name_app_rx_gb">App Rx GB</string>
<string name="basic_sensor_name_app_standby">App standby bucket</string>
<string name="basic_sensor_name_app_tx_gb">App Tx GB</string>
<string name="basic_sensor_name_battery_cycles">Battery cycle count</string>
<string name="basic_sensor_name_battery_health">Battery health</string>
<string name="basic_sensor_name_battery_level">Battery level</string>
<string name="basic_sensor_name_battery_state">Battery state</string>
Expand Down Expand Up @@ -592,6 +593,7 @@
<string name="sensor_description_app_tx_gb">App Tx GB since last device reboot</string>
<string name="sensor_description_audio_mode">The state of the device\'s audio mode</string>
<string name="sensor_description_audio_sensor">The state of the device\'s ringer mode</string>
<string name="sensor_description_battery_cycles">The number of charge cycles completed by the battery.\n\nNote: not all devices will report or update the cycle count.</string>
<string name="sensor_description_battery_health">The health of the battery</string>
<string name="sensor_description_battery_level">The current battery level of the device</string>
<string name="sensor_description_battery_state">The current charging state of the battery</string>
Expand Down

0 comments on commit adcb9f8

Please sign in to comment.