]> git.basschouten.com Git - openhab-addons.git/blob
0295844db534e75ec17c1fc21bb1a1f441e64f96
[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.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         PushoverAccountHandler localAccountHandler = accountHandler;
49         if (localAccountHandler != null && PUSHOVER_ACCOUNT.getAsString().equals(uri.getSchemeSpecificPart())
50                 && CONFIG_SOUND.equals(param)) {
51             List<Sound> sounds = localAccountHandler.getSounds();
52             if (!sounds.isEmpty()) {
53                 return sounds.stream().map(Sound::getAsParameterOption)
54                         .sorted(Comparator.comparing(ParameterOption::getLabel))
55                         .collect(Collectors.toUnmodifiableList());
56             }
57         }
58         return null;
59     }
60
61     @Override
62     public void setThingHandler(@Nullable ThingHandler handler) {
63         this.accountHandler = (PushoverAccountHandler) handler;
64     }
65
66     @Override
67     public @Nullable ThingHandler getThingHandler() {
68         return accountHandler;
69     }
70 }