From 30fd61ad6e391ca558fa20037704c78ed94f894b Mon Sep 17 00:00:00 2001 From: Daniel Shokouhi Date: Sat, 19 Sep 2020 11:44:21 -0700 Subject: [PATCH] Catch the exception when we get null for attributes (#958) * Catch the exception when we get null for attributes * Review comments * Review comments for proper fix --- .../companion/android/widgets/StaticWidget.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/StaticWidget.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/StaticWidget.kt index 20985612c..a3fe54ef9 100644 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/StaticWidget.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/StaticWidget.kt @@ -125,9 +125,16 @@ class StaticWidget : AppWidgetProvider() { if (attributeIds == null) return entity?.state - val fetchedAttributes = entity?.attributes as Map<*, *> - val attributeValues = attributeIds.split(",").map { id -> fetchedAttributes.get(id)?.toString() } - return entity.state.plus(if (attributeValues.isNotEmpty()) stateSeparator else "").plus(attributeValues.joinToString(attributeSeparator)) + var fetchedAttributes: Map<*, *> + var attributeValues: List + try { + fetchedAttributes = entity?.attributes as? Map<*, *> ?: mapOf() + attributeValues = attributeIds.split(",").map { id -> fetchedAttributes.get(id)?.toString() } + return entity?.state.plus(if (attributeValues.isNotEmpty()) stateSeparator else "").plus(attributeValues.joinToString(attributeSeparator)) + } catch (e: Exception) { + Log.d(TAG, "Unable to fetch entity state and attributes", e) + } + return null } override fun onReceive(context: Context, intent: Intent) {