2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.yamahareceiver.internal;
15 import static java.util.stream.Collectors.toList;
16 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.*;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Locale;
22 import java.util.stream.IntStream;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.yamahareceiver.internal.handler.YamahaZoneThingHandler;
27 import org.openhab.binding.yamahareceiver.internal.state.PresetInfoState;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.thing.binding.ThingHandlerService;
30 import org.openhab.core.thing.type.ChannelType;
31 import org.openhab.core.thing.type.ChannelTypeBuilder;
32 import org.openhab.core.thing.type.ChannelTypeProvider;
33 import org.openhab.core.thing.type.ChannelTypeUID;
34 import org.openhab.core.types.StateDescriptionFragment;
35 import org.openhab.core.types.StateDescriptionFragmentBuilder;
36 import org.openhab.core.types.StateOption;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.ServiceScope;
41 * Provide a custom channel type for the preset channel
43 * @author David Graeff - Initial contribution
44 * @author Tomasz Maruszak - RX-V3900 compatibility improvements
46 @Component(scope = ServiceScope.PROTOTYPE, service = { ChannelsTypeProviderPreset.class, ChannelTypeProvider.class })
48 public class ChannelsTypeProviderPreset implements ChannelTypeProvider, ThingHandlerService {
49 private @NonNullByDefault({}) ChannelType channelType;
50 private @NonNullByDefault({}) ChannelTypeUID channelTypeUID;
51 private @NonNullByDefault({}) YamahaZoneThingHandler handler;
54 public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
55 return Set.of(channelType);
59 public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
60 if (this.channelTypeUID.equals(channelTypeUID)) {
67 public ChannelTypeUID getChannelTypeUID() {
68 return channelTypeUID;
71 private StateDescriptionFragment getDefaultStateDescription() {
72 List<StateOption> options = IntStream.rangeClosed(1, 40)
73 .mapToObj(i -> new StateOption(Integer.toString(i), "Item_" + i)).collect(toList());
74 return StateDescriptionFragmentBuilder.create().withPattern("%s").withReadOnly(false).withOptions(options)
78 public void changePresetNames(List<PresetInfoState.Preset> presets) {
79 List<StateOption> options = presets.stream()
80 .map(preset -> new StateOption(String.valueOf(preset.getValue()), preset.getName())).collect(toList());
81 createChannelType(StateDescriptionFragmentBuilder.create().withPattern("%s").withReadOnly(false)
82 .withOptions(options).build());
85 private void createChannelType(StateDescriptionFragment state) {
86 channelType = ChannelTypeBuilder.state(channelTypeUID, "Preset", "Number")
87 .withDescription("Select a saved channel by its preset number").withStateDescriptionFragment(state)
93 public void setThingHandler(ThingHandler handler) {
94 this.handler = (YamahaZoneThingHandler) handler;
95 this.handler.channelsTypeProviderPreset = this;
97 * We generate a thing specific channelTypeUID, because presets absolutely depends on the thing.
99 channelTypeUID = new ChannelTypeUID(BINDING_ID,
100 CHANNEL_PLAYBACK_PRESET_TYPE_NAMED + handler.getThing().getUID().getId());
102 createChannelType(getDefaultStateDescription());
106 public @Nullable ThingHandler getThingHandler() {