From adcb9f8d3642c8bb4928415985c2b7c6d9adb118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Fri, 17 Jan 2025 20:06:24 +0100 Subject: [PATCH] Add battery cycle count sensor (#4992) --- .../common/sensors/BatterySensorManager.kt | 34 ++++++++++++++++++- common/src/main/res/values/strings.xml | 2 ++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/io/homeassistant/companion/android/common/sensors/BatterySensorManager.kt b/common/src/main/java/io/homeassistant/companion/android/common/sensors/BatterySensorManager.kt index dc1056b2d50..6d5833cbdc4 100644 --- a/common/src/main/java/io/homeassistant/companion/android/common/sensors/BatterySensorManager.kt +++ b/common/src/main/java/io/homeassistant/companion/android/common/sensors/BatterySensorManager.kt @@ -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) @@ -136,7 +146,9 @@ class BatterySensorManager : SensorManager { ) override suspend fun getAvailableSensors(context: Context): List { - 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 @@ -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) + } } } @@ -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" diff --git a/common/src/main/res/values/strings.xml b/common/src/main/res/values/strings.xml index bce36907c8d..2d0031bba97 100644 --- a/common/src/main/res/values/strings.xml +++ b/common/src/main/res/values/strings.xml @@ -57,6 +57,7 @@ App Rx GB App standby bucket App Tx GB + Battery cycle count Battery health Battery level Battery state @@ -592,6 +593,7 @@ App Tx GB since last device reboot The state of the device\'s audio mode The state of the device\'s ringer mode + The number of charge cycles completed by the battery.\n\nNote: not all devices will report or update the cycle count. The health of the battery The current battery level of the device The current charging state of the battery