]> git.basschouten.com Git - openhab-addons.git/blob
901cf74056424389dd964ffc112ed7cc1258dd45
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.yamahareceiver.internal;
14
15 import static java.util.stream.Collectors.toList;
16 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.*;
17
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Locale;
21 import java.util.Set;
22 import java.util.stream.IntStream;
23
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
38 /**
39  * Provide a custom channel type for the preset channel
40  *
41  * @author David Graeff - Initial contribution
42  * @author Tomasz Maruszak - RX-V3900 compatibility improvements
43  */
44 @NonNullByDefault
45 public class ChannelsTypeProviderPreset implements ChannelTypeProvider, ThingHandlerService {
46     private @NonNullByDefault({}) ChannelType channelType;
47     private @NonNullByDefault({}) ChannelTypeUID channelTypeUID;
48     private @NonNullByDefault({}) YamahaZoneThingHandler handler;
49
50     @Override
51     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
52         return Set.of(channelType);
53     }
54
55     @Override
56     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
57         if (this.channelTypeUID.equals(channelTypeUID)) {
58             return channelType;
59         } else {
60             return null;
61         }
62     }
63
64     public ChannelTypeUID getChannelTypeUID() {
65         return channelTypeUID;
66     }
67
68     private StateDescriptionFragment getDefaultStateDescription() {
69         List<StateOption> options = IntStream.rangeClosed(1, 40)
70                 .mapToObj(i -> new StateOption(Integer.toString(i), "Item_" + i)).collect(toList());
71         return StateDescriptionFragmentBuilder.create().withPattern("%s").withReadOnly(false).withOptions(options)
72                 .build();
73     }
74
75     public void changePresetNames(List<PresetInfoState.Preset> presets) {
76         List<StateOption> options = presets.stream()
77                 .map(preset -> new StateOption(String.valueOf(preset.getValue()), preset.getName())).collect(toList());
78         createChannelType(StateDescriptionFragmentBuilder.create().withPattern("%s").withReadOnly(false)
79                 .withOptions(options).build());
80     }
81
82     private void createChannelType(StateDescriptionFragment state) {
83         channelType = ChannelTypeBuilder.state(channelTypeUID, "Preset", "Number")
84                 .withDescription("Select a saved channel by its preset number").withStateDescriptionFragment(state)
85                 .build();
86     }
87
88     @NonNullByDefault({})
89     @Override
90     public void setThingHandler(ThingHandler handler) {
91         this.handler = (YamahaZoneThingHandler) handler;
92         this.handler.channelsTypeProviderPreset = this;
93         /**
94          * We generate a thing specific channelTypeUID, because presets absolutely depends on the thing.
95          */
96         channelTypeUID = new ChannelTypeUID(BINDING_ID,
97                 CHANNEL_PLAYBACK_PRESET_TYPE_NAMED + handler.getThing().getUID().getId());
98
99         createChannelType(getDefaultStateDescription());
100     }
101
102     @Override
103     public @Nullable ThingHandler getThingHandler() {
104         return this.handler;
105     }
106 }