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

Add support for media players in device controls #4917

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class HaControlsProviderService : ControlsProviderService() {
"input_number" to DefaultSliderControl,
"light" to LightControl,
"lock" to LockControl,
"media_player" to null,
"media_player" to MediaPlayerControl,
"remote" to null,
"scene" to DefaultButtonControl,
"script" to DefaultButtonControl,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package io.homeassistant.companion.android.controls

import android.content.Context
import android.os.Build
import android.service.controls.Control
import android.service.controls.DeviceTypes
import android.service.controls.actions.BooleanAction
import android.service.controls.actions.ControlAction
import android.service.controls.actions.FloatAction
import android.service.controls.templates.ControlButton
import android.service.controls.templates.RangeTemplate
import android.service.controls.templates.ToggleRangeTemplate
import android.service.controls.templates.ToggleTemplate
import androidx.annotation.RequiresApi
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
import io.homeassistant.companion.android.common.data.integration.getVolumeLevel
import io.homeassistant.companion.android.common.data.integration.getVolumeStep
import io.homeassistant.companion.android.common.data.integration.supportsVolumeSet
import java.math.BigDecimal
import java.math.RoundingMode

@RequiresApi(Build.VERSION_CODES.R)
object MediaPlayerControl : HaControl {
override fun provideControlFeatures(
context: Context,
control: Control.StatefulBuilder,
entity: Entity<Map<String, Any>>,
info: HaControlInfo
): Control.StatefulBuilder {
val isPlaying = entity.state == "playing"
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
if (entity.supportsVolumeSet()) {
val volumeLevel = entity.getVolumeLevel()
control.setControlTemplate(
ToggleRangeTemplate(
entity.entityId,
isPlaying,
"",
RangeTemplate(
entity.entityId,
volumeLevel?.min ?: 0f,
volumeLevel?.max ?: 100f,
volumeLevel?.value ?: 0f,
entity.getVolumeStep(),
"%.0f%%"
)
)
)
} else {
control.setControlTemplate(
ToggleTemplate(
entity.entityId,
ControlButton(
isPlaying,
""
)
)
)
}
return control
}

override fun getDeviceType(entity: Entity<Map<String, Any>>): Int =
DeviceTypes.TYPE_TV
jpelgrom marked this conversation as resolved.
Show resolved Hide resolved

override fun getDomainString(context: Context, entity: Entity<Map<String, Any>>): String =
context.getString(commonR.string.media_player)

override suspend fun performAction(
integrationRepository: IntegrationRepository,
action: ControlAction
): Boolean {
when (action) {
is BooleanAction -> {
integrationRepository.callAction(
action.templateId.split(".")[0],
"media_play_pause",
hashMapOf("entity_id" to action.templateId)
)
}
is FloatAction -> {
// Convert back to accepted format:
// https://github.com/home-assistant/frontend/blob/dev/src/dialogs/more-info/controls/more-info-media_player.ts#L289
val volumeLevel = action.newValue.div(100)
integrationRepository.callAction(
action.templateId.split(".")[0],
"volume_set",
hashMapOf(
"entity_id" to action.templateId,
"volume_level" to BigDecimal(volumeLevel.toDouble()).setScale(2, RoundingMode.HALF_UP)
)
)
}
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ object EntityExt {
const val LIGHT_SUPPORT_BRIGHTNESS_DEPR = 1
const val LIGHT_SUPPORT_COLOR_TEMP_DEPR = 2
const val ALARM_CONTROL_PANEL_SUPPORT_ARM_AWAY = 2
const val MEDIA_PLAYER_SUPPORT_VOLUME_SET = 4

val DOMAINS_PRESS = listOf("button", "input_button")
val DOMAINS_TOGGLE = listOf(
Expand Down Expand Up @@ -298,6 +299,50 @@ fun <T> Entity<T>.getLightColor(): Int? {
}
}

fun <T> Entity<T>.supportsVolumeSet(): Boolean {
return try {
if (domain != "media_player") return false
((attributes as Map<*, *>)["supported_features"] as Int) and EntityExt.MEDIA_PLAYER_SUPPORT_VOLUME_SET == EntityExt.MEDIA_PLAYER_SUPPORT_VOLUME_SET
} catch (e: Exception) {
Log.e(EntityExt.TAG, "Unable to get supportsVolumeSet", e)
false
}
}

fun <T> Entity<T>.getVolumeLevel(): EntityPosition? {
return try {
if (!supportsVolumeSet()) return null

val minValue = 0f
val maxValue = 100f

// Convert to percentage to match frontend behavior:
// https://github.com/home-assistant/frontend/blob/dev/src/dialogs/more-info/controls/more-info-media_player.ts#L137
val currentValue = ((attributes as Map<*, *>)["volume_level"] as? Number)?.toFloat()?.times(100) ?: 0f
jpelgrom marked this conversation as resolved.
Show resolved Hide resolved

EntityPosition(
value = currentValue.coerceAtLeast(minValue).coerceAtMost(maxValue),
min = minValue,
max = maxValue
)
} catch (e: Exception) {
Log.e(EntityExt.TAG, "Unable to get getVolumeLevel", e)
null
}
}

fun <T> Entity<T>.getVolumeStep(): Float {
return try {
if (!supportsVolumeSet()) return 0.1f

val volumeStep = ((attributes as Map<*, *>)["volume_step"] as? Number)?.toFloat() ?: 0.1f
volumeStep.coerceAtLeast(0.01f)
} catch (e: Exception) {
Log.e(EntityExt.TAG, "Unable to get getVolumeStep", e)
0.1f
}
}

fun <T> Entity<T>.getIcon(context: Context): IIcon {
val attributes = this.attributes as Map<String, Any?>
val icon = attributes["icon"] as? String
Expand Down
Loading