Skip to content

Commit

Permalink
Fix CommunicationManager erroring on unusual item<->channel link (ope…
Browse files Browse the repository at this point in the history
…nhab#3326)

Some bindings that dynamically create channels (mqtt.homie) might create
a channel that declares itself as Number:Dimensionless because the
end-device metadata has a unit of "%". But a savvy user might want to
link that to a Dimmer or Rollershutter item depending on what the device
actually is. This actually works just fine since QuantityType (with unit
PERCENT) and PercentType implicitly convert between each other. Except
this odd compatibility code in CommunicationManager that tries to assist
bindings that _aren't_ QuantityType compatible. It assumes if the channel's
item type is a dimensioned item, that the actual Item is a NumberItem,
and casts without checking. All this does is checks for that case to avoid
a ClassCastException.

Signed-off-by: Cody Cutrer <cody@cutrer.us>
  • Loading branch information
ccutrer authored Jan 23, 2023
1 parent 6738277 commit 35a7006
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,14 @@ private boolean hasDimension(Item item, @Nullable String acceptedItemType) {

private @Nullable QuantityType<?> convertToQuantityType(DecimalType originalType, Item item,
@Nullable String acceptedItemType) {
if (!(item instanceof NumberItem)) {
// PercentType command sent via DimmerItem to a channel that's dimensioned
// (such as Number:Dimensionless, expecting a %).
// We can't know the proper units to add, so just pass it through and assume
// The binding can deal with it.
return null;
}

NumberItem numberItem = (NumberItem) item;

// DecimalType command sent via a NumberItem with dimension:
Expand Down

0 comments on commit 35a7006

Please sign in to comment.