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
This commit is contained in:
Daniel Shokouhi 2020-09-19 11:44:21 -07:00 committed by GitHub
parent 54a0d83c03
commit 30fd61ad6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<String?>
try {
fetchedAttributes = entity?.attributes as? Map<*, *> ?: mapOf<String, String>()
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) {