Skip to content

Commit

Permalink
Add graceful handling of not being able to fetch entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martreides committed Jan 10, 2025
1 parent c644513 commit e395ce5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 44 deletions.
1 change: 1 addition & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,7 @@
<string name="tile_icon">Tile icon</string>
<string name="tile_icon_original">Use entity icon</string>
<string name="tile_select">Select a tile to edit</string>
<string name="tile_fetch_entity_error">Cannot fetch entity</string>
<string name="shortcut_pinned">Pinned shortcuts</string>
<string name="remote_debugging">WebView remote debugging</string>
<string name="remote_debugging_summary">Allow remote debugging of WebView to troubleshoot Home Assistant frontend issues</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.homeassistant.companion.android.tiles

import android.util.Log
import androidx.wear.protolayout.ActionBuilders
import androidx.wear.protolayout.ColorBuilders
import androidx.wear.protolayout.DimensionBuilders
Expand Down Expand Up @@ -62,59 +63,84 @@ class ThermostatTile : TileService() {
if (wearPrefsRepository.getWearHapticFeedback()) hapticClick(applicationContext)
}

val entity = tileConfig?.entityId?.let {
serverManager.integrationRepository().getEntity(it)
}
try {
val entity = tileConfig?.entityId?.let {
serverManager.integrationRepository().getEntity(it)
}

val lastId = requestParams.currentState.lastClickableId
var targetTemp = tileConfig?.targetTemperature ?: entity?.attributes?.get("temperature").toString().toFloat()
val lastId = requestParams.currentState.lastClickableId
var targetTemp = tileConfig?.targetTemperature ?: entity?.attributes?.get("temperature").toString().toFloat()

if (lastId == TAP_ACTION_UP || lastId == TAP_ACTION_DOWN) {
val entityStr = entity?.entityId.toString()
val stepSize = entity?.attributes?.get("target_temp_step").toString().toFloat()
val updatedTargetTemp = targetTemp + if (lastId == "Up") +stepSize else -stepSize
if (lastId == TAP_ACTION_UP || lastId == TAP_ACTION_DOWN) {
val entityStr = entity?.entityId.toString()
val stepSize = entity?.attributes?.get("target_temp_step").toString().toFloat()
val updatedTargetTemp = targetTemp + if (lastId == "Up") +stepSize else -stepSize

serverManager.integrationRepository().callAction(
entityStr.split(".")[0],
"set_temperature",
hashMapOf(
"entity_id" to entityStr,
"temperature" to updatedTargetTemp
serverManager.integrationRepository().callAction(
entityStr.split(".")[0],
"set_temperature",
hashMapOf(
"entity_id" to entityStr,
"temperature" to updatedTargetTemp
)
)
)
val updated = tileConfig?.copy(targetTemperature = updatedTargetTemp) ?: ThermostatTile(id = tileId, targetTemperature = updatedTargetTemp)
thermostatTileDao.add(updated)
targetTemp = updatedTargetTemp
} else {
val updated = tileConfig?.copy(targetTemperature = null) ?: ThermostatTile(id = tileId, targetTemperature = null)
thermostatTileDao.add(updated)
}
val updated = tileConfig?.copy(targetTemperature = updatedTargetTemp) ?: ThermostatTile(id = tileId, targetTemperature = updatedTargetTemp)
thermostatTileDao.add(updated)
targetTemp = updatedTargetTemp
} else {
val updated = tileConfig?.copy(targetTemperature = null) ?: ThermostatTile(id = tileId, targetTemperature = null)
thermostatTileDao.add(updated)
}

val freshness = when {
(tileConfig?.refreshInterval != null && tileConfig.refreshInterval!! <= 1) -> 0
tileConfig?.refreshInterval != null -> tileConfig.refreshInterval!!
else -> DEFAULT_REFRESH_INTERVAL
}
val freshness = when {
(tileConfig?.refreshInterval != null && tileConfig.refreshInterval!! <= 1) -> 0
tileConfig?.refreshInterval != null -> tileConfig.refreshInterval!!
else -> DEFAULT_REFRESH_INTERVAL
}

Tile.Builder()
.setResourcesVersion("$TAG$tileId.${System.currentTimeMillis()}")
.setFreshnessIntervalMillis(TimeUnit.SECONDS.toMillis(freshness))
.setTileTimeline(
if (serverManager.isRegistered()) {
timeline(
tileConfig,
targetTemp
)
} else {
loggedOutTimeline(
Tile.Builder()
.setResourcesVersion("$TAG$tileId.${System.currentTimeMillis()}")
.setFreshnessIntervalMillis(TimeUnit.SECONDS.toMillis(freshness))
.setTileTimeline(
if (serverManager.isRegistered()) {
timeline(
tileConfig,
targetTemp
)
} else {
loggedOutTimeline(
this@ThermostatTile,
requestParams,
R.string.thermostat,
R.string.thermostat_tile_log_in
)
}
)
.build()
} catch (e: Exception) {
Log.e(TAG, "Unable to fetch entity ${tileConfig?.entityId}", e)

val freshness = when {
(tileConfig?.refreshInterval != null && tileConfig.refreshInterval!! <= 1) -> 0
tileConfig?.refreshInterval != null -> tileConfig.refreshInterval!!
else -> DEFAULT_REFRESH_INTERVAL
}

Tile.Builder()
.setResourcesVersion("$TAG$tileId.${System.currentTimeMillis()}")
.setFreshnessIntervalMillis(TimeUnit.SECONDS.toMillis(freshness))
.setTileTimeline(
primaryLayoutTimeline(
this@ThermostatTile,
requestParams,
R.string.thermostat,
R.string.thermostat_tile_log_in
null,
R.string.tile_fetch_entity_error,
R.string.refresh,
ActionBuilders.LoadAction.Builder().build()
)
}
)
.build()
)
.build()
}
}

override fun onTileResourcesRequest(requestParams: ResourcesRequest): ListenableFuture<Resources> = serviceScope.future {
Expand Down

0 comments on commit e395ce5

Please sign in to comment.