Skip to content

Commit

Permalink
add percentage support for fan controls (#1761)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Oct 11, 2021
1 parent 2edc63c commit 321df48
Showing 1 changed file with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ import kotlinx.coroutines.runBlocking
@RequiresApi(Build.VERSION_CODES.R)
class FanControl {
companion object : HaControl {
private const val SUPPORT_SET_SPEED = 1
override fun createControl(
context: Context,
entity: Entity<Map<String, Any>>
): Control {
val speeds = entity.attributes["speed_list"].toString()
.removeSurrounding("[", "]")
.split(", ")
val currentSpeed = entity.attributes["speed"].toString()

val control = Control.StatefulBuilder(
entity.entityId,
PendingIntent.getActivity(
Expand All @@ -53,19 +49,27 @@ class FanControl {
else -> context.getString(R.string.state_unknown)
}
)
if (currentSpeed.isNotBlank()) {
if ((entity.attributes["supported_features"] as Int) and SUPPORT_SET_SPEED == SUPPORT_SET_SPEED) {
val minValue = 0f
val maxValue = 100f
var currentValue =
(entity.attributes["percentage"] as? Number)?.toFloat() ?: 0f
if (currentValue < minValue)
currentValue = minValue
if (currentValue > maxValue)
currentValue = maxValue
control.setControlTemplate(
ToggleRangeTemplate(
entity.entityId,
entity.state == "on",
"",
RangeTemplate(
entity.entityId,
0f,
speeds.size.toFloat() - 1,
if (speeds.contains(currentSpeed)) speeds.indexOf(currentSpeed).toFloat() else 0f,
minValue,
maxValue,
currentValue,
1f,
"%.0f"
"%.0f%%"
)
)
)
Expand Down Expand Up @@ -97,16 +101,13 @@ class FanControl {
)
}
is FloatAction -> {
val speeds = integrationRepository.getEntity(action.templateId)
.attributes["speed_list"].toString()
.removeSurrounding("[", "]")
.split(", ")
val convertPercentage = action.newValue
integrationRepository.callService(
action.templateId.split(".")[0],
"set_speed",
"set_percentage",
hashMapOf(
"entity_id" to action.templateId,
"speed" to speeds[action.newValue.toInt()]
"percentage" to convertPercentage.toInt()
)
)
}
Expand Down

0 comments on commit 321df48

Please sign in to comment.