Skip to content

Commit

Permalink
Various changes to entity usage (#3816)
Browse files Browse the repository at this point in the history
* Various changes to entity usage

* Remove unneeded code

* String fixes

* Remove redundant string
  • Loading branch information
dshokouhi authored Aug 26, 2023
1 parent cde8dc9 commit 6c2e555
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ import io.homeassistant.companion.android.R
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.websocket.impl.entities.AreaRegistryResponse
import io.homeassistant.companion.android.common.util.capitalize
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeoutOrNull
import java.net.URL
import java.util.Locale
import java.util.concurrent.TimeUnit
import io.homeassistant.companion.android.common.R as commonR

Expand All @@ -36,15 +34,6 @@ object CameraControl : HaControl {
area: AreaRegistryResponse?,
baseUrl: String?
): Control.StatefulBuilder {
control.setStatusText(
when (entity.state) {
"idle" -> context.getString(commonR.string.state_idle)
"recording" -> context.getString(commonR.string.state_recording)
"streaming" -> context.getString(commonR.string.state_streaming)
else -> entity.state.capitalize(Locale.getDefault())
}
)

val image = if (baseUrl != null && (entity.attributes["entity_picture"] as? String)?.isNotBlank() == true) {
getThumbnail(baseUrl + entity.attributes["entity_picture"] as String)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,6 @@ object ClimateControl : HaControl {
area: AreaRegistryResponse?,
baseUrl: String?
): Control.StatefulBuilder {
control.setStatusText(
when (entity.state) {
"auto" -> context.getString(commonR.string.state_auto)
"cool" -> context.getString(commonR.string.state_cool)
"dry" -> context.getString(commonR.string.state_dry)
"fan_only" -> context.getString(commonR.string.state_fan_only)
"heat" -> context.getString(commonR.string.state_heat)
"heat_cool" -> context.getString(commonR.string.state_heat_cool)
"off" -> context.getString(commonR.string.state_off)
"unavailable" -> context.getString(commonR.string.state_unavailable)
else -> entity.state
}
)
val minValue = (entity.attributes["min_temp"] as? Number)?.toFloat() ?: 0f
val maxValue = (entity.attributes["max_temp"] as? Number)?.toFloat() ?: 100f
var currentValue = (entity.attributes["temperature"] as? Number)?.toFloat() ?: (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.annotation.RequiresApi
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.getCoverPosition
import io.homeassistant.companion.android.common.data.integration.isActive
import io.homeassistant.companion.android.common.data.websocket.impl.entities.AreaRegistryResponse
import io.homeassistant.companion.android.common.R as commonR

Expand All @@ -28,22 +29,12 @@ object CoverControl : HaControl {
area: AreaRegistryResponse?,
baseUrl: String?
): Control.StatefulBuilder {
control.setStatusText(
when (entity.state) {
"closed" -> context.getString(commonR.string.state_closed)
"closing" -> context.getString(commonR.string.state_closing)
"open" -> context.getString(commonR.string.state_open)
"opening" -> context.getString(commonR.string.state_opening)
"unavailable" -> context.getString(commonR.string.state_unavailable)
else -> entity.state
}
)
val position = entity.getCoverPosition()
control.setControlTemplate(
if ((entity.attributes["supported_features"] as Int) and SUPPORT_SET_POSITION == SUPPORT_SET_POSITION) {
ToggleRangeTemplate(
entity.entityId,
entity.state in listOf("open", "opening"),
entity.isActive(),
"",
RangeTemplate(
entity.entityId,
Expand All @@ -58,7 +49,7 @@ object CoverControl : HaControl {
ToggleTemplate(
entity.entityId,
ControlButton(
entity.state in listOf("open", "opening"),
entity.isActive(),
"Description"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.annotation.RequiresApi
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.domain
import io.homeassistant.companion.android.common.data.integration.isActive
import io.homeassistant.companion.android.common.data.websocket.impl.entities.AreaRegistryResponse
import io.homeassistant.companion.android.common.util.capitalize
import java.util.Locale
Expand All @@ -30,7 +31,7 @@ object DefaultSwitchControl : HaControl {
ToggleTemplate(
entity.entityId,
ControlButton(
entity.state == "on",
entity.isActive(),
"Description"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.annotation.RequiresApi
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.getFanSpeed
import io.homeassistant.companion.android.common.data.integration.isActive
import io.homeassistant.companion.android.common.data.integration.supportsFanSetSpeed
import io.homeassistant.companion.android.common.data.websocket.impl.entities.AreaRegistryResponse
import io.homeassistant.companion.android.common.R as commonR
Expand All @@ -33,7 +34,7 @@ object FanControl : HaControl {
control.setControlTemplate(
ToggleRangeTemplate(
entity.entityId,
entity.state == "on",
entity.isActive(),
"",
RangeTemplate(
entity.entityId,
Expand All @@ -50,7 +51,7 @@ object FanControl : HaControl {
ToggleTemplate(
entity.entityId,
ControlButton(
entity.state == "on",
entity.isActive(),
""
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.homeassistant.companion.android.common.R
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.domain
import io.homeassistant.companion.android.common.data.integration.friendlyState
import io.homeassistant.companion.android.common.data.websocket.impl.entities.AreaRegistryResponse
import io.homeassistant.companion.android.webview.WebViewActivity

Expand Down Expand Up @@ -46,14 +47,7 @@ interface HaControl {
(info.area?.name ?: getDomainString(context, entity))
)
control.setStatus(Control.STATUS_OK)
control.setStatusText(
when (entity.state) {
"off" -> context.getString(R.string.state_off)
"on" -> context.getString(R.string.state_on)
"unavailable" -> context.getString(R.string.state_unavailable)
else -> context.getString(R.string.state_unknown)
}
)
control.setStatusText(entity.friendlyState(context))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
control.setAuthRequired(info.authRequired)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.annotation.RequiresApi
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.getLightBrightness
import io.homeassistant.companion.android.common.data.integration.isActive
import io.homeassistant.companion.android.common.data.integration.supportsLightBrightness
import io.homeassistant.companion.android.common.data.websocket.impl.entities.AreaRegistryResponse
import io.homeassistant.companion.android.common.R as commonR
Expand All @@ -33,7 +34,7 @@ object LightControl : HaControl {
if (entity.supportsLightBrightness()) {
ToggleRangeTemplate(
entity.entityId,
entity.state == "on",
entity.isActive(),
"",
RangeTemplate(
entity.entityId,
Expand All @@ -48,7 +49,7 @@ object LightControl : HaControl {
ToggleTemplate(
entity.entityId,
ControlButton(
entity.state == "on",
entity.isActive(),
"Description"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.service.controls.templates.ToggleTemplate
import androidx.annotation.RequiresApi
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.isActive
import io.homeassistant.companion.android.common.data.websocket.impl.entities.AreaRegistryResponse
import io.homeassistant.companion.android.common.R as commonR

Expand All @@ -23,22 +24,11 @@ object LockControl : HaControl {
area: AreaRegistryResponse?,
baseUrl: String?
): Control.StatefulBuilder {
control.setStatusText(
when (entity.state) {
"jammed" -> context.getString(commonR.string.state_jammed)
"locked" -> context.getString(commonR.string.state_locked)
"locking" -> context.getString(commonR.string.state_locking)
"unlocked" -> context.getString(commonR.string.state_unlocked)
"unlocking" -> context.getString(commonR.string.state_unlocking)
"unavailable" -> context.getString(commonR.string.state_unavailable)
else -> context.getString(commonR.string.state_unknown)
}
)
control.setControlTemplate(
ToggleTemplate(
entity.entityId,
ControlButton(
entity.state == "locked",
entity.isActive(),
"Description"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.service.controls.templates.ToggleTemplate
import androidx.annotation.RequiresApi
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.isActive
import io.homeassistant.companion.android.common.data.websocket.impl.entities.AreaRegistryResponse
import io.homeassistant.companion.android.common.R as commonR

Expand All @@ -27,29 +28,11 @@ object VacuumControl : HaControl {
baseUrl: String?
): Control.StatefulBuilder {
entitySupportedFeatures = entity.attributes["supported_features"] as Int
if (entitySupportedFeatures and SUPPORT_TURN_ON != SUPPORT_TURN_ON) {
control.setStatusText(
when (entity.state) {
"cleaning" -> context.getString(commonR.string.state_cleaning)
"docked" -> context.getString(commonR.string.state_docked)
"error" -> context.getString(commonR.string.state_error)
"idle" -> context.getString(commonR.string.state_idle)
"paused" -> context.getString(commonR.string.state_paused)
"returning" -> context.getString(commonR.string.state_returning)
"unavailable" -> context.getString(commonR.string.state_unavailable)
else -> context.getString(commonR.string.state_unknown)
}
)
}
control.setControlTemplate(
ToggleTemplate(
entity.entityId,
ControlButton(
if (entitySupportedFeatures and SUPPORT_TURN_ON == SUPPORT_TURN_ON) {
entity.state == "on"
} else {
entity.state == "cleaning"
},
entity.isActive(),
"Description"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import dagger.hilt.components.SingletonComponent
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.getIcon
import io.homeassistant.companion.android.common.data.integration.isActive
import io.homeassistant.companion.android.common.data.integration.onEntityPressedWithoutState
import io.homeassistant.companion.android.common.data.servers.ServerManager
import io.homeassistant.companion.android.database.qs.TileDao
Expand Down Expand Up @@ -111,7 +112,7 @@ abstract class TileExtensions : TileService() {
) {
serverManager.integrationRepository(tileData.serverId).getEntityUpdates(listOf(tileData.entityId))?.collect {
tile.state =
if (it.state in validActiveStates) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
if (it.isActive()) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
getTileIcon(tileData.iconName, it, applicationContext)?.let { icon ->
tile.icon = Icon.createWithBitmap(icon)
}
Expand Down Expand Up @@ -161,8 +162,8 @@ abstract class TileExtensions : TileService() {
}
if (tileData.entityId.split('.')[0] in toggleDomainsWithLock) {
tile.state = when {
state?.state in validActiveStates -> Tile.STATE_ACTIVE
state?.state != null && state.state !in validActiveStates -> Tile.STATE_INACTIVE
state?.isActive() == true -> Tile.STATE_ACTIVE
state?.state != null && !state.isActive() -> Tile.STATE_INACTIVE
else -> Tile.STATE_UNAVAILABLE
}
} else {
Expand Down Expand Up @@ -332,7 +333,6 @@ abstract class TileExtensions : TileService() {
companion object {
private const val TAG = "TileExtensions"
private val toggleDomainsWithLock = EntityExt.DOMAINS_TOGGLE
private val validActiveStates = listOf("on", "open", "locked")
}

private fun handleInject() {
Expand Down
Loading

0 comments on commit 6c2e555

Please sign in to comment.