Fix CommunicationManager erroring on unusual item<->channel link (#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>
pull/3337/head
Cody Cutrer 2023-01-23 12:53:24 -07:00 committed by GitHub
parent 673827733a
commit 35a70064c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 0 deletions

View File

@ -505,6 +505,14 @@ public class CommunicationManager implements EventSubscriber, RegistryChangeList
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: