From 6889c7147085fe0ef9888cf46677a369efad591f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20L=C3=B6vdahl?= Date: Mon, 24 Jul 2023 15:07:49 +0300 Subject: [PATCH] Extend existing `friendlyState` method --- .../android/common/data/integration/Entity.kt | 11 ++++++++++- .../complications/EntityStateDataSourceService.kt | 13 +++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/io/homeassistant/companion/android/common/data/integration/Entity.kt b/common/src/main/java/io/homeassistant/companion/android/common/data/integration/Entity.kt index 2a1bbc0019e..4c049146a23 100644 --- a/common/src/main/java/io/homeassistant/companion/android/common/data/integration/Entity.kt +++ b/common/src/main/java/io/homeassistant/companion/android/common/data/integration/Entity.kt @@ -604,7 +604,7 @@ suspend fun Entity.onPressed( val Entity.friendlyName: String get() = (attributes as? Map<*, *>)?.get("friendly_name")?.toString() ?: entityId -fun Entity.friendlyState(context: Context, options: EntityRegistryOptions? = null): String { +fun Entity.friendlyState(context: Context, options: EntityRegistryOptions? = null, appendUnitOfMeasurement: Boolean = false): String { var friendlyState = when (state) { "closed" -> context.getString(commonR.string.state_closed) "closing" -> context.getString(commonR.string.state_closing) @@ -649,6 +649,15 @@ fun Entity.friendlyState(context: Context, options: EntityRegistryOptions } } } + + if (appendUnitOfMeasurement) { + val unit = (attributes as? Map<*, *>)?.get("unit_of_measurement")?.toString() + + if (unit?.isNotEmpty() == true) { + return "$friendlyState $unit" + } + } + return friendlyState } diff --git a/wear/src/main/java/io/homeassistant/companion/android/complications/EntityStateDataSourceService.kt b/wear/src/main/java/io/homeassistant/companion/android/complications/EntityStateDataSourceService.kt index c2757f766c9..8dbb459bf54 100644 --- a/wear/src/main/java/io/homeassistant/companion/android/complications/EntityStateDataSourceService.kt +++ b/wear/src/main/java/io/homeassistant/companion/android/complications/EntityStateDataSourceService.kt @@ -80,12 +80,13 @@ class EntityStateDataSourceService : SuspendingComplicationDataSourceService() { null } - var stateText = entity.friendlyState(this, entityOptions) - if (entity.attributes.containsKey("unit_of_measurement")) { - stateText += " " + entity.attributes["unit_of_measurement"] - } - - val text = PlainComplicationText.Builder(stateText).build() + val text = PlainComplicationText.Builder( + entity.friendlyState( + this, + entityOptions, + appendUnitOfMeasurement = true + ) + ).build() val contentDescription = PlainComplicationText.Builder(getText(R.string.complication_entity_state_content_description)).build() val monochromaticImage = MonochromaticImage.Builder(Icon.createWithBitmap(iconBitmap)).build()