From e8d308e912aca5f05c07117fa39a2784261a8cd0 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Sun, 14 Jan 2024 21:53:57 +0100 Subject: [PATCH] [yamahareceiver] Fix ChannelTypeProvider (#16278) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [yamahareceiver] Fix ChannelTypeProvider * [yamaha] Fix remaining ChannelTypeProvider (#23) Also-by: Florian Hotze Signed-off-by: Jan N. Klug Signed-off-by: Jørgen Austvik --- .../ChannelsTypeProviderAvailableInputs.java | 21 +++++++++-------- .../internal/ChannelsTypeProviderPreset.java | 23 +++++++++++-------- .../handler/YamahaZoneThingHandler.java | 21 ++++++++++++----- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/ChannelsTypeProviderAvailableInputs.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/ChannelsTypeProviderAvailableInputs.java index 6255d3ce87296..489417691786b 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/ChannelsTypeProviderAvailableInputs.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/ChannelsTypeProviderAvailableInputs.java @@ -47,31 +47,35 @@ ChannelTypeProvider.class }) @NonNullByDefault public class ChannelsTypeProviderAvailableInputs implements ChannelTypeProvider, ThingHandlerService { - private @NonNullByDefault({}) ChannelType channelType; - private @NonNullByDefault({}) ChannelTypeUID channelTypeUID; - private @NonNullByDefault({}) YamahaZoneThingHandler handler; + private @Nullable ChannelType channelType; + private @Nullable ChannelTypeUID channelTypeUID; + private @Nullable YamahaZoneThingHandler handler; @Override public Collection getChannelTypes(@Nullable Locale locale) { - return Set.of(channelType); + ChannelType channelType = this.channelType; + return channelType == null ? Set.of() : Set.of(channelType); } @Override public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) { - if (this.channelTypeUID.equals(channelTypeUID)) { + if (channelTypeUID.equals(this.channelTypeUID)) { return channelType; } else { return null; } } - public ChannelTypeUID getChannelTypeUID() { + public @Nullable ChannelTypeUID getChannelTypeUID() { return channelTypeUID; } private void createChannelType(StateDescriptionFragment state) { - channelType = ChannelTypeBuilder.state(channelTypeUID, "Input source", "String") - .withDescription("Select the input source of the AVR").withStateDescriptionFragment(state).build(); + ChannelTypeUID channelTypeUID = this.channelTypeUID; + if (channelTypeUID != null) { + channelType = ChannelTypeBuilder.state(channelTypeUID, "Input source", "String") + .withDescription("Select the input source of the AVR").withStateDescriptionFragment(state).build(); + } } private StateDescriptionFragment getDefaultStateDescription() { @@ -128,7 +132,6 @@ public void changeAvailableInputs(Map availableInputs) { .withOptions(options).build()); } - @NonNullByDefault({}) @Override public void setThingHandler(ThingHandler handler) { this.handler = (YamahaZoneThingHandler) handler; diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/ChannelsTypeProviderPreset.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/ChannelsTypeProviderPreset.java index e49939016e414..3b9a02b48ae20 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/ChannelsTypeProviderPreset.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/ChannelsTypeProviderPreset.java @@ -46,25 +46,26 @@ @Component(scope = ServiceScope.PROTOTYPE, service = { ChannelsTypeProviderPreset.class, ChannelTypeProvider.class }) @NonNullByDefault public class ChannelsTypeProviderPreset implements ChannelTypeProvider, ThingHandlerService { - private @NonNullByDefault({}) ChannelType channelType; - private @NonNullByDefault({}) ChannelTypeUID channelTypeUID; - private @NonNullByDefault({}) YamahaZoneThingHandler handler; + private @Nullable ChannelType channelType; + private @Nullable ChannelTypeUID channelTypeUID; + private @Nullable YamahaZoneThingHandler handler; @Override public Collection getChannelTypes(@Nullable Locale locale) { - return Set.of(channelType); + ChannelType channelType = this.channelType; + return channelType == null ? Set.of() : Set.of(channelType); } @Override public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) { - if (this.channelTypeUID.equals(channelTypeUID)) { + if (channelTypeUID.equals(this.channelTypeUID)) { return channelType; } else { return null; } } - public ChannelTypeUID getChannelTypeUID() { + public @Nullable ChannelTypeUID getChannelTypeUID() { return channelTypeUID; } @@ -83,12 +84,14 @@ public void changePresetNames(List presets) { } private void createChannelType(StateDescriptionFragment state) { - channelType = ChannelTypeBuilder.state(channelTypeUID, "Preset", "Number") - .withDescription("Select a saved channel by its preset number").withStateDescriptionFragment(state) - .build(); + ChannelTypeUID channelTypeUID = this.channelTypeUID; + if (channelTypeUID != null) { + channelType = ChannelTypeBuilder.state(channelTypeUID, "Preset", "Number") + .withDescription("Select a saved channel by its preset number").withStateDescriptionFragment(state) + .build(); + } } - @NonNullByDefault({}) @Override public void setThingHandler(ThingHandler handler) { this.handler = (YamahaZoneThingHandler) handler; diff --git a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaZoneThingHandler.java b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaZoneThingHandler.java index 192883f7c7a20..d9f54a7892994 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaZoneThingHandler.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/main/java/org/openhab/binding/yamahareceiver/internal/handler/YamahaZoneThingHandler.java @@ -73,6 +73,7 @@ import org.openhab.core.thing.binding.BaseThingHandler; import org.openhab.core.thing.binding.ThingHandlerService; import org.openhab.core.thing.binding.builder.ChannelBuilder; +import org.openhab.core.thing.type.ChannelTypeUID; import org.openhab.core.types.Command; import org.openhab.core.types.RefreshType; import org.slf4j.Logger; @@ -755,8 +756,12 @@ public void availableInputsChanged(AvailableInputState msg) { // Remove the old channel and add the new channel. The channel will be requested from the // yamahaChannelTypeProvider. ChannelUID inputChannelUID = new ChannelUID(thing.getUID(), CHANNEL_GROUP_ZONE, CHANNEL_INPUT); - Channel channel = ChannelBuilder.create(inputChannelUID, "String") - .withType(channelsTypeProviderAvailableInputs.getChannelTypeUID()).build(); + ChannelTypeUID channelTypeUID = channelsTypeProviderAvailableInputs.getChannelTypeUID(); + if (channelTypeUID == null) { + logger.warn("ChannelTypeUID is null, this should not happen."); + return; + } + Channel channel = ChannelBuilder.create(inputChannelUID, "String").withType(channelTypeUID).build(); updateThing(editThing().withoutChannel(inputChannelUID).withChannel(channel).build()); } @@ -795,11 +800,15 @@ public void presetInfoUpdated(PresetInfoState msg) { // Remove the old channel and add the new channel. The channel will be requested from the // channelsTypeProviderPreset. - ChannelUID inputChannelUID = new ChannelUID(thing.getUID(), CHANNEL_GROUP_PLAYBACK, + ChannelUID presetChannelUID = new ChannelUID(thing.getUID(), CHANNEL_GROUP_PLAYBACK, CHANNEL_PLAYBACK_PRESET); - Channel channel = ChannelBuilder.create(inputChannelUID, "Number") - .withType(channelsTypeProviderPreset.getChannelTypeUID()).build(); - updateThing(editThing().withoutChannel(inputChannelUID).withChannel(channel).build()); + ChannelTypeUID channelTypeUID = channelsTypeProviderPreset.getChannelTypeUID(); + if (channelTypeUID == null) { + logger.warn("ChannelTypeUID is null, this should not happen."); + return; + } + Channel channel = ChannelBuilder.create(presetChannelUID, "Number").withType(channelTypeUID).build(); + updateThing(editThing().withoutChannel(presetChannelUID).withChannel(channel).build()); } updateState(grpPlayback(CHANNEL_PLAYBACK_PRESET), new DecimalType(msg.presetChannel));