[yamahareceiver] Fix ChannelTypeProvider (#16278)

* [yamahareceiver] Fix ChannelTypeProvider
* [yamaha] Fix remaining ChannelTypeProvider (#23)

Also-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
J-N-K 2024-01-14 21:53:57 +01:00 committed by Ciprian Pascu
parent a07dbca26a
commit 1b5eed74d1
3 changed files with 40 additions and 25 deletions

View File

@ -47,31 +47,35 @@ import org.osgi.service.component.annotations.ServiceScope;
ChannelTypeProvider.class }) ChannelTypeProvider.class })
@NonNullByDefault @NonNullByDefault
public class ChannelsTypeProviderAvailableInputs implements ChannelTypeProvider, ThingHandlerService { public class ChannelsTypeProviderAvailableInputs implements ChannelTypeProvider, ThingHandlerService {
private @NonNullByDefault({}) ChannelType channelType; private @Nullable ChannelType channelType;
private @NonNullByDefault({}) ChannelTypeUID channelTypeUID; private @Nullable ChannelTypeUID channelTypeUID;
private @NonNullByDefault({}) YamahaZoneThingHandler handler; private @Nullable YamahaZoneThingHandler handler;
@Override @Override
public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) { public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
return Set.of(channelType); ChannelType channelType = this.channelType;
return channelType == null ? Set.of() : Set.of(channelType);
} }
@Override @Override
public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) { public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
if (this.channelTypeUID.equals(channelTypeUID)) { if (channelTypeUID.equals(this.channelTypeUID)) {
return channelType; return channelType;
} else { } else {
return null; return null;
} }
} }
public ChannelTypeUID getChannelTypeUID() { public @Nullable ChannelTypeUID getChannelTypeUID() {
return channelTypeUID; return channelTypeUID;
} }
private void createChannelType(StateDescriptionFragment state) { private void createChannelType(StateDescriptionFragment state) {
channelType = ChannelTypeBuilder.state(channelTypeUID, "Input source", "String") ChannelTypeUID channelTypeUID = this.channelTypeUID;
.withDescription("Select the input source of the AVR").withStateDescriptionFragment(state).build(); if (channelTypeUID != null) {
channelType = ChannelTypeBuilder.state(channelTypeUID, "Input source", "String")
.withDescription("Select the input source of the AVR").withStateDescriptionFragment(state).build();
}
} }
private StateDescriptionFragment getDefaultStateDescription() { private StateDescriptionFragment getDefaultStateDescription() {
@ -128,7 +132,6 @@ public class ChannelsTypeProviderAvailableInputs implements ChannelTypeProvider,
.withOptions(options).build()); .withOptions(options).build());
} }
@NonNullByDefault({})
@Override @Override
public void setThingHandler(ThingHandler handler) { public void setThingHandler(ThingHandler handler) {
this.handler = (YamahaZoneThingHandler) handler; this.handler = (YamahaZoneThingHandler) handler;

View File

@ -46,25 +46,26 @@ import org.osgi.service.component.annotations.ServiceScope;
@Component(scope = ServiceScope.PROTOTYPE, service = { ChannelsTypeProviderPreset.class, ChannelTypeProvider.class }) @Component(scope = ServiceScope.PROTOTYPE, service = { ChannelsTypeProviderPreset.class, ChannelTypeProvider.class })
@NonNullByDefault @NonNullByDefault
public class ChannelsTypeProviderPreset implements ChannelTypeProvider, ThingHandlerService { public class ChannelsTypeProviderPreset implements ChannelTypeProvider, ThingHandlerService {
private @NonNullByDefault({}) ChannelType channelType; private @Nullable ChannelType channelType;
private @NonNullByDefault({}) ChannelTypeUID channelTypeUID; private @Nullable ChannelTypeUID channelTypeUID;
private @NonNullByDefault({}) YamahaZoneThingHandler handler; private @Nullable YamahaZoneThingHandler handler;
@Override @Override
public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) { public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
return Set.of(channelType); ChannelType channelType = this.channelType;
return channelType == null ? Set.of() : Set.of(channelType);
} }
@Override @Override
public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) { public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
if (this.channelTypeUID.equals(channelTypeUID)) { if (channelTypeUID.equals(this.channelTypeUID)) {
return channelType; return channelType;
} else { } else {
return null; return null;
} }
} }
public ChannelTypeUID getChannelTypeUID() { public @Nullable ChannelTypeUID getChannelTypeUID() {
return channelTypeUID; return channelTypeUID;
} }
@ -83,12 +84,14 @@ public class ChannelsTypeProviderPreset implements ChannelTypeProvider, ThingHan
} }
private void createChannelType(StateDescriptionFragment state) { private void createChannelType(StateDescriptionFragment state) {
channelType = ChannelTypeBuilder.state(channelTypeUID, "Preset", "Number") ChannelTypeUID channelTypeUID = this.channelTypeUID;
.withDescription("Select a saved channel by its preset number").withStateDescriptionFragment(state) if (channelTypeUID != null) {
.build(); channelType = ChannelTypeBuilder.state(channelTypeUID, "Preset", "Number")
.withDescription("Select a saved channel by its preset number").withStateDescriptionFragment(state)
.build();
}
} }
@NonNullByDefault({})
@Override @Override
public void setThingHandler(ThingHandler handler) { public void setThingHandler(ThingHandler handler) {
this.handler = (YamahaZoneThingHandler) handler; this.handler = (YamahaZoneThingHandler) handler;

View File

@ -73,6 +73,7 @@ import org.openhab.core.thing.ThingStatusInfo;
import org.openhab.core.thing.binding.BaseThingHandler; import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService; import org.openhab.core.thing.binding.ThingHandlerService;
import org.openhab.core.thing.binding.builder.ChannelBuilder; 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.Command;
import org.openhab.core.types.RefreshType; import org.openhab.core.types.RefreshType;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -755,8 +756,12 @@ public class YamahaZoneThingHandler extends BaseThingHandler
// Remove the old channel and add the new channel. The channel will be requested from the // Remove the old channel and add the new channel. The channel will be requested from the
// yamahaChannelTypeProvider. // yamahaChannelTypeProvider.
ChannelUID inputChannelUID = new ChannelUID(thing.getUID(), CHANNEL_GROUP_ZONE, CHANNEL_INPUT); ChannelUID inputChannelUID = new ChannelUID(thing.getUID(), CHANNEL_GROUP_ZONE, CHANNEL_INPUT);
Channel channel = ChannelBuilder.create(inputChannelUID, "String") ChannelTypeUID channelTypeUID = channelsTypeProviderAvailableInputs.getChannelTypeUID();
.withType(channelsTypeProviderAvailableInputs.getChannelTypeUID()).build(); 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()); updateThing(editThing().withoutChannel(inputChannelUID).withChannel(channel).build());
} }
@ -795,11 +800,15 @@ public class YamahaZoneThingHandler extends BaseThingHandler
// Remove the old channel and add the new channel. The channel will be requested from the // Remove the old channel and add the new channel. The channel will be requested from the
// channelsTypeProviderPreset. // channelsTypeProviderPreset.
ChannelUID inputChannelUID = new ChannelUID(thing.getUID(), CHANNEL_GROUP_PLAYBACK, ChannelUID presetChannelUID = new ChannelUID(thing.getUID(), CHANNEL_GROUP_PLAYBACK,
CHANNEL_PLAYBACK_PRESET); CHANNEL_PLAYBACK_PRESET);
Channel channel = ChannelBuilder.create(inputChannelUID, "Number") ChannelTypeUID channelTypeUID = channelsTypeProviderPreset.getChannelTypeUID();
.withType(channelsTypeProviderPreset.getChannelTypeUID()).build(); if (channelTypeUID == null) {
updateThing(editThing().withoutChannel(inputChannelUID).withChannel(channel).build()); 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)); updateState(grpPlayback(CHANNEL_PLAYBACK_PRESET), new DecimalType(msg.presetChannel));