Skip to content
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

Calculate fan speed step count #2818

Merged
merged 3 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 getFanPercentageStep")
wjtje marked this conversation as resolved.
Show resolved Hide resolved
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