]> git.basschouten.com Git - openhab-addons.git/blob
e72fd3c7002a3b11da412ccd8d152f57d0ce3a8b
[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.amplipi.internal;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Locale;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.amplipi.internal.model.Preset;
22 import org.openhab.core.thing.Channel;
23 import org.openhab.core.thing.binding.BaseDynamicCommandDescriptionProvider;
24 import org.openhab.core.thing.binding.ThingHandler;
25 import org.openhab.core.thing.binding.ThingHandlerService;
26 import org.openhab.core.thing.type.ChannelTypeUID;
27 import org.openhab.core.types.CommandDescription;
28 import org.openhab.core.types.CommandOption;
29
30 /**
31  * This class provides the list of valid commands for the preset channel.
32  *
33  * @author Kai Kreuzer - Initial contribution
34  *
35  */
36 @NonNullByDefault
37 public class PresetCommandOptionProvider extends BaseDynamicCommandDescriptionProvider implements ThingHandlerService {
38
39     private @Nullable AmpliPiHandler handler;
40
41     @Override
42     public void setThingHandler(ThingHandler handler) {
43         this.handler = (AmpliPiHandler) handler;
44     }
45
46     @Override
47     public @Nullable ThingHandler getThingHandler() {
48         return handler;
49     }
50
51     @Override
52     public @Nullable CommandDescription getCommandDescription(Channel channel,
53             @Nullable CommandDescription originalCommandDescription, @Nullable Locale locale) {
54         ChannelTypeUID typeUID = channel.getChannelTypeUID();
55         List<CommandOption> options = new ArrayList<>();
56         if (typeUID != null && AmpliPiBindingConstants.CHANNEL_PRESET.equals(typeUID.getId()) && handler != null) {
57             List<Preset> presets = handler.getPresets();
58             for (Preset preset : presets) {
59                 options.add(new CommandOption(preset.getId().toString(), preset.getName()));
60             }
61             setCommandOptions(channel.getUID(), options);
62         }
63         return super.getCommandDescription(channel, originalCommandDescription, locale);
64     }
65 }