-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add a seperate battery_state sensor #530
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
Changes from 4 commits
8e0d89b
b16f34b
ba86c95
fa315ed
ff28545
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ class BatterySensorManager : SensorManager { | |
| override fun getSensorRegistrations(context: Context): List<SensorRegistration<Any>> { | ||
| return context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))?.let { | ||
| val retVal = ArrayList<SensorRegistration<Any>>() | ||
|
|
||
| getBatteryLevelSensor(it)?.let { sensor -> | ||
| retVal.add( | ||
| SensorRegistration( | ||
|
|
@@ -28,60 +29,117 @@ class BatterySensorManager : SensorManager { | |
| ) | ||
| } | ||
|
|
||
| getBatteryStateSensor(it)?.let { sensor -> | ||
| retVal.add( | ||
| SensorRegistration( | ||
| sensor, | ||
| "Battery State", | ||
| "battery" | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| return@let retVal | ||
| } ?: listOf() | ||
| } | ||
|
|
||
| override fun getSensors(context: Context): List<Sensor<Any>> { | ||
| val retVal = ArrayList<Sensor<Any>>() | ||
|
|
||
| context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))?.let { | ||
| getBatteryLevelSensor(it)?.let { sensor -> | ||
| retVal.add(sensor) | ||
| } | ||
|
|
||
| getBatteryStateSensor(it)?.let { sensor -> | ||
| retVal.add(sensor) | ||
| } | ||
| } | ||
|
|
||
| return retVal | ||
| } | ||
|
|
||
| private fun getBatteryLevelSensor(intent: Intent): Sensor<Any>? { | ||
| val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) | ||
| val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1) | ||
| val status: Int = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1) | ||
|
|
||
| if (level == -1 || scale == -1) { | ||
| Log.e(TAG, "Issue getting battery level!") | ||
| return null | ||
| } | ||
| private fun getBatteryIcon(batteryStep: Int, isCharging: Boolean = false, chargerType: String? = null, chargingStatus: String? = null): String { | ||
| var batteryIcon = "mdi:battery" | ||
|
|
||
| val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING || | ||
| status == BatteryManager.BATTERY_STATUS_FULL | ||
| if (chargingStatus == "unknown") { | ||
| batteryIcon += "-unknown" | ||
|
|
||
| val chargerType = when (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)) { | ||
| BatteryManager.BATTERY_PLUGGED_AC -> "AC" | ||
| BatteryManager.BATTERY_PLUGGED_USB -> "USB" | ||
| BatteryManager.BATTERY_PLUGGED_WIRELESS -> "Wireless" | ||
| else -> "N/A" | ||
| return batteryIcon | ||
| } | ||
|
|
||
| val percent = (level.toFloat() / scale.toFloat() * 100.0f).toInt() | ||
| var batteryIcon = "mdi:battery" | ||
| if (isCharging) | ||
| batteryIcon += "-charging" | ||
| if (chargerType == "Wireless") | ||
|
|
||
| if (chargerType == "wireless") | ||
| batteryIcon += "-wireless" | ||
|
|
||
| val batteryStep = percent / 10 | ||
| batteryIcon += when (batteryStep) { | ||
| 0 -> "-outline" | ||
| 10 -> "" | ||
| else -> "-${batteryStep}0" | ||
| } | ||
|
|
||
| return batteryIcon | ||
| } | ||
|
|
||
| private fun getBatteryLevelSensor(intent: Intent): Sensor<Any>? { | ||
| val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) | ||
| val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1) | ||
|
|
||
| if (level == -1 || scale == -1) { | ||
| Log.e(TAG, "Issue getting battery level!") | ||
| return null | ||
| } | ||
|
|
||
| val percentage: Int = (level.toFloat() / scale.toFloat() * 100.0f).toInt() | ||
| val batteryStep: Int = percentage / 10 | ||
|
|
||
| return Sensor( | ||
| "battery_level", | ||
| percent, | ||
| percentage, | ||
| "sensor", | ||
| getBatteryIcon(batteryStep), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not display the fancy battery icon for both sensors?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I figured since this sensor is now purely for the battery level, the icon should also represent just that. |
||
| mapOf() | ||
| ) | ||
| } | ||
|
|
||
| private fun getBatteryStateSensor(intent: Intent): Sensor<Any>? { | ||
| val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) | ||
| val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1) | ||
| val status: Int = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1) | ||
|
|
||
| if (level == -1 || scale == -1 || status == -1) { | ||
| Log.e(TAG, "Issue getting battery state!") | ||
| return null | ||
| } | ||
|
|
||
| val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING || | ||
| status == BatteryManager.BATTERY_STATUS_FULL | ||
|
|
||
| val chargerType: String = when (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)) { | ||
| BatteryManager.BATTERY_PLUGGED_AC -> "ac" | ||
| BatteryManager.BATTERY_PLUGGED_USB -> "usb" | ||
| BatteryManager.BATTERY_PLUGGED_WIRELESS -> "wireless" | ||
| else -> "unknown" | ||
| } | ||
|
|
||
| val chargingStatus: String = when (status) { | ||
| BatteryManager.BATTERY_STATUS_FULL -> "full" | ||
| BatteryManager.BATTERY_STATUS_CHARGING -> "charging" | ||
| BatteryManager.BATTERY_STATUS_DISCHARGING -> "discharging" | ||
| BatteryManager.BATTERY_STATUS_NOT_CHARGING -> "not_charging" | ||
| else -> "unknown" | ||
| } | ||
|
|
||
| val percentage: Int = (level.toFloat() / scale.toFloat() * 100.0f).toInt() | ||
| val batteryStep: Int = percentage / 10 | ||
|
|
||
| return Sensor( | ||
| "battery_state", | ||
| chargingStatus, | ||
| "sensor", | ||
| batteryIcon, | ||
| getBatteryIcon(batteryStep, isCharging, chargerType, chargingStatus), | ||
| mapOf( | ||
| "is_charging" to isCharging, | ||
| "charger_type" to chargerType | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.