Skip to content

Commit

Permalink
Calculate fan speed step count (#2818)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
wjtje and jpelgrom committed Aug 30, 2022
1 parent af0371f commit 0148c65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -190,7 +192,7 @@ fun FanSpeedSlider(
haptic
)
},
steps = 9,
steps = steps,
valueRange = position.min..position.max,
decreaseIcon = {
Image(
Expand Down

0 comments on commit 0148c65

Please sign in to comment.