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()