BaseStateDescriptionProvider question

I want to upgrade my dynamic state description to include min/max/step. I am using the setStateOptions() setStatePattern() provided in the BaseStateDescriptionProvider. I created my own:

public class HoneywellStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {

    protected final Map<ChannelUID, List<BigDecimal>> channelMinMaxStepMap = new ConcurrentHashMap<>();

    @Activate
    public HoneywellStateDescriptionProvider(final @Reference EventPublisher eventPublisher, //
            final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry, //
            final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
        this.eventPublisher = eventPublisher;
        this.itemChannelLinkRegistry = itemChannelLinkRegistry;
        this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
    }

    public void setMinMaxStep(ChannelUID channelUID, List<BigDecimal> newMinMaxStep) {
        List<BigDecimal> oldMinMaxStep = channelMinMaxStepMap.get(channelUID);
        if (!oldMinMaxStep.equals(newMinMaxStep)) {
            channelMinMaxStepMap.put(channelUID, newMinMaxStep);
        }
    }

    @SuppressWarnings("unused")
    @Override
    public @Nullable StateDescription getStateDescription(Channel channel, @Nullable StateDescription original,
            @Nullable Locale locale) {
        // can be overridden by subclasses
        ChannelUID channelUID = channel.getUID();
        List<BigDecimal> bigDecimals = channelMinMaxStepMap.get(channelUID);
        if (bigDecimals == null) {
            return super.getStateDescription(channel, original, locale);
        }

        StateDescriptionFragmentBuilder builder = (original == null) ? StateDescriptionFragmentBuilder.create()
                : StateDescriptionFragmentBuilder.create(original);

        builder.withMinimum(bigDecimals.get(0)).withMaximum(bigDecimals.get(1)).withStep(bigDecimals.get(2));

        return super.getStateDescription(channel, builder.build().toStateDescription(), locale);
    }
}

But the original has:

            postEvent(ThingEventFactory.createChannelDescriptionPatternChangedEvent(channelUID,
                    itemChannelLinkRegistry != null ? itemChannelLinkRegistry.getLinkedItemNames(channelUID) : Set.of(),
                    pattern, oldPattern));

Which I have no idea what it is doing or why it is needed (never a good sign). My question is do I need to learn about it in order to get my Min/Max/Step to properly work? Since it is a black box to me I don’t know what gotchas will occur if I don’t figure it out.

The post of event is important. Look at the parent (abstract) class, it is also doing such post event.
One important usage of this event is in UIs. When receiving such an event, they know that the state description was updated and they have potentially to adjust associated widgets to take into account the new options or the new min/max.

You can check the hue or nanoleaf bindings for an example.

1 Like

I was afraid of that, I was hoping it was one of those “extra” screws or bolts that are always left over when I put something back together again :slight_smile: . I will take a look at your suggestions.

The use of StateDescriptionFragment in the NanoLeaf binding was very helpful. Thanks.