Calculate fan speed step count (#2818)

* Calculate fan speed step count

* Fix linting issues

* Fixed wrong function name in log text

Co-authored-by: Joris Pelgröm <jpelgrom@users.noreply.github.com>

Co-authored-by: Joris Pelgröm <jpelgrom@users.noreply.github.com>
This commit is contained in:
Wouter van der Wal 2022-08-30 02:27:02 +02:00 committed by GitHub
parent af0371fb7b
commit 0148c65284
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package io.homeassistant.companion.android.common.data.integration
import android.graphics.Color
import android.util.Log
import java.util.Calendar
import kotlin.math.round
data class Entity<T>(
val entityId: String,
@ -80,7 +81,25 @@ fun <T> Entity<T>.getFanSpeed(): EntityPosition? {
max = maxValue
)
} catch (e: Exception) {
Log.e(EntityExt.TAG, "Unable to get getLightBrightness", e)
Log.e(EntityExt.TAG, "Unable to get getFanSpeed", e)
null
}
}
fun <T> Entity<T>.getFanSteps(): Int? {
return try {
if (!supportsFanSetSpeed()) return null
fun calculateNumStep(percentageStep: Double): Int {
val numSteps = round(100 / percentageStep).toInt()
if (numSteps <= 10) return numSteps
if (numSteps % 10 == 0) return 10
return calculateNumStep(percentageStep * 2)
}
return calculateNumStep(((attributes as Map<*, *>)["percentage_step"] as? Double)?.toDouble() ?: 1.0) - 1
} catch (e: Exception) {
Log.e(EntityExt.TAG, "Unable to get getFanSteps")
null
}
}

View File

@ -30,6 +30,7 @@ import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.EntityExt
import io.homeassistant.companion.android.common.data.integration.domain
import io.homeassistant.companion.android.common.data.integration.getFanSpeed
import io.homeassistant.companion.android.common.data.integration.getFanSteps
import io.homeassistant.companion.android.common.data.integration.getLightBrightness
import io.homeassistant.companion.android.common.data.integration.supportsFanSetSpeed
import io.homeassistant.companion.android.common.data.integration.supportsLightBrightness
@ -169,6 +170,7 @@ fun FanSpeedSlider(
val haptic = LocalHapticFeedback.current
val context = LocalContext.current
val position = entity.getFanSpeed() ?: return
val steps = entity.getFanSteps() ?: return
Column {
Text(
@ -190,7 +192,7 @@ fun FanSpeedSlider(
haptic
)
},
steps = 9,
steps = steps,
valueRange = position.min..position.max,
decreaseIcon = {
Image(