]> git.basschouten.com Git - openhab-addons.git/blob
b9b0a77469b7846fd35c0c57e58bd16e978ddf32
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.pushover.internal.config;
14
15 import static org.openhab.binding.pushover.internal.PushoverBindingConstants.*;
16
17 import java.net.URI;
18 import java.util.Collection;
19 import java.util.Comparator;
20 import java.util.List;
21 import java.util.Locale;
22 import java.util.stream.Collectors;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.pushover.internal.dto.Sound;
27 import org.openhab.binding.pushover.internal.handler.PushoverAccountHandler;
28 import org.openhab.core.config.core.ConfigOptionProvider;
29 import org.openhab.core.config.core.ParameterOption;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerService;
32 import org.osgi.service.component.annotations.Component;
33
34 /**
35  * The {@link PushoverConfigOptionProvider} class contains fields mapping thing configuration parameters.
36  *
37  * @author Christoph Weitkamp - Initial contribution
38  */
39 @NonNullByDefault
40 @Component(service = ConfigOptionProvider.class)
41 public class PushoverConfigOptionProvider implements ConfigOptionProvider, ThingHandlerService {
42
43     private @Nullable PushoverAccountHandler accountHandler;
44
45     @Override
46     public @Nullable Collection<ParameterOption> getParameterOptions(URI uri, String param, @Nullable String context,
47             @Nullable Locale locale) {
48         if (accountHandler != null && PUSHOVER_ACCOUNT.getAsString().equals(uri.getSchemeSpecificPart())
49                 && CONFIG_SOUND.equals(param)) {
50             List<Sound> sounds = accountHandler.getSounds();
51             if (!sounds.isEmpty()) {
52                 return sounds.stream().map(Sound::getAsParameterOption)
53                         .sorted(Comparator.comparing(ParameterOption::getLabel))
54                         .collect(Collectors.toUnmodifiableList());
55             }
56         }
57         return null;
58     }
59
60     @Override
61     public void setThingHandler(@Nullable ThingHandler handler) {
62         this.accountHandler = (PushoverAccountHandler) handler;
63     }
64
65     @Override
66     public @Nullable ThingHandler getThingHandler() {
67         return accountHandler;
68     }
69 }